merge_apk.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. from PrintLog import PrintLog
  5. def startMerge(config_json_path):
  6. apk_decompile_tmp_dir = ''
  7. try:
  8. time_start = time.time()
  9. json_text = file_utils.read_file(config_json_path)
  10. config = json.loads(json_text)
  11. gcp_code = config['gcp_code']
  12. http_utils.notify_cut_state(gcp_code, "5", "开始读取配置")
  13. PrintLog.LOGFILE = os.path.join(path_utils.get_sdk_log_path(gcp_code), gcp_code)
  14. if os.path.exists(PrintLog.LOGFILE):
  15. os.remove(PrintLog.LOGFILE)
  16. PrintLog("[gcp_code] : %s" % gcp_code)
  17. PrintLog("[config_json_path] : %s" % config_json_path)
  18. if 'refactorSdk' in config and toBoolean(config['refactorSdk']):
  19. PrintLog("using refactorSdk")
  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. channel_path = path_utils.get_sdk_channel_path(config['channel_key'])
  55. PrintLog("[channel_path] : %s" % channel_path)
  56. if not os.path.exists(channel_path):
  57. errorMsg = "该渠道尚未接入完成"
  58. PrintLog(errorMsg)
  59. http_utils.notify_cut_state(gcp_code, "99", errorMsg)
  60. return
  61. # keystore配置文件
  62. keystore_path = config['keystore_path']
  63. PrintLog("[keystore_path] : %s" % keystore_path)
  64. if not os.path.exists(keystore_path):
  65. errorMsg = "找不到签名文件,请检查"
  66. PrintLog(errorMsg)
  67. http_utils.notify_cut_state(gcp_code, "99", errorMsg)
  68. return
  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 != None:
  85. PrintLog("母包未更新,无需重新反编译")
  86. if ret == 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. # minSDKVersion = meta_config['minSDKVersion']
  98. # PrintLog("[minSDKVersion]: %s"%minSDKVersion)
  99. orientation = meta_config['SDK_ORIENTATION']
  100. PrintLog("[orientation]: %s" % orientation)
  101. http_utils.notify_cut_state(gcp_code, "20", "开始复制文件")
  102. randomNumber = text_utils.getRamdomNumber(6)
  103. apk_decompile_tmp_dir = os.path.join(origin_apk_dir, randomNumber, "dcm_tmp" + sdk_name)
  104. PrintLog("[apk_decompile_tmp_dir]: %s" % apk_decompile_tmp_dir)
  105. # 创建临时目录()
  106. PrintLog("创建并复制母包资源至临时目录")
  107. if os.path.exists(apk_decompile_tmp_dir):
  108. file_utils.del_file(apk_decompile_tmp_dir)
  109. shutil.copytree(apk_decompile_out_dir, apk_decompile_tmp_dir)
  110. http_utils.notify_cut_state(gcp_code, "30", "开始合并资源")
  111. file_utils.modifyPkgName(apk_decompile_tmp_dir, package_name)
  112. if icon_path:
  113. icon_utils.replace_icon(apk_decompile_tmp_dir, icon_path)
  114. if package_utils.pack(apk_decompile_tmp_dir, channel_path, sdk_name, package_name):
  115. PrintLog("合并代码失败")
  116. return 1
  117. # 修改${applicationId}为包名
  118. splash_path = config['splash']
  119. if splash_path:
  120. if os.path.exists(splash_path):
  121. apk_temp_splash_path = os.path.join(apk_decompile_tmp_dir, 'res', 'drawable', 'yyxx_comm_welcome')
  122. shutil.copy(splash_path, apk_temp_splash_path)
  123. PrintLog("复制闪屏页至[apk_temp_splash_path] : %s" % apk_temp_splash_path)
  124. else:
  125. PrintLog("找不到闪屏文件,请检查[splash_path] : %s" % splash_path)
  126. http_utils.notify_cut_state(gcp_code, "50", "开始替换渠道参数")
  127. temp_yyxx_cfg_path = os.path.join(apk_decompile_tmp_dir, 'assets', 'yyxx_game', 'yyxx_cfg.properties')
  128. if not os.path.exists(temp_yyxx_cfg_path):
  129. PrintLog('找不到yyxx_cfg.properties,请检查游戏是否正确接入SDK')
  130. return 1
  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. meta_config = config['meta_config']
  146. if meta_config:
  147. keys = meta_config.keys()
  148. for key in keys:
  149. if key == "APP_NAME":
  150. file_utils.replace_string_app_name(apk_decompile_tmp_dir, meta_config[key])
  151. elif key == "version":
  152. version = meta_config[key]
  153. elif key == "v_code":
  154. v_code = meta_config[key]
  155. elif key == "targetSdkVersion":
  156. targetSdkVersion = meta_config[key]
  157. # 替换方向
  158. elif key == "SDK_ORIENTATION":
  159. file_utils.replace_content(temp_manifest_path, 'sdk_orientation', meta_config[key])
  160. file_utils.replace_assets_param(temp_yyxx_cfg_path, "SDK_ORIENTATION", meta_config[key])
  161. http_utils.notify_cut_state(gcp_code, "60", "开始修改版本号")
  162. yml = os.path.join(apk_decompile_tmp_dir, 'apktool.yml')
  163. file_utils.changeVersion(yml, v_code, version, targetSdkVersion)
  164. file_utils.merge_wx_page(package_name, apk_decompile_tmp_dir)
  165. http_utils.notify_cut_state(gcp_code, "70", "开始替换游戏资源")
  166. game_resource_replace = config['game_resource_replace']
  167. PrintLog('[game_resource_replace]:%s' % game_resource_replace)
  168. if game_resource_replace:
  169. for resource in game_resource_replace:
  170. if os.path.exists(resource[1]):
  171. file_utils.copy_file(resource[1], os.path.join(apk_decompile_tmp_dir, resource[0]), False)
  172. out_put_unsigned_apk_path = os.path.join(apk_decompile_tmp_dir, 'gen', '_unsigned.apk')
  173. out_put_signed_apk_path = os.path.join(apk_decompile_tmp_dir, 'gen', '_signed.apk')
  174. out_put_zipalign_apk_path = os.path.join(apk_decompile_tmp_dir, 'gen', '_zipaligned.apk')
  175. http_utils.notify_cut_state(gcp_code, "80", "开始回编译APK")
  176. ret = apk_tool.recompile(apk_decompile_tmp_dir, out_put_unsigned_apk_path)
  177. if ret:
  178. PrintLog('apk回编译失败')
  179. return ret
  180. http_utils.notify_cut_state(gcp_code, "90", "开始重签名,对齐APK")
  181. ret = apk_tool.signer(out_put_unsigned_apk_path, out_put_signed_apk_path, keystore_path, storepass, alias,
  182. keypass)
  183. if ret:
  184. PrintLog('apk签名失败')
  185. return ret
  186. ret = apk_tool.zipalign(out_put_signed_apk_path, out_put_zipalign_apk_path)
  187. if ret:
  188. PrintLog('aapk压缩失败')
  189. return ret
  190. dis_path = os.path.join(contants.get_out_put_dir(),
  191. "g%s_%s_%s_%s.apk" % (gid, sdk_name, package_name, gcp_code))
  192. if not os.path.exists(contants.get_out_put_dir()):
  193. os.makedirs(contants.get_out_put_dir())
  194. file_utils.copy_file(out_put_zipalign_apk_path, dis_path)
  195. http_utils.notify_cut_state(gcp_code, "100", "切包成功(点击复制链接)")
  196. time_c = time.time() - time_start
  197. PrintLog("APK路径:%s\n切包总用时:%s秒" % (dis_path, time_c))
  198. except:
  199. PrintLog(traceback.format_exc())
  200. finally:
  201. if apk_decompile_tmp_dir:
  202. PrintLog('清空打包临时目录')
  203. PrintLog('[apk_decompile_tmp_dir]:%s'%os.path.dirname(apk_decompile_tmp_dir))
  204. file_utils.delete_folder(os.path.dirname(apk_decompile_tmp_dir))
  205. def toBoolean(bool_str):
  206. if type(bool_str) == bool:
  207. return bool_str
  208. if bool_str == 'true':
  209. return True
  210. return False