merge_apk.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. # -*- coding:utf-8 -*-
  2. import os
  3. import platform
  4. import shutil
  5. import xml.etree.ElementTree as ET
  6. import traceback
  7. import yaml
  8. import random
  9. from V1 import Contants, Exec_Cmd_Utils, HttpUtils, Replace_GameRes_Utils, PrintLog, CommonUtils, Json_Util, File, \
  10. Replace, apk_utils, Icon_Util
  11. from V1.PrintLog import printlog
  12. import time
  13. SUFFIX_BAT = ""
  14. SUFFIX_EXE = ""
  15. DIR_SPLIT = "/"
  16. # Windows下使用.bat,unix无后缀
  17. if platform.system() == "Windows":
  18. SUFFIX_BAT = ".bat"
  19. SUFFIX_EXE = ".exe"
  20. DIR_SPLIT = "\\"
  21. # decompile apk
  22. def decompile(apkPath, desPath):
  23. printlog("start to apkPath -- %s" % apkPath)
  24. global SUFFIX_BAT
  25. if os.path.exists(apkPath) == False:
  26. printlog("no such apk:%s" % apkPath)
  27. return 1
  28. home = apk_utils.getComplieToolsHome()
  29. decompileCmd = "%s/apktool%s d -f -o %s/ %s" % (home, SUFFIX_BAT, desPath, apkPath)
  30. printlog("decompileCmd:%s" % decompileCmd)
  31. ret = os.system(decompileCmd)
  32. printlog("finished decompiling -------------------------")
  33. return ret
  34. # recompile fileset to apk
  35. def recompile(apkPath, outputApkPath):
  36. global SUFFIX_BAT
  37. printlog("------------------------------ start to recompile ------------------------------ ")
  38. global SUFFIX_BAT
  39. recompileCmd = "%s/apktool%s -q b %s -o %s" % (apk_utils.getComplieToolsHome(), SUFFIX_BAT, apkPath, outputApkPath)
  40. status, output = Exec_Cmd_Utils.exeCommonCmd(recompileCmd)
  41. printlog(output)
  42. if status != 0:
  43. return 1
  44. printlog("------------------------------ finished recompiling ------------------------------ ")
  45. return 0
  46. def mergeYml(fromfile, tofile):
  47. if not os.path.exists(fromfile):
  48. return False, "origin yaml file is not exists"
  49. if not os.path.exists(tofile):
  50. return False, "destination yaml file is not exists"
  51. header = "!!brut.androlib.meta.MetaInfo\n"
  52. fromcontent = open(fromfile, "r").read()
  53. fromcontent = fromcontent.replace(header, "")
  54. tocontent = open(tofile, "r").read()
  55. tocontent = tocontent.replace(header, "")
  56. fromroot = yaml.safe_load(fromcontent)
  57. toroot = yaml.safe_load(tocontent)
  58. if fromroot == None:
  59. printlog("from root is empty")
  60. return False, "orgin yaml file cant not be parsed or empty"
  61. if toroot == None:
  62. printlog("to root is empty")
  63. return False, "destination yaml file cant not be parsed or is empty"
  64. donotcompresskey = "doNotCompress"
  65. if donotcompresskey in fromroot:
  66. arr = fromroot[donotcompresskey]
  67. if not donotcompresskey in toroot:
  68. toroot[donotcompresskey] = arr
  69. else:
  70. toarr = toroot[donotcompresskey]
  71. toroot[donotcompresskey] = list(set(toarr + arr))
  72. unknownfileskey = "unknownFiles"
  73. if unknownfileskey in fromroot:
  74. fromunknownfilesarr = fromroot[unknownfileskey]
  75. else:
  76. fromunknownfilesarr = {}
  77. tounknownfilesarr = toroot[unknownfileskey]
  78. toroot[unknownfileskey] = dict(fromunknownfilesarr.items()).update(tounknownfilesarr.items())
  79. desfile = open(tofile, "w")
  80. desfile.writelines(header)
  81. yaml.safe_dump(toroot, desfile, default_flow_style=False)
  82. return True, ""
  83. pass
  84. # sdk resources + apk resources
  85. def mergeResources(sdkPath, apkPath):
  86. printlog("--------------------------- start to merge resources ---------------------------")
  87. ret = 0
  88. # merge res
  89. # sdk_root_dir = apk_utils.getCutSdkRootHome() + "/" + os.path.basename(sdkPath)
  90. ret = ret | File.copyFiles("%s/res" % sdkPath, "%s/res" % apkPath)
  91. # copy lib and assets
  92. # 避免由于架构导致复制无用架构
  93. ret = ret | mergeLibs(sdkPath, apkPath)
  94. ret = ret | File.copyFiles("%s/assets" % sdkPath, "%s/assets" % apkPath)
  95. ret = ret | File.mergeDir("%s/smali" % sdkPath, "%s/smali" % apkPath)
  96. ret = ret | File.mergeDir("%s/smali_classes2" % sdkPath, "%s/smali_classes2" % apkPath)
  97. ret = ret | File.mergeDir("%s/smali_classes3" % sdkPath, "%s/smali_classes3" % apkPath)
  98. ret = ret | File.mergeDir("%s/smali_classes4" % sdkPath, "%s/smali_classes4" % apkPath)
  99. ret = ret | File.mergeDir("%s/smali_classes5" % sdkPath, "%s/smali_classes5" % apkPath)
  100. ret = ret | File.mergeDir("%s/smali_classes6" % sdkPath, "%s/smali_classes6" % apkPath)
  101. ret = ret | File.mergeDir("%s/unknown" % sdkPath, "%s/unknown" % apkPath)
  102. # 暂时的方案是用sdk的apktool.yml覆盖原包apktool.yml 后期需要做apktool.yml合并
  103. if os.path.exists("%s/apktool.yml" % sdkPath):
  104. mergeYml("%s/apktool.yml" % sdkPath, "%s/apktool.yml" % apkPath)
  105. # shutil.copy("%s/apktool.yml"%sdkPath, "%s/apktool.yml"%apkPath)
  106. # 修改微信支付
  107. # ret = ret | File.copyFiles("%s/smali"%sdkPath,"%s/smali"%apkPath)
  108. printlog("--------------------------- finished merging resources ---------------------------")
  109. return ret
  110. # add 合并lib文件
  111. def mergeLibs(sdkPath, apkPath):
  112. printlog("--------------------------- start to merge lib --------------------------------")
  113. apkLib = "%s/lib" % apkPath
  114. sdkLib = "%s/lib" % sdkPath
  115. ret = 0
  116. if not os.path.exists(apkLib) and os.path.exists(sdkLib):
  117. shutil.copytree(sdkLib, apkLib)
  118. printlog("finished merging lib --------------------------------")
  119. return ret
  120. apkFiles = os.listdir(apkLib)
  121. sdkFiles = os.listdir(sdkLib)
  122. for cpFile in sdkFiles:
  123. if os.path.isfile(cpFile) == False and cpFile not in apkFiles:
  124. continue
  125. ret = ret | File.copyFiles("%s/lib/%s" % (sdkPath, cpFile), "%s/lib/%s" % (apkPath, cpFile))
  126. printlog("--------------------------- finished merging lib --------------------------------")
  127. return ret
  128. # 修改微信支付
  129. def mergeWXPay(sdkPath, apkPath):
  130. printlog("--------------------------- start merge wechat pay ---------------------------")
  131. sdkAM = "%s/AndroidManifest.xml" % sdkPath
  132. sdkAMRoot = ET.parse(sdkAM).getroot()
  133. sdkPkgName = sdkAMRoot.attrib["package"].replace(".", "/")
  134. sdkWxApiFilePath = "%s/smali/%s/wxapi" % (sdkPath, sdkPkgName)
  135. printlog("wx api file path:%s" % sdkWxApiFilePath)
  136. if os.path.exists(sdkWxApiFilePath):
  137. printlog("begin copy wxapi file")
  138. apkWxApi = ET.parse("%s/AndroidManifest.xml" % apkPath).getroot().attrib["package"].replace(".", "/")
  139. apkWxApiFilePath = "%s/smali/%s/wxapi" % (apkPath, apkWxApi)
  140. printlog("wxapi path:%s" % apkWxApiFilePath)
  141. if not os.path.exists(apkWxApiFilePath):
  142. os.makedirs(apkWxApiFilePath)
  143. # shutil.copy("%s/*"%wxApiFilePath, apkWxApiFilePath)
  144. os.system("/bin/cp -r %s/* %s" % (sdkWxApiFilePath, apkWxApiFilePath))
  145. for parent, dirnames, filenames in os.walk(apkWxApiFilePath):
  146. printlog("replace pgk ---------")
  147. for filename in filenames:
  148. printlog("start to replace: %s" % filename)
  149. formatFname = "'%s'" % filename
  150. replacePkgShell = 'sed -i "s#%s#%s#g" %s/%s' % (sdkPkgName, apkWxApi, apkWxApiFilePath, formatFname)
  151. printlog("replace package name shell: %s" % replacePkgShell)
  152. os.system(replacePkgShell)
  153. printlog("end copy wxapi file -------------------------------")
  154. pass
  155. # sdkConfig.xml + AndroidManifest.xml
  156. # sdkConfigPath: 渠道配置路径
  157. # apkPath:解包后AndroidManifest.xml路径
  158. def mergeManifest(sdkConfigPath, apkPath):
  159. printlog("start to merge AndroidManifest.xml -------------------------")
  160. ret = 0
  161. ret = ret | File.mergeManifest("%s/AndroidManifest.xml" % apkPath, sdkConfigPath)
  162. # 根据配置修改游戏名,包名等
  163. printlog("--------------------------- finished merging AndroidManifest.xml -------------------------")
  164. return ret
  165. def replacePkgName(manifestPath, pkgName):
  166. if not os.path.exists(manifestPath):
  167. return False, "manifest path is not exists"
  168. if pkgName == None or pkgName == "":
  169. return False, "package name is empty"
  170. pkgName = pkgName.strip()
  171. root = ET.parse(manifestPath).getroot()
  172. if root == None:
  173. return False, "can's parse manifest"
  174. oldpkgname = root.attrib["package"]
  175. lines = open(manifestPath, "r").readlines()
  176. f = open(manifestPath, "w")
  177. for line in lines:
  178. line = line.replace(oldpkgname, pkgName)
  179. f.write(line)
  180. f.close()
  181. return True, ""
  182. pass
  183. # @Description -- 对apk进行重新签名.若未提供对齐后的路径或名字,则以当前的命令在.apk前添加_signed
  184. # unsignedApkPath: 未签名的包
  185. # keyConfig: 签名配置(暂时未使用)
  186. # outputPath: 签名完成后的包路径
  187. def resignApk(unsignedApkPath, keystorepath, storepass, alias, keypass, outputPath=""):
  188. printlog("start to resign apk -----------------------")
  189. if outputPath == "":
  190. point = unsignedApkPath.rfind(".")
  191. outputPath = unsignedApkPath[0:point] + "_signed" + unsignedApkPath[point:]
  192. printlog("signedName:%s" % outputPath)
  193. # 获取签名算法
  194. getKeystoreAlgorithmShell = "%s/keytool -list -v -keystore %s -alias %s -storepass %s | /bin/sed -n \"13p\" | /usr/bin/awk -F ': ' '{print $2}'" % (
  195. apk_utils.getJavaBinPath(), keystorepath, alias, storepass)
  196. ret, algorithm = Exec_Cmd_Utils.exeCommonCmd(getKeystoreAlgorithmShell)
  197. algorithmMethod = algorithm.strip()
  198. printlog("algorithmMethod: %s" % algorithmMethod)
  199. resignCmd = "%s/jarsigner -sigalg %s -digestalg SHA1 -storepass %s -keypass %s -keystore %s -signedjar %s %s %s" % (
  200. apk_utils.getJavaBinPath(), algorithmMethod, storepass, keypass, keystorepath, outputPath, unsignedApkPath,
  201. alias)
  202. status, output = Exec_Cmd_Utils.exeCommonCmd(resignCmd)
  203. if status != 0:
  204. return 1
  205. printlog("--------------------------- finished resigning apk --------------------------")
  206. return 0
  207. # @Description -- 对apk进行V2重新签名.若未提供对齐后的路径或名字,则以当前的命令在.apk前添加_signed
  208. # unsignedApkPath: 未签名已对齐的包
  209. # keyConfig: 签名配置(暂时未使用)
  210. # outputPath: 签名完成后的包路径
  211. def resignV2Apk(unsignedApkPath, keystorepath, storepass, alias, keypass, outputPath=""):
  212. printlog("--------------------------- start to resign apk by V2 -----------------------")
  213. if outputPath == "":
  214. point = unsignedApkPath.rfind(".")
  215. outputPath = unsignedApkPath[0:point] + "_signed" + unsignedApkPath[point:]
  216. printlog("signedName:%s" % outputPath)
  217. resignCmd = "/usr/bin/java -jar %s sign --ks %s --ks-key-alias %s --ks-pass pass:%s --key-pass pass:%s --out %s %s" % (
  218. apk_utils.getSignV2Jar(), keystorepath, alias, storepass, keypass, outputPath, unsignedApkPath)
  219. printlog(resignCmd)
  220. status, output = Exec_Cmd_Utils.exeCommonCmd(resignCmd)
  221. if status != 0:
  222. return 1
  223. printlog("--------------------------- finished resigning V2 apk --------------------------")
  224. return 0
  225. # @Description -- 把重新签名的包做4字节对齐,若未提供对齐后的路径或名字,则以当前的命令在.apk前添加_zipaligned
  226. # unZipalignApk: 未对齐的apk路径
  227. # outputPath: 对齐后的文件名
  228. # @return 0为成功 1为失败
  229. def zipalignApk(unZipalignApk, outputPath=""):
  230. if outputPath == "":
  231. point = unZipalignApk.rfind(".")
  232. outputPath = unZipalignApk[0:point] + "_zipaligned" + unZipalignApk[point:]
  233. printlog("outputPath:%s" % outputPath)
  234. zipalignCmd = "%s/zipalign -f 4 %s %s" % (apk_utils.getSdkToolsPath(), unZipalignApk, outputPath)
  235. printlog("zipalignCmd:%s" % zipalignCmd)
  236. status, output = Exec_Cmd_Utils.exeCommonCmd(zipalignCmd)
  237. printlog(output)
  238. if status != 0:
  239. return 1
  240. printlog("--------------------------- finished zipaligning apk -----------------------------")
  241. return 0
  242. def getRamdomNumber(size):
  243. str = ""
  244. for i in range(size):
  245. ch = chr(random.randrange(ord('0'), ord('9') + 1))
  246. str += ch
  247. return str
  248. def modifyPkgName(apkPath, pkgName):
  249. # 修改包名
  250. manifestRoot = ET.parse("%s/AndroidManifest.xml" % apkPath).getroot()
  251. if manifestRoot == None:
  252. return 1
  253. if pkgName == "":
  254. pkgName = manifestRoot.attrib["package"]
  255. printlog("get pkg name:%s ----------------------------" % pkgName)
  256. else:
  257. oldPkgName = manifestRoot.attrib["package"]
  258. content = open("%s/AndroidManifest.xml" % apkPath, "r").read()
  259. content = content.replace(oldPkgName, pkgName)
  260. open("%s/AndroidManifest.xml" % apkPath, "w").write(content)
  261. printlog("old pkg name:%s ----------------------------" % oldPkgName)
  262. printlog("new pkg name:%s ----------------------------" % pkgName)
  263. def getOldPkgName(apkPath):
  264. # 修改包名
  265. manifestRoot = ET.parse("%s/AndroidManifest.xml" % apkPath).getroot()
  266. if manifestRoot == None:
  267. return ""
  268. else:
  269. oldPkgName = manifestRoot.attrib["package"]
  270. return oldPkgName
  271. def startMerge(config_json_path):
  272. global gcp_code
  273. isSuccess = True
  274. errorMsg = "切包失败,请联系开发人员"
  275. apk_decompile_tmp_dir = ""
  276. try:
  277. # 当前脚本路径
  278. # PrintLog "[current_py_path]: %s"%sys.argv[0]
  279. # py_dir = sys.path[0]
  280. # PrintLog "[py_dir] : %s"%py_dir
  281. # 更新svn库,1脚本文件;2sdk内容;3sdk_config;4tools;5doc etc
  282. # os.system("svn up " + py_dir + DIR_SPLIT + ".." + DIR_SPLIT)
  283. # 第一个参数json文件路径
  284. # config_json_path = sys.argv[1]
  285. # 包编号
  286. time_start = time.time()
  287. gcp_code = Json_Util.ReadJson(config_json_path, "gcp_code")
  288. PrintLog.LOGFILE = "%s/%s.txt" % (Contants.SDK_LOG, gcp_code)
  289. if os.path.exists(PrintLog.LOGFILE):
  290. os.remove(PrintLog.LOGFILE)
  291. HttpUtils.notify_cut_state(gcp_code, "5", "正在读取配置")
  292. printlog("[LOGFILE] : %s" % PrintLog.LOGFILE)
  293. printlog("[gcp_code] : %s" % gcp_code)
  294. printlog("[config_json_path] : %s" % config_json_path)
  295. # apk文件的全路径(eg: /sdk/develop/tools/outputs/mxd/package/0.01.150608/360/360_0.01.150608_high_all.apk )
  296. origin_apk_full_path = Json_Util.ReadJson(config_json_path, "apk_path") # sys.argv[1]
  297. printlog("[origin_apk_full_path] : %s" % origin_apk_full_path)
  298. if not os.path.exists(origin_apk_full_path):
  299. errorMsg = "找不到游戏母包,请检查"
  300. printlog(errorMsg)
  301. HttpUtils.notify_cut_state(gcp_code, "99", errorMsg)
  302. return
  303. # apk文件全名(eg: 360_0.01.150608_high_all.apk )
  304. origin_apk_full_name = os.path.basename(origin_apk_full_path)
  305. printlog("[origin_apk_full_name] : %s" % origin_apk_full_name)
  306. # apk文件名(eg: 360_0.01.150608_high_all)
  307. origin_apk_name = os.path.splitext(origin_apk_full_name)[0]
  308. printlog("[origin_apk_name] : %s" % origin_apk_name)
  309. # apk文件路径(eg: /sdk/develop/tools/outputs/mxd/package/0.01.150608/360 )
  310. origin_apk_dir = os.path.dirname(origin_apk_full_path)
  311. printlog("[origin_apk_dir] : %s" % origin_apk_dir)
  312. # 渠道名
  313. sdk_name = Json_Util.ReadJson(config_json_path, "channel_key") # sys.argv[2]
  314. printlog("[sdk_name] : %s" % sdk_name)
  315. # 包名
  316. package_name = Json_Util.ReadJson(config_json_path, "package_name") # sys.argv[3]
  317. printlog("[package_name] : %s" % package_name)
  318. result = CommonUtils.isMatchRegExp(package_name, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.")
  319. if result == False:
  320. errorMsg = "包名包含特殊字符,不允许切包"
  321. printlog(errorMsg)
  322. HttpUtils.notify_cut_state(gcp_code, "99", errorMsg)
  323. return
  324. # 游戏编号
  325. g_id = Json_Util.ReadJson(config_json_path, "gid")
  326. printlog("[gid] : %s" % g_id)
  327. # 游戏c_id
  328. c_id = Json_Util.ReadJson(config_json_path, "cid")
  329. printlog("[cid] : %s" % c_id)
  330. # 渠道文件的根目录
  331. sdk_root_dir = apk_utils.getCutSdkRootHome()
  332. printlog("[sdk_root_dir] : %s" % sdk_root_dir)
  333. # 打包渠道SDK的目录
  334. channel_sdk_root_dir = sdk_root_dir + DIR_SPLIT + sdk_name
  335. printlog("[channel_sdk_root_dir] : %s" % channel_sdk_root_dir)
  336. if not os.path.exists(channel_sdk_root_dir):
  337. errorMsg = "该渠道尚未接入完成"
  338. printlog(errorMsg)
  339. HttpUtils.notify_cut_state(gcp_code, "99", errorMsg)
  340. return
  341. # keystore配置文件
  342. keystorepath = Json_Util.ReadJson(config_json_path, "keystore_path")
  343. printlog("[keystorepath] : %s" % keystorepath)
  344. if not os.path.exists(keystorepath):
  345. errorMsg = "找不到签名文件,请检查"
  346. printlog(errorMsg)
  347. HttpUtils.notify_cut_state(gcp_code, "99", errorMsg)
  348. return
  349. storepass = Json_Util.ReadJson(config_json_path, "storepass")
  350. printlog("[storepass] : %s" % storepass)
  351. keypass = Json_Util.ReadJson(config_json_path, "keypass")
  352. printlog("[keypass] : %s" % keypass)
  353. alias = Json_Util.ReadJson(config_json_path, "alias")
  354. printlog("[alias] : %s" % alias)
  355. # 反编译原apk文件的输出路径
  356. apk_decompile_out_dir = origin_apk_dir + DIR_SPLIT + origin_apk_name
  357. printlog("[apk_decompile_out_dir] : %s" % apk_decompile_out_dir)
  358. # icon路径
  359. icon_path = Json_Util.ReadJson(config_json_path, "icon")
  360. printlog("[icon_path]: %s" % icon_path)
  361. # 版本号
  362. version = Json_Util.ReadJson(config_json_path, "version")
  363. vCode = Json_Util.ReadJson(config_json_path, "v_code")
  364. # targetSdkVersion
  365. targetSdkVersion = Json_Util.ReadJson(config_json_path, "targetSdkVersion")
  366. HttpUtils.notify_cut_state(gcp_code, "10", "正在反编译文件")
  367. # 判断反编译出的文件修改时间与apk修改时间比较,若比apk最则重新反编译
  368. ret = File.compareFileModifyTime(origin_apk_full_path, apk_decompile_out_dir)
  369. if ret != None:
  370. printlog("母包未更新,无需重新反编译")
  371. if ret == None or ret > 0:
  372. # 反编译原apk文件
  373. decompile(origin_apk_full_path, apk_decompile_out_dir)
  374. HttpUtils.notify_cut_state(gcp_code, "20", "正在复制文件")
  375. randomNumber = getRamdomNumber(6)
  376. apk_decompile_tmp_dir = origin_apk_dir + DIR_SPLIT + randomNumber + DIR_SPLIT + "dcm_tmp" + sdk_name
  377. printlog("[apk_decompile_tmp_dir] : %s" % apk_decompile_tmp_dir)
  378. shutil.copytree(apk_decompile_out_dir, apk_decompile_tmp_dir)
  379. HttpUtils.notify_cut_state(gcp_code, "30", "正在合并资源")
  380. # compileResources(apk_decompile_tmp_dir, tmp_dir, package_name)
  381. if package_name != "" and package_name != None:
  382. printlog("modifyPkgName %s" % package_name)
  383. modifyPkgName(apk_decompile_tmp_dir, package_name)
  384. pass
  385. # 合并Manifest文件
  386. mergeManifest(sdk_root_dir + DIR_SPLIT + sdk_name + DIR_SPLIT + sdk_name + "_sdk_config.xml",
  387. apk_decompile_tmp_dir)
  388. # 合并资源
  389. mergeResources(sdk_root_dir + DIR_SPLIT + sdk_name + DIR_SPLIT + sdk_name, apk_decompile_tmp_dir)
  390. # 生成R文件
  391. # Aapt_Util.createRFile(apk_decompile_tmp_dir,True, package_name)
  392. # 替换YyrhParam中的gcp_code
  393. printlog(apk_decompile_tmp_dir + DIR_SPLIT + "assets" + DIR_SPLIT + "YyrhParam.cnf")
  394. Replace.replaceAssets_Param(apk_decompile_tmp_dir + DIR_SPLIT + "assets" + DIR_SPLIT + "YyrhParam.cnf",
  395. "GCP_CODE", gcp_code)
  396. # 替换Androidmanifest.xml中的yyrh_gameChannelID为gcp_code yyrh_game_channel_id
  397. Replace.replaceAM_Meta_data(apk_decompile_tmp_dir + DIR_SPLIT + "AndroidManifest.xml", "yyrh_gameChannelID",
  398. gcp_code)
  399. # 修改debuggable为false
  400. # Replace.replaceAM_Debuggable("%s/AndroidManifest.xml"%apk_decompile_out_dir)
  401. # 替换Icon
  402. HttpUtils.notify_cut_state(gcp_code, "40", "正在修改资源")
  403. # 替换Icon
  404. printlog("----- begin replace resource -----")
  405. if icon_path:
  406. ret = Icon_Util.replaceIcon(icon_path, apk_decompile_tmp_dir)
  407. if not ret:
  408. errorMsg = "找不到icon,请检查"
  409. printlog(errorMsg)
  410. HttpUtils.notify_cut_state(gcp_code, "99", errorMsg)
  411. # 替换启动页
  412. splash_path = Json_Util.ReadJson(config_json_path, "splash")
  413. if splash_path:
  414. if os.path.exists(splash_path):
  415. shutil.copy(splash_path, "%s/assets/yyrh_start_image.jpg" % apk_decompile_tmp_dir)
  416. else:
  417. errorMsg = "找不到闪屏文件,请检查"
  418. printlog(errorMsg)
  419. HttpUtils.notify_cut_state(gcp_code, "99", errorMsg)
  420. return
  421. ########### #sdk 关键字
  422. HttpUtils.notify_cut_state(gcp_code, "50", "正在替换渠道参数")
  423. sdkParam = Json_Util.ReadJson(config_json_path, "sdk_client_config")
  424. printlog(str(sdkParam))
  425. # 替换Androidmanifest.xml中的 package_name值为包名
  426. Replace.replaceAM_package_name(apk_decompile_tmp_dir + DIR_SPLIT + "AndroidManifest.xml")
  427. if len(sdkParam):
  428. keys = Json_Util.getJsonKeys(sdkParam)
  429. for key in keys:
  430. # 替换AndroidManifest.xml中的meta-data字段
  431. Replace.replaceAM_Meta_data(apk_decompile_tmp_dir + DIR_SPLIT + "AndroidManifest.xml", key,
  432. sdkParam[key])
  433. # 替换assets/YyrhParam.cnf中的关键字
  434. Replace.replaceAssets_Param(apk_decompile_tmp_dir + DIR_SPLIT + "assets" + DIR_SPLIT + "YyrhParam.cnf",
  435. key, sdkParam[key])
  436. # 特殊渠道需要特殊处理
  437. Replace.special_replace(sdk_name, apk_decompile_tmp_dir, DIR_SPLIT, key, sdkParam[key])
  438. pass
  439. pass
  440. ########## #game 关键字
  441. gameParam = Json_Util.ReadJson(config_json_path, "meta_config")
  442. printlog("meta_config:%s" % gameParam)
  443. if len(gameParam):
  444. keys = Json_Util.getJsonKeys(gameParam)
  445. for key in keys:
  446. if key == "APP_NAME":
  447. Replace.replaceString_Appname(apk_decompile_tmp_dir, key, gameParam[key])
  448. elif (key == "version"):
  449. version = gameParam[key]
  450. elif (key == "v_code"):
  451. vCode = gameParam[key]
  452. elif (key == "targetSdkVersion"):
  453. targetSdkVersion = gameParam[key]
  454. # 替换方向
  455. elif (key == "SDK_ORIENTATION"):
  456. replaceOrientation = '/bin/sed -i "1,\\$s/sdk_orientation/%s/g" %s/AndroidManifest.xml' % (
  457. gameParam[key], apk_decompile_tmp_dir)
  458. printlog("replaceOrientationCmd: %s" % replaceOrientation)
  459. os.system(replaceOrientation)
  460. Replace.replaceAssets_Param(
  461. apk_decompile_tmp_dir + DIR_SPLIT + "assets" + DIR_SPLIT + "YyrhParam.cnf", "SDK_ORIENTATION",
  462. gameParam[key])
  463. pass
  464. # 修改版本号
  465. HttpUtils.notify_cut_state(gcp_code, "60", "正在修改版本号")
  466. yml = "%s/apktool.yml" % apk_decompile_tmp_dir
  467. if version != None and version != "":
  468. Replace.replaceAM_VersionName(yml, version)
  469. if vCode != None and vCode != "":
  470. ret = CommonUtils.isMatchRegExp(vCode, "0123456789")
  471. if ret == False:
  472. errorMsg = "游戏版本号,需为纯数字"
  473. printlog(errorMsg)
  474. HttpUtils.notify_cut_state(gcp_code, "99", errorMsg)
  475. return
  476. Replace.replaceAM_VersionCode(yml, vCode)
  477. if targetSdkVersion != None and targetSdkVersion != "":
  478. ret = CommonUtils.isMatchRegExp(targetSdkVersion, "0123456789")
  479. if ret == False:
  480. errorMsg = "游戏targetSdkVersion,需为纯数字"
  481. printlog(errorMsg)
  482. HttpUtils.notify_cut_state(gcp_code, "99", errorMsg)
  483. return
  484. Replace.replaceYml_targetSdkVersion(yml, targetSdkVersion)
  485. Replace.replaceYml_minSdkVersion(yml)
  486. mergeWXPay(sdk_root_dir + DIR_SPLIT + sdk_name + DIR_SPLIT + sdk_name, apk_decompile_tmp_dir)
  487. HttpUtils.notify_cut_state(gcp_code, "70", "正在替换游戏资源")
  488. # 替换游戏资源
  489. resourcepaths = Json_Util.ReadJson(config_json_path, "game_resource_replace")
  490. printlog(("游戏替换资源预路径:%s" % resourcepaths))
  491. if resourcepaths != None and resourcepaths != "":
  492. for resourcepath in resourcepaths:
  493. path = resourcepath[1]
  494. if not os.path.exists(path) and path != "":
  495. errorMsg = "游戏资源不存在:%s" % path
  496. printlog(errorMsg)
  497. HttpUtils.notify_cut_state(gcp_code, "99", errorMsg)
  498. return
  499. Replace_GameRes_Utils.ReplaceGameResource(apk_decompile_tmp_dir, resourcepaths)
  500. dst_dir = "%s%s" % (apk_utils.getOutPutDir(), DIR_SPLIT)
  501. channels = Json_Util.ReadJson(config_json_path, "channels")
  502. Replace.replaceAM_Meta_data("%s/AndroidManifest.xml" % apk_decompile_tmp_dir, Contants.getSdkChannelid(),
  503. channels)
  504. outputApkPath = "%s_unsigned.apk" % apk_decompile_tmp_dir
  505. signedApkPath = "%s_signed.apk" % apk_decompile_tmp_dir
  506. zipalignApkPath = "%s_zipaligned.apk" % apk_decompile_tmp_dir
  507. # 再编译
  508. HttpUtils.notify_cut_state(gcp_code, "80", "正在回编译APK")
  509. recompile(apk_decompile_tmp_dir, outputApkPath)
  510. HttpUtils.notify_cut_state(gcp_code, "90", "正在重签名,对齐APK")
  511. # 重签名
  512. resignApk(outputApkPath, keystorepath, storepass, alias, keypass, signedApkPath)
  513. # 对齐
  514. zipalignApk(signedApkPath, zipalignApkPath)
  515. apkDstPath = "%sg%s_%s_%s_%s.apk" % (dst_dir, g_id, sdk_name, package_name, gcp_code)
  516. apkDstDir = os.path.dirname(apkDstPath)
  517. if not os.path.exists(apkDstDir):
  518. os.makedirs(apkDstDir)
  519. shutil.copyfile(zipalignApkPath, apkDstPath)
  520. HttpUtils.notify_cut_state(gcp_code, "100", "切包成功(点击复制链接)")
  521. time_end = time.time()
  522. time_c = time_end - time_start
  523. printlog("APK路径:%s" % apkDstPath)
  524. printlog("切包总用时:%s秒" % time_c)
  525. except Exception as err:
  526. printlog(errorMsg)
  527. HttpUtils.notify_cut_state(gcp_code, "99", errorMsg)
  528. isSuccess = False
  529. printlog("cut error occur:%s" % err)
  530. printlog(traceback.format_exc())
  531. finally:
  532. printlog("clear temp dir")
  533. # 删除中间生成的目录和文件
  534. if apk_decompile_tmp_dir:
  535. File.safeFileDelete(os.path.dirname(apk_decompile_tmp_dir))
  536. return isSuccess
  537. if __name__ == "__main__":
  538. ret, msg = mergeYml("sdk.yml", "/Volumes/forsdk/sdk/develop/921sdk_cut/script/P0000005/apktool.yml")
  539. if not ret:
  540. print(msg)
  541. #
  542. # eg. python merge_apk_v2.py config.json
  543. #