merge_apk.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. # -*- coding:utf-8 -*-
  2. import file_utils, http_utils, contants, text_utils, apk_tool, path_utils, icon_utils, package_utils
  3. import json, os, shutil, time, traceback
  4. import print_log
  5. from print_log import printlog
  6. def startMerge(config_json_path):
  7. apk_decompile_tmp_dir = ''
  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. if 'refactorSdk' in config and toBoolean(config['refactorSdk']):
  21. printlog("using refactorSdk")
  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 1
  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. printlog(errorMsg)
  48. http_utils.notify_cut_state(gcp_code, "99", errorMsg)
  49. return
  50. # cid
  51. cid = config['cid']
  52. printlog("[cid] : %s" % cid)
  53. # gid
  54. gid = config['gid']
  55. printlog("[gid] : %s" % gid)
  56. # 使用主体
  57. platform = path_utils.get_full_path('platform_sdk', config['meta_config']['platform'])
  58. printlog("[platform] : %s" % platform)
  59. channel_path = path_utils.get_sdk_channel_path(config['channel_key'])
  60. printlog("[channel_path] : %s" % channel_path)
  61. if not os.path.exists(channel_path):
  62. errorMsg = "该渠道尚未接入完成"
  63. printlog(errorMsg)
  64. http_utils.notify_cut_state(gcp_code, "99", errorMsg)
  65. return
  66. # keystore配置文件
  67. keystore_path = config['keystore_path']
  68. printlog("[keystore_path] : %s" % keystore_path)
  69. if not os.path.exists(keystore_path):
  70. errorMsg = "找不到签名文件,请检查"
  71. printlog(errorMsg)
  72. http_utils.notify_cut_state(gcp_code, "99", errorMsg)
  73. return
  74. storepass = config['storepass']
  75. printlog("[storepass] : %s" % storepass)
  76. keypass = config['keypass']
  77. printlog("[keypass] : %s" % keypass)
  78. alias = config['alias']
  79. printlog("[alias] : %s" % alias)
  80. # 反编译原apk文件的输出路径
  81. apk_decompile_out_dir = os.path.join(origin_apk_dir, origin_apk_name)
  82. printlog("[apk_decompile_out_dir] : %s" % apk_decompile_out_dir)
  83. # icon路径
  84. icon_path = config['icon']
  85. printlog("[icon_path]: %s" % icon_path)
  86. http_utils.notify_cut_state(gcp_code, "10", "开始反编译文件")
  87. # 判断反编译出的文件修改时间与apk修改时间比较,若比apk最则重新反编译
  88. ret = file_utils.compareFileModifyTime(origin_apk_full_path, apk_decompile_out_dir)
  89. if ret is not None:
  90. printlog("母包未更新,无需重新反编译")
  91. if ret is None or ret > 0:
  92. # 反编译原apk文件
  93. apk_tool.decompile(origin_apk_full_path, apk_decompile_out_dir)
  94. meta_config = config['meta_config']
  95. # 版本号
  96. version = meta_config['version']
  97. printlog("[version]: %s" % version)
  98. v_code = meta_config['v_code']
  99. printlog("[v_code]: %s" % v_code)
  100. targetSdkVersion = meta_config['targetSdkVersion']
  101. printlog("[targetSdkVersion]: %s" % targetSdkVersion)
  102. orientation = meta_config['SDK_ORIENTATION']
  103. printlog("[orientation]: %s" % orientation)
  104. http_utils.notify_cut_state(gcp_code, "20", "开始复制文件")
  105. randomNumber = text_utils.getRamdomNumber(6)
  106. apk_decompile_tmp_dir = os.path.join(origin_apk_dir, randomNumber, "dcm_tmp" + sdk_name)
  107. printlog("[apk_decompile_tmp_dir]: %s" % apk_decompile_tmp_dir)
  108. # 创建临时目录()
  109. printlog("创建并复制母包资源至临时目录")
  110. if os.path.exists(apk_decompile_tmp_dir):
  111. file_utils.del_file(apk_decompile_tmp_dir)
  112. shutil.copytree(apk_decompile_out_dir, apk_decompile_tmp_dir)
  113. http_utils.notify_cut_state(gcp_code, "30", "开始合并资源")
  114. file_utils.modifyPkgName(apk_decompile_tmp_dir, package_name)
  115. if icon_path:
  116. icon_utils.replace_icon(apk_decompile_tmp_dir, icon_path)
  117. if package_utils.pack(apk_decompile_tmp_dir, channel_path, sdk_name, package_name, platform):
  118. printlog("合并代码失败")
  119. return 1
  120. # 修改${applicationId}为包名
  121. splash_path = config['splash']
  122. if splash_path:
  123. if os.path.exists(splash_path):
  124. apk_temp_splash_path = os.path.join(apk_decompile_tmp_dir, 'res', 'drawable', 'yyxx_comm_welcome')
  125. shutil.copy(splash_path, apk_temp_splash_path)
  126. printlog("复制闪屏页至[apk_temp_splash_path] : %s" % apk_temp_splash_path)
  127. else:
  128. printlog("找不到闪屏文件,请检查[splash_path] : %s" % splash_path)
  129. http_utils.notify_cut_state(gcp_code, "50", "开始替换渠道参数")
  130. temp_yyxx_cfg_path = os.path.join(apk_decompile_tmp_dir, 'assets', 'yyxx_game', 'yyxx_cfg.properties')
  131. if not os.path.exists(temp_yyxx_cfg_path):
  132. printlog('找不到yyxx_cfg.properties,请检查游戏是否正确接入SDK')
  133. return 1
  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. except:
  216. printlog(traceback.format_exc())
  217. finally:
  218. if apk_decompile_tmp_dir:
  219. printlog('清空打包临时目录')
  220. printlog('[apk_decompile_tmp_dir]:%s' % os.path.dirname(apk_decompile_tmp_dir))
  221. # file_utils.delete_folder(os.path.dirname(apk_decompile_tmp_dir))
  222. def toBoolean(bool_str):
  223. if type(bool_str) == bool:
  224. return bool_str
  225. if bool_str == 'true':
  226. return True
  227. return False