merge_apk_v2.py 14 KB

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