merge_ad_pkg.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. # -*- coding:utf-8 -*-
  2. import sys
  3. import os
  4. import xml.etree.ElementTree as ET
  5. import print_log
  6. from print_log import printlog
  7. import shutil
  8. import json
  9. import traceback
  10. import time
  11. import file_utils, Replace, contants, http_utils, icon_utils, apk_tool, text_utils
  12. keystorepath = "/opt/packing_tool/keystore/yyrh.jks"
  13. storepass = "yyrh123456"
  14. keypass = "yyrh123456"
  15. alias = "yyrh"
  16. def startMerge(config_json_path):
  17. global ad_code_list, sp_code, apk_decompile_tmp_dir
  18. isSuccess = False
  19. errorMsg = "切包失败,请联系开发人员"
  20. time_start = time.time()
  21. try:
  22. json_text = file_utils.read_file(config_json_path)
  23. config = json.loads(json_text)
  24. sp_code = config['sp_code']
  25. print_log.LOGFILE = "%s/%s.txt" % (contants.sdk_log_path, sp_code)
  26. if os.path.exists(print_log.LOGFILE):
  27. os.remove(print_log.LOGFILE)
  28. printlog("[LOGFILE] : %s" % print_log.LOGFILE)
  29. printlog("[sp_code] : %s" % sp_code)
  30. origin_apk_full_path = config['sub_apk_path']
  31. printlog("[origin_apk_full_path] : %s" % origin_apk_full_path)
  32. origin_apk_dir = os.path.dirname(origin_apk_full_path)
  33. origin_apk_full_name = os.path.basename(origin_apk_full_path)
  34. printlog("[origin_apk_dir] %s" % origin_apk_dir)
  35. randomNumber = text_utils.getRamdomNumber(6)
  36. origin_apk_name = os.path.splitext(origin_apk_full_name)[0]
  37. apk_decompile_tmp_dir = os.path.join(origin_apk_dir, randomNumber, 'dcm_tmp', origin_apk_name)
  38. ad_sdk_type = config['ad_sdk_type']
  39. printlog("[ad_sdk_type] : %s" % ad_sdk_type)
  40. ad_allow_pt = config['ad_allow_pt']
  41. printlog("[ad_allow_pt] : %s" % ad_allow_pt)
  42. if config.get('ad_code_list'):
  43. ad_code_list = json.dumps(config['ad_code_list'])
  44. else:
  45. ad_code_list = ''
  46. printlog("[ad_code_list] : %s" % ad_code_list)
  47. sdk_code = config['sdk_code']
  48. printlog("[sdk_code] : %s" % sdk_code)
  49. if config.get('channel_map_id'):
  50. channel_map_id = config['channel_map_id']
  51. else:
  52. channel_map_id = ''
  53. printlog("[channel_map_id] : %s" % channel_map_id)
  54. # 包名
  55. if config.get('package_name'):
  56. package_name = config['package_name']
  57. else:
  58. package_name = ''
  59. printlog("[package_name] : %s" % package_name)
  60. if config.get('meta_config_value'):
  61. meta_config_value = config['meta_config_value']
  62. else:
  63. meta_config_value = config.get('meta_config_value', '')
  64. printlog("meta_config_value:%s" % str(meta_config_value))
  65. # 版本号
  66. version = ''
  67. v_code = ''
  68. targetSdkVersion = ''
  69. # icon路径
  70. icon_path = config.get('icon_path', '')
  71. printlog("[icon_path]: %s" % icon_path)
  72. unsignedApkPath = "%s_unsigned.apk" % apk_decompile_tmp_dir
  73. printlog("unsignedApkPath: %s" % unsignedApkPath)
  74. signedApkPath = "%s_signed.apk" % apk_decompile_tmp_dir
  75. printlog("signedApkPath: %s" % signedApkPath)
  76. zipalignApkPath = "%s_zipaligned.apk" % apk_decompile_tmp_dir
  77. printlog("zipalignApkPath: %s" % zipalignApkPath)
  78. http_utils.notify_sp_cut_state(sp_code, "10", "正在反编译文件", ad_code_list)
  79. printlog("----- begin decompile -----")
  80. ret = apk_tool.decompile(origin_apk_full_path, apk_decompile_tmp_dir)
  81. if ret == 1:
  82. errorMsg = "找不到游戏母包,请检查"
  83. printlog(errorMsg)
  84. http_utils.notify_sp_cut_state(sp_code, "99", errorMsg, ad_code_list)
  85. return
  86. printlog("----- begin replace params -----")
  87. temp_xml_path = os.path.join(apk_decompile_tmp_dir, 'AndroidManifest.xml')
  88. printlog("temp_xml_path %s" % temp_xml_path)
  89. http_utils.notify_sp_cut_state(sp_code, "30", "正在修改SP_CODE", ad_code_list)
  90. if ad_allow_pt != 0:
  91. add_andriod_node(temp_xml_path, "yyrh_sp_code", sp_code)
  92. http_utils.notify_sp_cut_state(sp_code, "40", "正在修改合并渠道", ad_code_list)
  93. if ad_sdk_type == 2:
  94. sdk_root_dir = contants.ad_sdk_path
  95. file_utils.mergeManifest(os.path.join(sdk_root_dir, sdk_code, sdk_code + '_sdk_config.xml'),
  96. temp_xml_path)
  97. Replace.mergeResources(os.path.join(sdk_root_dir, sdk_code, sdk_code), apk_decompile_tmp_dir)
  98. sdk_param_conf = config['sdk_param_conf']
  99. printlog("channel sdk_param_conf: %s" % str(sdk_param_conf))
  100. # 替换Androidmanifest.xml中的 package_name值为包名
  101. file_utils.replace_content(temp_xml_path, 'package_name', package_name)
  102. if sdk_param_conf:
  103. keys = sdk_param_conf.keys()
  104. for key in keys:
  105. # 替换AndroidManifest.xml中的meta-data字段
  106. file_utils.replace_manifest_meta_data(temp_xml_path, key,
  107. sdk_param_conf[key])
  108. # 替换assets/YyrhAdParam.cnf中的关键字
  109. file_utils.replace_assets_param(
  110. os.path.join(apk_decompile_tmp_dir, 'assets', 'YyrhAdParam.cnf'), key,
  111. sdk_param_conf[key])
  112. # 特殊渠道需要特殊处理
  113. # Replace.special_replace(sdk_name, apk_decompile_tmp_dir, DIR_SPLIT, key, sdk_param_conf[key])
  114. pass
  115. # 修改包Apk文件名
  116. # 修改包名
  117. file_utils.modifyPkgName(apk_decompile_tmp_dir, package_name)
  118. # 替换Icon
  119. printlog("----- begin replace resource -----")
  120. http_utils.notify_sp_cut_state(sp_code, "50", "正在替换资源", ad_code_list)
  121. if icon_path:
  122. icon_utils.replace_icon(apk_decompile_tmp_dir, icon_path)
  123. # 替换启动页
  124. splash_path = config.get('splash_path', '')
  125. printlog("splash_path:%s" % splash_path)
  126. if splash_path:
  127. if os.path.exists(splash_path):
  128. shutil.copy(splash_path, "%s/assets/yyrh_start_image.jpg" % apk_decompile_tmp_dir)
  129. else:
  130. errorMsg = "找不到闪屏文件,请检查"
  131. printlog(errorMsg)
  132. http_utils.notify_sp_cut_state(sp_code, "99", errorMsg, ad_code_list)
  133. return
  134. # 获取游戏名,版本号,版本名,targetSdkVersion,屏幕方向关键字
  135. if meta_config_value:
  136. keys = meta_config_value.keys()
  137. for key in keys:
  138. if key == "APP_NAME":
  139. file_utils.replace_string_app_name(apk_decompile_tmp_dir, meta_config_value[key])
  140. elif key == "version":
  141. version = meta_config_value[key]
  142. elif key == "v_code":
  143. v_code = meta_config_value[key]
  144. elif key == "targetSdkVersion":
  145. targetSdkVersion = meta_config_value[key]
  146. pass
  147. # 修改版本号 targetSdkVersion
  148. http_utils.notify_sp_cut_state(sp_code, "60", "正在修改版本号", ad_code_list)
  149. yml = os.path.join(apk_decompile_tmp_dir, 'apktool.yml')
  150. file_utils.changeVersion(yml, v_code, version, targetSdkVersion)
  151. file_utils.change_min_sdk_version(yml, 21)
  152. http_utils.notify_sp_cut_state(sp_code, "70", "正在替换游戏资源", ad_code_list)
  153. # 替换游戏资源
  154. if config.get('game_resource_replace_path'):
  155. game_resource_replace_path = config['game_resource_replace_path']
  156. printlog('[game_resource_replace_path]:%s' % game_resource_replace_path)
  157. if game_resource_replace_path:
  158. for resource in game_resource_replace_path:
  159. if os.path.exists(resource[1]):
  160. file_utils.copy_file(resource[1], os.path.join(apk_decompile_tmp_dir, resource[0]), False)
  161. printlog("----- begin recompile apk -----")
  162. http_utils.notify_sp_cut_state(sp_code, "80", "正在回编译APK", ad_code_list)
  163. ret = apk_tool.recompile(apk_decompile_tmp_dir, unsignedApkPath)
  164. if ret:
  165. printlog('apk回编译失败')
  166. return ret
  167. printlog("unsignedApkPath: %s" % unsignedApkPath)
  168. printlog("----- begin resign apk -----")
  169. http_utils.notify_sp_cut_state(sp_code, "90", "正在重签名,对齐APK", ad_code_list)
  170. ret = apk_tool.V1signer(unsignedApkPath, signedApkPath, keystorepath, storepass, alias,
  171. keypass)
  172. if ret:
  173. printlog('apk签名失败')
  174. return ret
  175. printlog("signedApkPath %s" % signedApkPath)
  176. printlog("----- begin zipalign apk -----")
  177. ret = apk_tool.zipalign(signedApkPath, zipalignApkPath)
  178. if ret:
  179. printlog('apk压缩失败')
  180. return ret
  181. printlog("zipalignApkPath %s" % zipalignApkPath)
  182. dis_path = os.path.join(contants.sp_out_put_dir, "%s_%s.apk" % (origin_apk_name, sp_code))
  183. printlog("origin_apk_name %s" % origin_apk_name)
  184. if ad_allow_pt == 0 and ad_sdk_type == 2:
  185. dis_path = os.path.join(contants.sp_out_put_dir,
  186. "%s_%s_%s.apk" % (origin_apk_name, sdk_code, channel_map_id))
  187. if ad_allow_pt == 0 and ad_sdk_type == 1:
  188. dis_path = os.path.join(contants.sp_out_put_dir,
  189. "%s_%s_%s.apk" % (origin_apk_name, sdk_code, channel_map_id))
  190. dst_dir = os.path.dirname(dis_path)
  191. if not os.path.exists(dst_dir):
  192. os.makedirs(dst_dir)
  193. printlog("apkDstPath %s" % dis_path)
  194. shutil.copyfile(zipalignApkPath, dis_path)
  195. time_end = time.time()
  196. time_c = time_end - time_start
  197. printlog("切包总用时:%s秒" % time_c)
  198. http_utils.notify_sp_cut_state(sp_code, "100", "切包成功(点击复制链接)", ad_code_list)
  199. isSuccess = True
  200. except Exception as err:
  201. http_utils.notify_sp_cut_state(sp_code, "99", errorMsg, ad_code_list)
  202. printlog("cut error occur:%s" % err)
  203. printlog(traceback.format_exc())
  204. finally:
  205. if apk_decompile_tmp_dir:
  206. Replace.safeFileDelete(os.path.dirname(apk_decompile_tmp_dir))
  207. return isSuccess
  208. def add_andriod_node(xmlpath, key, value):
  209. global child
  210. ET.register_namespace('android', 'http://schemas.android.com/apk/res/android')
  211. tree = ET.parse(xmlpath)
  212. root = tree.getroot()
  213. node = ET.Element("meta-data")
  214. node.set("android:name", key)
  215. node.set("android:value", value)
  216. node.tail = "\n\t"
  217. for i in root.iter("application"):
  218. for child in i:
  219. pass
  220. child.tail = '\n\t\t'
  221. for a in root.iter("application"):
  222. a.append(node)
  223. break
  224. tree.write(xmlpath, encoding="utf-8", xml_declaration=True)
  225. pass