merge_apk.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. # -*- coding:utf-8 -*-
  2. import os, json, shutil
  3. import xml.etree.ElementTree as ET
  4. import traceback
  5. import file_utils, contants, http_utils, text_utils, apk_tool, icon_utils, Replace
  6. import print_log
  7. from print_log import printlog
  8. import time
  9. def startMerge(config_json_path):
  10. global gcp_code
  11. isSuccess = False
  12. errorMsg = "切包失败,请联系开发人员"
  13. apk_decompile_tmp_dir = ""
  14. try:
  15. # 当前脚本路径
  16. # PrintLog "[current_py_path]: %s"%sys.argv[0]
  17. # py_dir = sys.path[0]
  18. # PrintLog "[py_dir] : %s"%py_dir
  19. # 更新svn库,1脚本文件;2sdk内容;3sdk_config;4tools;5doc etc
  20. # os.system("svn up " + py_dir + DIR_SPLIT + ".." + DIR_SPLIT)
  21. # 第一个参数json文件路径
  22. # config_json_path = sys.argv[1]
  23. # 包编号
  24. time_start = time.time()
  25. json_text = file_utils.read_file(config_json_path)
  26. config = json.loads(json_text)
  27. gcp_code = config["gcp_code"]
  28. print_log.LOGFILE = "%s/%s.txt" % (contants.sdk_log_path, gcp_code)
  29. if os.path.exists(print_log.LOGFILE):
  30. os.remove(print_log.LOGFILE)
  31. http_utils.notify_cut_state(gcp_code, "5", "正在读取配置")
  32. printlog("[LOGFILE] : %s" % print_log.LOGFILE)
  33. printlog("[gcp_code] : %s" % gcp_code)
  34. printlog("[config_json_path] : %s" % config_json_path)
  35. # apk文件的全路径(eg: /sdk/develop/tools/outputs/mxd/package/0.01.150608/360/360_0.01.150608_high_all.apk )
  36. origin_apk_full_path = config["apk_path"] # sys.argv[1]
  37. printlog("[origin_apk_full_path] : %s" % origin_apk_full_path)
  38. if not os.path.exists(origin_apk_full_path):
  39. errorMsg = "找不到游戏母包,请检查"
  40. return isSuccess
  41. # apk文件全名(eg: 360_0.01.150608_high_all.apk )
  42. origin_apk_full_name = os.path.basename(origin_apk_full_path)
  43. printlog("[origin_apk_full_name] : %s" % origin_apk_full_name)
  44. # apk文件名(eg: 360_0.01.150608_high_all)
  45. origin_apk_name = os.path.splitext(origin_apk_full_name)[0]
  46. printlog("[origin_apk_name] : %s" % origin_apk_name)
  47. # apk文件路径(eg: /sdk/develop/tools/outputs/mxd/package/0.01.150608/360 )
  48. origin_apk_dir = os.path.dirname(origin_apk_full_path)
  49. printlog("[origin_apk_dir] : %s" % origin_apk_dir)
  50. # 渠道名
  51. sdk_name = config["channel_key"] # sys.argv[2]
  52. printlog("[sdk_name] : %s" % sdk_name)
  53. # 包名
  54. package_name = config["package_name"] # sys.argv[3]
  55. printlog("[package_name] : %s" % package_name)
  56. result = text_utils.isMatchRegExp(package_name, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.")
  57. if not result:
  58. errorMsg = "包名包含特殊字符,不允许切包"
  59. return isSuccess
  60. # 游戏编号
  61. gid = config['gid']
  62. printlog("[gid] : %s" % gid)
  63. # 游戏c_id
  64. cid = config['cid']
  65. printlog("[cid] : %s" % cid)
  66. # 渠道文件的根目录
  67. sdk_root_dir = contants.channel_sdk_path
  68. printlog("[sdk_root_dir] : %s" % sdk_root_dir)
  69. # 打包渠道SDK的目录
  70. channel_sdk_root_dir = os.path.join(sdk_root_dir, sdk_name)
  71. printlog("[channel_sdk_root_dir] : %s" % channel_sdk_root_dir)
  72. if not os.path.exists(channel_sdk_root_dir):
  73. errorMsg = "该渠道尚未接入完成"
  74. return isSuccess
  75. # keystore配置文件
  76. keystore_path = config['keystore_path']
  77. printlog("[keystore_path] : %s" % keystore_path)
  78. if not os.path.exists(keystore_path):
  79. errorMsg = "找不到签名文件,请检查"
  80. return isSuccess
  81. storepass = config['storepass']
  82. printlog("[storepass] : %s" % storepass)
  83. keypass = config['keypass']
  84. printlog("[keypass] : %s" % keypass)
  85. alias = config['alias']
  86. printlog("[alias] : %s" % alias)
  87. # 反编译原apk文件的输出路径
  88. apk_decompile_out_dir = os.path.join(origin_apk_dir, origin_apk_name)
  89. printlog("[apk_decompile_out_dir] : %s" % apk_decompile_out_dir)
  90. # icon路径
  91. icon_path = config['icon']
  92. printlog("[icon_path]: %s" % icon_path)
  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, "10", "正在反编译文件")
  104. # 判断反编译出的文件修改时间与apk修改时间比较,若比apk最则重新反编译
  105. ret = file_utils.compareFileModifyTime(origin_apk_full_path, apk_decompile_out_dir)
  106. if ret is not None:
  107. printlog("母包未更新,无需重新反编译")
  108. else:
  109. apk_tool.decompile(origin_apk_full_path, apk_decompile_out_dir)
  110. http_utils.notify_cut_state(gcp_code, "20", "正在复制文件")
  111. randomNumber = text_utils.getRamdomNumber(6)
  112. apk_decompile_tmp_dir = os.path.join(origin_apk_dir, randomNumber, 'dcm_tmp', sdk_name)
  113. printlog("[apk_decompile_tmp_dir] : %s" % apk_decompile_tmp_dir)
  114. shutil.copytree(apk_decompile_out_dir, apk_decompile_tmp_dir)
  115. http_utils.notify_cut_state(gcp_code, "30", "正在合并资源")
  116. # compileResources(apk_decompile_tmp_dir, tmp_dir, package_name)
  117. file_utils.modifyPkgName(apk_decompile_tmp_dir, package_name)
  118. # 添加打点SDK
  119. printlog("-----开始添加打点SDK -----")
  120. Replace.add_point_smali(os.path.join(sdk_root_dir, 'data-point', 'data-point'), apk_decompile_tmp_dir)
  121. file_utils.replace_assets_param(
  122. os.path.join(apk_decompile_tmp_dir, 'assets', 'datapoint_config.properties'), 'DP_BIZ_GCP_CODE', gcp_code)
  123. temp_manifest_path = os.path.join(apk_decompile_tmp_dir, 'AndroidManifest.xml')
  124. metadatas = ET.parse(temp_manifest_path).getroot().find("application").findall("meta-data")
  125. for meta in metadatas:
  126. if meta.attrib["{http://schemas.android.com/apk/res/android}name"] == "yyrh_game_code":
  127. values = meta.attrib["{http://schemas.android.com/apk/res/android}value"]
  128. file_utils.replace_assets_param(os.path.join(apk_decompile_tmp_dir, 'assets',
  129. 'datapoint_config.properties'), 'DP_BIZ_GAME_CODE',
  130. values)
  131. printlog("-----打点SDK添加完成 -----")
  132. # 合并Manifest文件
  133. file_utils.mergeManifest(os.path.join(sdk_root_dir, sdk_name, sdk_name + '_sdk_config.xml'),
  134. temp_manifest_path)
  135. # 合并资源
  136. Replace.mergeResources(os.path.join(sdk_root_dir, sdk_name, sdk_name), apk_decompile_tmp_dir)
  137. # 生成R文件
  138. # Aapt_Util.createRFile(apk_decompile_tmp_dir,True, package_name)
  139. temp_yyrh_cnf_path = os.path.join(apk_decompile_tmp_dir, 'assets', 'YyrhParam.cnf')
  140. # 替换YyrhParam中的gcp_code
  141. printlog(temp_yyrh_cnf_path)
  142. file_utils.replace_assets_param(temp_yyrh_cnf_path, "GCP_CODE", gcp_code)
  143. # 替换Androidmanifest.xml中的yyrh_gameChannelID为gcp_code yyrh_game_channel_id
  144. file_utils.replace_manifest_meta_data(temp_manifest_path, "yyrh_gameChannelID",
  145. gcp_code)
  146. # 修改debuggable为false
  147. # Replace.replaceAM_Debuggable("%s/AndroidManifest.xml"%apk_decompile_out_dir)
  148. # 替换Icon
  149. http_utils.notify_cut_state(gcp_code, "40", "正在修改资源")
  150. # 替换Icon
  151. printlog("----- begin replace resource -----")
  152. if icon_path:
  153. icon_utils.replace_icon(apk_decompile_tmp_dir, icon_path)
  154. # 替换启动页
  155. splash_path = config['splash']
  156. if splash_path:
  157. if os.path.exists(splash_path):
  158. shutil.copy(splash_path, "%s/assets/yyrh_start_image.jpg" % apk_decompile_tmp_dir)
  159. else:
  160. errorMsg = "找不到闪屏文件,请检查"
  161. return isSuccess
  162. ########### #sdk 关键字
  163. http_utils.notify_cut_state(gcp_code, "50", "正在替换渠道参数")
  164. # 替换Androidmanifest.xml中的 package_name值为包名
  165. file_utils.replace_content(temp_manifest_path, 'package_name', package_name)
  166. sdk_client_config = config['sdk_client_config']
  167. printlog('[sdk_client_config] : %s' % sdk_client_config)
  168. if sdk_client_config:
  169. keys = sdk_client_config.keys()
  170. for key in keys:
  171. # 替换AndroidManifest.xml中的meta-data字段
  172. file_utils.replace_manifest_meta_data(temp_manifest_path, key,
  173. sdk_client_config[key])
  174. # 替换assets/YyrhParam.cnf中的关键字
  175. file_utils.replace_assets_param(temp_yyrh_cnf_path, key,
  176. sdk_client_config[key])
  177. # 特殊渠道需要特殊处理
  178. Replace.special_replace(sdk_name, apk_decompile_tmp_dir, key, sdk_client_config[key])
  179. ########## #game 关键字
  180. printlog("meta_config:%s" % meta_config)
  181. if meta_config:
  182. keys = meta_config.keys()
  183. for key in keys:
  184. if key == "APP_NAME":
  185. file_utils.replace_string_app_name(apk_decompile_tmp_dir, meta_config[key])
  186. elif key == "version":
  187. version = meta_config[key]
  188. elif key == "v_code":
  189. v_code = meta_config[key]
  190. elif key == "targetSdkVersion":
  191. targetSdkVersion = meta_config[key]
  192. # 替换方向
  193. elif key == "SDK_ORIENTATION":
  194. file_utils.replace_content(temp_manifest_path, 'sdk_orientation', meta_config[key])
  195. file_utils.replace_assets_param(temp_yyrh_cnf_path, "SDK_ORIENTATION", meta_config[key])
  196. # 修改版本号
  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.change_min_sdk_version(yml, 21)
  201. file_utils.merge_wx_page(package_name, apk_decompile_tmp_dir)
  202. http_utils.notify_cut_state(gcp_code, "70", "正在替换游戏资源")
  203. game_resource_replace = config['game_resource_replace']
  204. printlog('[game_resource_replace]:%s' % game_resource_replace)
  205. if game_resource_replace:
  206. for resource in game_resource_replace:
  207. if os.path.exists(resource[1]):
  208. file_utils.copy_file(resource[1], os.path.join(apk_decompile_tmp_dir, resource[0]), False)
  209. out_put_unsigned_apk_path = os.path.join(apk_decompile_tmp_dir, 'gen', '_unsigned.apk')
  210. out_put_signed_apk_path = os.path.join(apk_decompile_tmp_dir, 'gen', '_signed.apk')
  211. out_put_zipalign_apk_path = os.path.join(apk_decompile_tmp_dir, 'gen', '_zipaligned.apk')
  212. # 再编译
  213. http_utils.notify_cut_state(gcp_code, "80", "正在回编译APK")
  214. if apk_tool.recompile(apk_decompile_tmp_dir, out_put_unsigned_apk_path):
  215. errorMsg = 'apk回编译失败'
  216. return isSuccess
  217. http_utils.notify_cut_state(gcp_code, "90", "开始重签名,对齐APK")
  218. if apk_tool.V1signer(out_put_unsigned_apk_path, out_put_signed_apk_path, keystore_path, storepass, alias,
  219. keypass):
  220. errorMsg = 'apk回编译失败'
  221. return isSuccess
  222. printlog("开始执行apk压缩")
  223. if apk_tool.zipalign(out_put_signed_apk_path, out_put_zipalign_apk_path):
  224. errorMsg = 'apk压缩失败'
  225. return isSuccess
  226. dis_path = os.path.join(contants.out_put_dir,
  227. "g%s_%s_%s_%s.apk" % (gid, sdk_name, package_name, gcp_code))
  228. apkDstDir = os.path.dirname(dis_path)
  229. if not os.path.exists(apkDstDir):
  230. os.makedirs(apkDstDir)
  231. shutil.copyfile(out_put_zipalign_apk_path, dis_path)
  232. time_end = time.time()
  233. time_c = time_end - time_start
  234. printlog("APK路径:%s" % dis_path)
  235. printlog("切包总用时:%s秒" % time_c)
  236. isSuccess = True
  237. except Exception as err:
  238. printlog(errorMsg)
  239. printlog("cut error occur:%s" % err)
  240. printlog(traceback.format_exc())
  241. finally:
  242. if isSuccess:
  243. http_utils.notify_cut_state(gcp_code, "100", "切包成功(点击复制链接)")
  244. else:
  245. http_utils.notify_cut_state(gcp_code, "99", errorMsg)
  246. if apk_decompile_tmp_dir:
  247. printlog('清空打包临时目录')
  248. printlog('[apk_decompile_tmp_dir]:%s' % os.path.dirname(apk_decompile_tmp_dir))
  249. file_utils.delete_folder(os.path.dirname(apk_decompile_tmp_dir))
  250. return isSuccess
  251. if __name__ == "__main__":
  252. pass