merge_apk_v2.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 = config['meta_config']['platform']
  56. platform_path = path_utils.get_full_path('platform_sdk', config['meta_config']['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. printlog(errorMsg)
  63. http_utils.notify_cut_state(gcp_code, "99", errorMsg)
  64. return
  65. # keystore配置文件
  66. keystore_path = config['keystore_path']
  67. printlog("[keystore_path] : %s" % keystore_path)
  68. if not os.path.exists(keystore_path):
  69. errorMsg = "找不到签名文件,请检查"
  70. printlog(errorMsg)
  71. http_utils.notify_cut_state(gcp_code, "99", errorMsg)
  72. return
  73. storepass = config['storepass']
  74. printlog("[storepass] : %s" % storepass)
  75. keypass = config['keypass']
  76. printlog("[keypass] : %s" % keypass)
  77. alias = config['alias']
  78. printlog("[alias] : %s" % alias)
  79. # 反编译原apk文件的输出路径
  80. apk_decompile_out_dir = os.path.join(origin_apk_dir, origin_apk_name)
  81. printlog("[apk_decompile_out_dir] : %s" % apk_decompile_out_dir)
  82. # icon路径
  83. icon_path = config['icon']
  84. printlog("[icon_path]: %s" % icon_path)
  85. http_utils.notify_cut_state(gcp_code, "10", "开始反编译文件")
  86. # 判断反编译出的文件修改时间与apk修改时间比较,若比apk最则重新反编译
  87. ret = file_utils.compareFileModifyTime(origin_apk_full_path, apk_decompile_out_dir)
  88. if ret is not None:
  89. printlog("母包未更新,无需重新反编译")
  90. if ret is None or ret > 0:
  91. # 反编译原apk文件
  92. apk_tool.decompile(origin_apk_full_path, apk_decompile_out_dir)
  93. meta_config = config['meta_config']
  94. # 版本号
  95. version = meta_config['version']
  96. printlog("[version]: %s" % version)
  97. v_code = meta_config['v_code']
  98. printlog("[v_code]: %s" % v_code)
  99. targetSdkVersion = meta_config['targetSdkVersion']
  100. printlog("[targetSdkVersion]: %s" % targetSdkVersion)
  101. orientation = meta_config['SDK_ORIENTATION']
  102. printlog("[orientation]: %s" % orientation)
  103. http_utils.notify_cut_state(gcp_code, "20", "开始复制文件")
  104. randomNumber = text_utils.getRamdomNumber(6)
  105. apk_decompile_tmp_dir = os.path.join(origin_apk_dir, randomNumber, "dcm_tmp" + sdk_name)
  106. printlog("[apk_decompile_tmp_dir]: %s" % apk_decompile_tmp_dir)
  107. # 创建临时目录()
  108. printlog("创建并复制母包资源至临时目录")
  109. if os.path.exists(apk_decompile_tmp_dir):
  110. file_utils.del_file(apk_decompile_tmp_dir)
  111. shutil.copytree(apk_decompile_out_dir, apk_decompile_tmp_dir)
  112. http_utils.notify_cut_state(gcp_code, "30", "开始合并资源")
  113. file_utils.modifyPkgName(apk_decompile_tmp_dir, package_name)
  114. if icon_path:
  115. icon_utils.replace_icon(apk_decompile_tmp_dir, icon_path)
  116. if package_utils.pack(apk_decompile_tmp_dir,channel_path, package_name):
  117. printlog("合并代码失败")
  118. return False
  119. # 修改${applicationId}为包名
  120. splash_path = config['splash']
  121. if splash_path:
  122. if os.path.exists(splash_path):
  123. apk_temp_splash_path = os.path.join(apk_decompile_tmp_dir, 'assets','yyxx_comm_welcome.jpg')
  124. shutil.copy(splash_path, apk_temp_splash_path)
  125. printlog("复制闪屏页至[apk_temp_splash_path] : %s" % apk_temp_splash_path)
  126. else:
  127. printlog("找不到闪屏文件,请检查[splash_path] : %s" % splash_path)
  128. http_utils.notify_cut_state(gcp_code, "50", "开始替换渠道参数")
  129. temp_yyxx_cfg_path = os.path.join(apk_decompile_tmp_dir, 'assets', 'yyxx_game', 'yyxx_cfg.properties')
  130. if not os.path.exists(temp_yyxx_cfg_path):
  131. printlog('找不到yyxx_cfg.properties,请检查游戏是否正确接入SDK')
  132. return False
  133. file_utils.replace_assets_param(temp_yyxx_cfg_path, 'YYXX_GCP_CODE',
  134. gcp_code)
  135. temp_manifest_path = os.path.join(apk_decompile_tmp_dir, 'AndroidManifest.xml')
  136. sdk_client_config = config['sdk_client_config']
  137. printlog('[sdk_client_config] : %s' % sdk_client_config)
  138. if sdk_client_config:
  139. keys = sdk_client_config.keys()
  140. for key in keys:
  141. # 替换AndroidManifest.xml中的meta-data字段
  142. file_utils.replace_manifest_meta_data(temp_manifest_path, key,
  143. sdk_client_config[key])
  144. # 替换assets/YyrhParam.cnf中的关键字
  145. file_utils.replace_assets_param(temp_yyxx_cfg_path, key,
  146. sdk_client_config[key])
  147. # 特殊渠道需要特殊处理
  148. file_utils.special_replace(sdk_name, apk_decompile_tmp_dir, key, sdk_client_config[key])
  149. if not contants.isTestEnvironment():
  150. if platform == 'hnyy':
  151. file_utils.replace_assets_param(temp_yyxx_cfg_path, 'YYXX_ONLINE_ENV',
  152. 'https://sdkapi.yyxxgame.com')
  153. elif platform == 'hnqj':
  154. file_utils.replace_assets_param(temp_yyxx_cfg_path, 'YYXX_ONLINE_ENV',
  155. 'https://fxsy.qijinghao.com')
  156. elif platform == 'shxy':
  157. file_utils.replace_assets_param(temp_yyxx_cfg_path, 'YYXX_ONLINE_ENV',
  158. 'https://sdkapi.bklinok.com')
  159. elif platform == 'xinrui':
  160. file_utils.replace_assets_param(temp_yyxx_cfg_path, 'YYXX_ONLINE_ENV',
  161. 'https://sdkapi.ykklayo.com')
  162. if platform != 'hnyy':
  163. temp_res_path = os.path.join(apk_decompile_tmp_dir, 'res')
  164. file_utils.copyAllFile(platform_path,temp_res_path)
  165. meta_config = config['meta_config']
  166. if meta_config:
  167. keys = meta_config.keys()
  168. for key in keys:
  169. if key == "APP_NAME":
  170. file_utils.replace_string_app_name(apk_decompile_tmp_dir, meta_config[key])
  171. elif key == "version":
  172. version = meta_config[key]
  173. elif key == "v_code":
  174. v_code = meta_config[key]
  175. elif key == "targetSdkVersion":
  176. targetSdkVersion = meta_config[key]
  177. # 替换方向
  178. elif key == "SDK_ORIENTATION":
  179. file_utils.replace_content(temp_manifest_path, 'sdk_orientation', meta_config[key])
  180. file_utils.replace_assets_param(temp_yyxx_cfg_path, "SDK_ORIENTATION", meta_config[key])
  181. http_utils.notify_cut_state(gcp_code, "60", "开始修改版本号")
  182. yml = os.path.join(apk_decompile_tmp_dir, 'apktool.yml')
  183. file_utils.changeVersion(yml, v_code, version, targetSdkVersion)
  184. file_utils.merge_wx_page(package_name, apk_decompile_tmp_dir)
  185. http_utils.notify_cut_state(gcp_code, "70", "开始替换游戏资源")
  186. game_resource_replace = config['game_resource_replace']
  187. printlog('[game_resource_replace]:%s' % game_resource_replace)
  188. if game_resource_replace:
  189. for resource in game_resource_replace:
  190. if os.path.exists(resource[1]):
  191. file_utils.copy_file(resource[1], os.path.join(apk_decompile_tmp_dir, resource[0]), False)
  192. out_put_unsigned_apk_path = os.path.join(apk_decompile_tmp_dir, 'gen', '_unsigned.apk')
  193. out_put_signed_apk_path = os.path.join(apk_decompile_tmp_dir, 'gen', '_signed.apk')
  194. out_put_zipalign_apk_path = os.path.join(apk_decompile_tmp_dir, 'gen', '_zipaligned.apk')
  195. http_utils.notify_cut_state(gcp_code, "80", "开始回编译APK")
  196. ret = apk_tool.recompile(apk_decompile_tmp_dir, out_put_unsigned_apk_path)
  197. if ret:
  198. printlog('apk回编译失败')
  199. return ret
  200. http_utils.notify_cut_state(gcp_code, "90", "开始重签名,对齐APK")
  201. ret = apk_tool.V1signer(out_put_unsigned_apk_path, out_put_signed_apk_path, keystore_path, storepass, alias,
  202. keypass)
  203. if ret:
  204. printlog('apk签名失败')
  205. return ret
  206. printlog("开始执行apk压缩")
  207. ret = apk_tool.zipalign(out_put_signed_apk_path, out_put_zipalign_apk_path)
  208. if ret:
  209. printlog('apk压缩失败')
  210. return ret
  211. dis_path = os.path.join(contants.get_out_put_dir(),
  212. "g%s_%s_%s_%s.apk" % (gid, sdk_name, package_name, gcp_code))
  213. if not os.path.exists(contants.get_out_put_dir()):
  214. os.makedirs(contants.get_out_put_dir())
  215. file_utils.copy_file(out_put_zipalign_apk_path, dis_path)
  216. http_utils.notify_cut_state(gcp_code, "100", "切包成功(点击复制链接)")
  217. time_c = time.time() - time_start
  218. printlog("APK路径:%s\n切包总用时:%s秒" % (dis_path, time_c))
  219. isSuccess = True
  220. except:
  221. printlog(traceback.format_exc())
  222. isSuccess = False
  223. finally:
  224. if apk_decompile_tmp_dir:
  225. printlog('清空打包临时目录')
  226. printlog('[apk_decompile_tmp_dir]:%s' % os.path.dirname(apk_decompile_tmp_dir))
  227. file_utils.delete_folder(os.path.dirname(apk_decompile_tmp_dir))
  228. return isSuccess