merge_apk_v2.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. # -*- coding:utf-8 -*-
  2. from V2 import icon_utils, file_utils, package_utils, apk_tool, path_utils, contants, http_utils, print_log, text_utils
  3. import json, os, shutil, time, traceback
  4. from V2.print_log import printlog
  5. def startMerge(config_json_path):
  6. apk_decompile_tmp_dir = ''
  7. isSuccess = False
  8. try:
  9. time_start = time.time()
  10. json_text = file_utils.read_file(config_json_path)
  11. config = json.loads(json_text)
  12. gcp_code = config['gcp_code']
  13. print_log.LOGFILE = os.path.join(contants.SDK_LOG, "%s.txt" % gcp_code)
  14. if os.path.exists(print_log.LOGFILE):
  15. os.remove(print_log.LOGFILE)
  16. printlog("[LOGFILE] : %s" % print_log.LOGFILE)
  17. http_utils.notify_cut_state(gcp_code, "5", "开始读取配置")
  18. printlog("[gcp_code] : %s" % gcp_code)
  19. printlog("[config_json_path] : %s" % config_json_path)
  20. origin_apk_full_path = config['apk_path']
  21. printlog("[origin_apk_full_path] : %s" % origin_apk_full_path)
  22. if not os.path.exists(origin_apk_full_path):
  23. errorMsg = "找不到游戏母包,请检查"
  24. printlog(errorMsg)
  25. http_utils.notify_cut_state(gcp_code, "99", errorMsg)
  26. return 1
  27. # apk文件全名(eg: 360_0.01.150608_high_all.apk )
  28. origin_apk_full_name = os.path.basename(origin_apk_full_path)
  29. printlog("[origin_apk_full_name] : %s" % origin_apk_full_name)
  30. # apk文件名(eg: 360_0.01.150608_high_all)
  31. origin_apk_name = os.path.splitext(origin_apk_full_name)[0]
  32. printlog("[origin_apk_name] : %s" % origin_apk_name)
  33. # apk文件路径(eg: /sdk/develop/tools/outputs/mxd/package/0.01.150608/360 )
  34. origin_apk_dir = os.path.dirname(origin_apk_full_path)
  35. printlog("[origin_apk_dir] : %s" % origin_apk_dir)
  36. # 渠道名
  37. sdk_name = config['channel_key'] # sys.argv[2]
  38. printlog("[sdk_name] : %s" % sdk_name)
  39. # 包名
  40. package_name = config['package_name']
  41. printlog("[package_name] : %s" % package_name)
  42. result = text_utils.isMatchRegExp(package_name, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.")
  43. if result is False:
  44. errorMsg = "包名包含特殊字符,不允许切包"
  45. printlog(errorMsg)
  46. http_utils.notify_cut_state(gcp_code, "99", errorMsg)
  47. return
  48. # cid
  49. cid = config['cid']
  50. printlog("[cid] : %s" % cid)
  51. # gid
  52. gid = config['gid']
  53. printlog("[gid] : %s" % gid)
  54. # 使用主体
  55. platform = path_utils.get_full_path('platform_sdk', config['meta_config']['platform'])
  56. printlog("[platform] : %s" % platform)
  57. channel_path = path_utils.get_sdk_channel_path(config['channel_key'])
  58. printlog("[channel_path] : %s" % channel_path)
  59. if not os.path.exists(channel_path):
  60. errorMsg = "该渠道尚未接入完成"
  61. printlog(errorMsg)
  62. http_utils.notify_cut_state(gcp_code, "99", errorMsg)
  63. return
  64. # keystore配置文件
  65. keystore_path = config['keystore_path']
  66. printlog("[keystore_path] : %s" % keystore_path)
  67. if not os.path.exists(keystore_path):
  68. errorMsg = "找不到签名文件,请检查"
  69. printlog(errorMsg)
  70. http_utils.notify_cut_state(gcp_code, "99", errorMsg)
  71. return
  72. storepass = config['storepass']
  73. printlog("[storepass] : %s" % storepass)
  74. keypass = config['keypass']
  75. printlog("[keypass] : %s" % keypass)
  76. alias = config['alias']
  77. printlog("[alias] : %s" % alias)
  78. # 反编译原apk文件的输出路径
  79. apk_decompile_out_dir = os.path.join(origin_apk_dir, origin_apk_name)
  80. printlog("[apk_decompile_out_dir] : %s" % apk_decompile_out_dir)
  81. # icon路径
  82. icon_path = config['icon']
  83. printlog("[icon_path]: %s" % icon_path)
  84. http_utils.notify_cut_state(gcp_code, "10", "开始反编译文件")
  85. # 判断反编译出的文件修改时间与apk修改时间比较,若比apk最则重新反编译
  86. ret = file_utils.compareFileModifyTime(origin_apk_full_path, apk_decompile_out_dir)
  87. if ret is not None:
  88. printlog("母包未更新,无需重新反编译")
  89. if ret is None or ret > 0:
  90. # 反编译原apk文件
  91. apk_tool.decompile(origin_apk_full_path, apk_decompile_out_dir)
  92. meta_config = config['meta_config']
  93. # 版本号
  94. version = meta_config['version']
  95. printlog("[version]: %s" % version)
  96. v_code = meta_config['v_code']
  97. printlog("[v_code]: %s" % v_code)
  98. targetSdkVersion = meta_config['targetSdkVersion']
  99. printlog("[targetSdkVersion]: %s" % targetSdkVersion)
  100. orientation = meta_config['SDK_ORIENTATION']
  101. printlog("[orientation]: %s" % orientation)
  102. http_utils.notify_cut_state(gcp_code, "20", "开始复制文件")
  103. randomNumber = text_utils.getRamdomNumber(6)
  104. apk_decompile_tmp_dir = os.path.join(origin_apk_dir, randomNumber, "dcm_tmp" + sdk_name)
  105. printlog("[apk_decompile_tmp_dir]: %s" % apk_decompile_tmp_dir)
  106. # 创建临时目录()
  107. printlog("创建并复制母包资源至临时目录")
  108. if os.path.exists(apk_decompile_tmp_dir):
  109. file_utils.del_file(apk_decompile_tmp_dir)
  110. shutil.copytree(apk_decompile_out_dir, apk_decompile_tmp_dir)
  111. http_utils.notify_cut_state(gcp_code, "30", "开始合并资源")
  112. file_utils.modifyPkgName(apk_decompile_tmp_dir, package_name)
  113. if icon_path:
  114. icon_utils.replace_icon(apk_decompile_tmp_dir, icon_path)
  115. if package_utils.pack(apk_decompile_tmp_dir, channel_path, sdk_name, package_name, platform):
  116. printlog("合并代码失败")
  117. return False
  118. # 修改${applicationId}为包名
  119. splash_path = config['splash']
  120. if splash_path:
  121. if os.path.exists(splash_path):
  122. apk_temp_splash_path = os.path.join(apk_decompile_tmp_dir, 'assets','yyxx_comm_welcome.jpg')
  123. shutil.copy(splash_path, apk_temp_splash_path)
  124. printlog("复制闪屏页至[apk_temp_splash_path] : %s" % apk_temp_splash_path)
  125. else:
  126. printlog("找不到闪屏文件,请检查[splash_path] : %s" % splash_path)
  127. http_utils.notify_cut_state(gcp_code, "50", "开始替换渠道参数")
  128. temp_yyxx_cfg_path = os.path.join(apk_decompile_tmp_dir, 'assets', 'yyxx_game', 'yyxx_cfg.properties')
  129. if not os.path.exists(temp_yyxx_cfg_path):
  130. printlog('找不到yyxx_cfg.properties,请检查游戏是否正确接入SDK')
  131. return False
  132. file_utils.replace_assets_param(temp_yyxx_cfg_path, 'YYXX_GCP_CODE',
  133. gcp_code)
  134. temp_manifest_path = os.path.join(apk_decompile_tmp_dir, 'AndroidManifest.xml')
  135. sdk_client_config = config['sdk_client_config']
  136. printlog('[sdk_client_config] : %s' % sdk_client_config)
  137. if sdk_client_config:
  138. keys = sdk_client_config.keys()
  139. for key in keys:
  140. # 替换AndroidManifest.xml中的meta-data字段
  141. file_utils.replace_manifest_meta_data(temp_manifest_path, key,
  142. sdk_client_config[key])
  143. # 替换assets/YyrhParam.cnf中的关键字
  144. file_utils.replace_assets_param(temp_yyxx_cfg_path, key,
  145. sdk_client_config[key])
  146. # 特殊渠道需要特殊处理
  147. file_utils.special_replace(sdk_name, apk_decompile_tmp_dir, key, sdk_client_config[key])
  148. if not contants.isTestEnvironment():
  149. if platform == 'hnyy':
  150. file_utils.replace_assets_param(temp_yyxx_cfg_path, 'YYXX_ONLINE_ENV',
  151. 'https://sdkapi.yyxxgame.com')
  152. elif platform == 'hnqj':
  153. file_utils.replace_assets_param(temp_yyxx_cfg_path, 'YYXX_ONLINE_ENV',
  154. 'https://fxsy.qijinghao.com')
  155. elif platform == 'shxy':
  156. file_utils.replace_assets_param(temp_yyxx_cfg_path, 'YYXX_ONLINE_ENV',
  157. 'https://sdkapi.bklinok.com')
  158. elif platform == 'xinrui':
  159. file_utils.replace_assets_param(temp_yyxx_cfg_path, 'YYXX_ONLINE_ENV',
  160. 'https://sdkapi.ykklayo.com')
  161. meta_config = config['meta_config']
  162. if meta_config:
  163. keys = meta_config.keys()
  164. for key in keys:
  165. if key == "APP_NAME":
  166. file_utils.replace_string_app_name(apk_decompile_tmp_dir, meta_config[key])
  167. elif key == "version":
  168. version = meta_config[key]
  169. elif key == "v_code":
  170. v_code = meta_config[key]
  171. elif key == "targetSdkVersion":
  172. targetSdkVersion = meta_config[key]
  173. # 替换方向
  174. elif key == "SDK_ORIENTATION":
  175. file_utils.replace_content(temp_manifest_path, 'sdk_orientation', meta_config[key])
  176. file_utils.replace_assets_param(temp_yyxx_cfg_path, "SDK_ORIENTATION", meta_config[key])
  177. http_utils.notify_cut_state(gcp_code, "60", "开始修改版本号")
  178. yml = os.path.join(apk_decompile_tmp_dir, 'apktool.yml')
  179. file_utils.changeVersion(yml, v_code, version, targetSdkVersion)
  180. file_utils.merge_wx_page(package_name, apk_decompile_tmp_dir)
  181. http_utils.notify_cut_state(gcp_code, "70", "开始替换游戏资源")
  182. game_resource_replace = config['game_resource_replace']
  183. printlog('[game_resource_replace]:%s' % game_resource_replace)
  184. if game_resource_replace:
  185. for resource in game_resource_replace:
  186. if os.path.exists(resource[1]):
  187. file_utils.copy_file(resource[1], os.path.join(apk_decompile_tmp_dir, resource[0]), False)
  188. out_put_unsigned_apk_path = os.path.join(apk_decompile_tmp_dir, 'gen', '_unsigned.apk')
  189. out_put_signed_apk_path = os.path.join(apk_decompile_tmp_dir, 'gen', '_signed.apk')
  190. out_put_zipalign_apk_path = os.path.join(apk_decompile_tmp_dir, 'gen', '_zipaligned.apk')
  191. http_utils.notify_cut_state(gcp_code, "80", "开始回编译APK")
  192. ret = apk_tool.recompile(apk_decompile_tmp_dir, out_put_unsigned_apk_path)
  193. if ret:
  194. printlog('apk回编译失败')
  195. return ret
  196. http_utils.notify_cut_state(gcp_code, "90", "开始重签名,对齐APK")
  197. ret = apk_tool.V1signer(out_put_unsigned_apk_path, out_put_signed_apk_path, keystore_path, storepass, alias,
  198. keypass)
  199. if ret:
  200. printlog('apk签名失败')
  201. return ret
  202. printlog("开始执行apk压缩")
  203. ret = apk_tool.zipalign(out_put_signed_apk_path, out_put_zipalign_apk_path)
  204. if ret:
  205. printlog('apk压缩失败')
  206. return ret
  207. dis_path = os.path.join(contants.get_out_put_dir(),
  208. "g%s_%s_%s_%s.apk" % (gid, sdk_name, package_name, gcp_code))
  209. if not os.path.exists(contants.get_out_put_dir()):
  210. os.makedirs(contants.get_out_put_dir())
  211. file_utils.copy_file(out_put_zipalign_apk_path, dis_path)
  212. http_utils.notify_cut_state(gcp_code, "100", "切包成功(点击复制链接)")
  213. time_c = time.time() - time_start
  214. printlog("APK路径:%s\n切包总用时:%s秒" % (dis_path, time_c))
  215. isSuccess = True
  216. except:
  217. printlog(traceback.format_exc())
  218. isSuccess = False
  219. finally:
  220. if apk_decompile_tmp_dir:
  221. printlog('清空打包临时目录')
  222. printlog('[apk_decompile_tmp_dir]:%s' % os.path.dirname(apk_decompile_tmp_dir))
  223. file_utils.delete_folder(os.path.dirname(apk_decompile_tmp_dir))
  224. return isSuccess