package_utils.py 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. # 安卓游戏打包脚本
  2. # 1.用apktool解包
  3. # 2.复制res资源、assets资源、jniLib资源
  4. # 3.修改包名、app名、渠道文件
  5. # 4.添加app icon、合并AndroidManifest文件
  6. # 5.添加app启动图,修改AndroidManifest文件
  7. # 6.生成新的R文件
  8. # 7.将新的R文件编译,生成dex,再用baksmali生成smali,替换旧的R文件
  9. # 8.将jar资源打包成dex,再用baksmali生成smali,拷贝到smali目录下
  10. # 9.统计方法量,并分割dex(将smali文件拷贝到目录smali_classes2)
  11. # 10.用apktool回包
  12. # 11.重新签名
  13. import file_utils
  14. import xml_utils
  15. import smali_utils
  16. import config_utils
  17. import game_utils
  18. import os
  19. import os.path
  20. import json
  21. import sys
  22. import importlib
  23. import uuid
  24. import zipfile
  25. ignoreLauncher = ['jm_ysdk', 'jm_yijie', 'jm_quick', 'jm_beiyu', 'jm_xq_jrtt','jm_zy_ysdk']
  26. adaptApp = ['yjzx']
  27. def pack(game, sdk, config):
  28. config['cache'] = uuid.uuid1()
  29. subChannel = config['subChannel']
  30. print('game = %s, sdk = %s, subChannel = %s, ...' % (game,sdk,subChannel))
  31. # 解包
  32. ret = decomplie(game, sdk, subChannel, config)
  33. if ret:
  34. return ret
  35. # 删除旧代码
  36. ret = removeOldCode(game, sdk, subChannel, config)
  37. if ret:
  38. return ret
  39. # 删除一些不支持的属性
  40. ret = removeNoSupportAttr(game, sdk, subChannel, config)
  41. if ret:
  42. return ret
  43. # 删除一些不支持的配置
  44. ret = fixUnSupportConfig(game, sdk, subChannel, config)
  45. if ret:
  46. return ret
  47. # 合并Drawable-v4目录
  48. ret = mergeDrawableRes(game, sdk, subChannel, config)
  49. if ret:
  50. return ret
  51. # 移除相同的资源
  52. ret = removeSameRes(game, sdk, subChannel, config)
  53. if ret:
  54. return ret
  55. # 复制res资源
  56. ret = copyRes(game, sdk, subChannel, config)
  57. if ret:
  58. return ret
  59. # 合并主文件
  60. ret = mergeManifestRes(game, sdk, subChannel, config)
  61. if ret:
  62. return ret
  63. # 替换占位符
  64. ret = changePlaceholders(game, sdk, subChannel, config)
  65. if ret:
  66. return ret
  67. # 添加meta-data
  68. ret = addMetaData(game, sdk, subChannel, config)
  69. if ret:
  70. return ret
  71. # 增加配置文件
  72. ret = addConfig(game, sdk, subChannel, config)
  73. if ret:
  74. return ret
  75. # 复制app res资源
  76. ret = copyAppRes(game, sdk, subChannel, config)
  77. if ret:
  78. return ret
  79. # 更改包名
  80. if 'packageName' in config and config['packageName'] != '':
  81. ret = changePackageName(game, sdk, subChannel, config)
  82. if ret:
  83. return ret
  84. # 更改app名
  85. if 'name' in config and config['name'] != '':
  86. ret = changeAppName(game, sdk, subChannel, config)
  87. if ret:
  88. return ret
  89. # 更改app icon
  90. if config['changeIcon']:
  91. ret = changeAppIcon(game, sdk, subChannel, config)
  92. if ret:
  93. return ret
  94. # 添加启动图操作
  95. if config['addLauncher']:
  96. ret = addLauncher(game, sdk, subChannel, config)
  97. if ret:
  98. return ret
  99. # 添加多图标
  100. #ret = addMoreIcon(game, sdk, subChannel, config)
  101. # 复制icon图标到res
  102. ret = copyIcon(game, sdk, subChannel, config)
  103. #if ret:
  104. # return ret
  105. # 打包lib依赖
  106. ret = packJar(game, sdk, subChannel, config)
  107. if ret:
  108. return ret
  109. # sdk脚本处理
  110. ret = doSDKPostScript(game, sdk, config)
  111. if ret:
  112. return ret
  113. # 乐变sdk的特殊处理
  114. ret = game_utils.sdkLebianChange(game, sdk, config)
  115. if ret:
  116. return ret
  117. # 游戏脚本处理
  118. ret = doGamePostScript(game, sdk, config)
  119. if ret:
  120. return ret
  121. # log sdk
  122. if 'logSdk' in config:
  123. for log in config['logSdk']:
  124. ret = addLogSdk(game, sdk, subChannel, config, log)
  125. if ret:
  126. return ret
  127. # 生成R文件
  128. '''ret = generateNewRFile(game, sdk, subChannel, config)
  129. if ret:
  130. return ret'''
  131. # 添加MultiDex支持
  132. if config['splitDex']:
  133. ret = splitDex(game, sdk, subChannel, config)
  134. if ret:
  135. return ret
  136. # 更改版本号
  137. ret = changeVersion(game, sdk, subChannel, config)
  138. if ret:
  139. return ret
  140. # 回编译
  141. ret = recomplie(game, sdk, subChannel, config)
  142. if ret:
  143. return ret
  144. # 对齐apk
  145. ret = alignApk(game, sdk, subChannel, config)
  146. if ret:
  147. return ret
  148. # 签名
  149. ret = apksignerApk(game, sdk, subChannel, config)
  150. if ret:
  151. return ret
  152. # 添加渠道信息
  153. ret = addChannel(game, sdk, subChannel, config)
  154. if ret:
  155. return ret
  156. # 清理产生的中间文件
  157. if config['clearCache']:
  158. clearTemp(game, sdk, subChannel, config)
  159. return 0
  160. def decomplie(game, sdk, subChannel, config):
  161. '''
  162. 解包
  163. '''
  164. apktoolPath = file_utils.getApkToolPath()
  165. gamePath = file_utils.getFullGameApk(game)
  166. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  167. if os.path.exists(decompliePath):
  168. print('delete decomplie folder...')
  169. file_utils.deleteFolder(decompliePath)
  170. print('decomplie apk...')
  171. return file_utils.execJarCmd(apktoolPath, 'd -f "%s" -o "%s"' % (gamePath, decompliePath))
  172. def removeOldCode(game, sdk, subChannel, config):
  173. '''
  174. 删除旧代码
  175. '''
  176. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  177. codePath = os.path.join(decompliePath, 'smali', 'com', 'jmhy', 'sdk')
  178. #file_utils.deleteFolder(codePath)
  179. allFiles = []
  180. allFiles = file_utils.list_files(codePath, allFiles)
  181. for f in allFiles:
  182. fpath, fname = os.path.split(f) #分离文件名和路径
  183. if fname == 'R.smali' or fname.startswith('R$'):
  184. continue
  185. os.remove(f)
  186. print('remove %s' % f)
  187. return 0
  188. def copyRes(game, sdk, subChannel, config):
  189. '''
  190. 复制res资源
  191. '''
  192. # 拷贝sdk资源
  193. print('copy res...')
  194. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  195. sdkPath = file_utils.getFullSDKPath(sdk)
  196. resPath = os.path.join(sdkPath, 'res')
  197. decomplieResPath = file_utils.getFullPath(decompliePath, 'res')
  198. for d in os.listdir(resPath):
  199. copyResWithType(resPath, decomplieResPath, d)
  200. # 拷贝assets
  201. print('copy assets...')
  202. assetsPath = file_utils.getFullPath(sdkPath, 'assets')
  203. decomplieAssetsPath = file_utils.getFullPath(decompliePath, 'assets')
  204. if os.path.exists(assetsPath):
  205. ret = file_utils.copyFileAllDir(assetsPath, decomplieAssetsPath)
  206. if ret:
  207. return ret
  208. # 拷贝jniLib
  209. print('copy jniLibs...')
  210. jniPath = file_utils.getFullPath(sdkPath, 'jniLibs')
  211. decomplieJniPath = file_utils.getFullPath(decompliePath, 'lib')
  212. abiFilters = []
  213. if os.path.exists(decomplieJniPath):
  214. for abi in os.listdir(decomplieJniPath):
  215. if abi == 'armeabi-v7a' or abi == 'armeabi':
  216. abiFilters.append(abi)
  217. else:
  218. abiFilters = ['armeabi-v7a']
  219. if os.path.exists(jniPath):
  220. ret = file_utils.copyFileAllDir(jniPath, decomplieJniPath, False, abiFilters)
  221. if ret:
  222. return ret
  223. return 0
  224. def mergeDrawableRes(game, sdk, subChannel, config):
  225. '''
  226. 合并Drawable-v4目录
  227. '''
  228. print('merge drawable path...')
  229. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  230. resPath = os.path.join(decompliePath, 'res')
  231. for path in os.listdir(resPath):
  232. print('res path %s' % path)
  233. if (path.startswith('drawable') or path.startswith('mipmap')) and path.endswith('-v4'):
  234. print('merge path %s' % path)
  235. v4DrawablePath = os.path.join(resPath, path)
  236. drawablePath = os.path.join(resPath, path[:-3])
  237. if os.path.exists(drawablePath):
  238. ret = file_utils.copyFileAllDir(v4DrawablePath, drawablePath, True)
  239. if ret:
  240. return ret
  241. else:
  242. os.rename(v4DrawablePath, drawablePath)
  243. return 0
  244. def removeSameRes(game, sdk, subChannel, config):
  245. '''
  246. 移除相同的资源
  247. '''
  248. # 读取sdk的资源
  249. print('remove same res...')
  250. sdkPath = file_utils.getFullSDKPath(sdk)
  251. sdkResPath = os.path.join(sdkPath, 'res')
  252. resList = []
  253. for path in os.listdir(sdkResPath):
  254. if not path.startswith('values'):
  255. continue
  256. absPath = os.path.join(sdkResPath, path)
  257. for resFile in os.listdir(absPath):
  258. '''if not resFile.startswith('jm_'):
  259. continue'''
  260. resList = xml_utils.readAllRes(os.path.join(absPath, resFile), resList)
  261. if len(resList) == 0:
  262. print('no same res found')
  263. return 0
  264. # 移除相同的资源
  265. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  266. resPath = os.path.join(decompliePath, 'res')
  267. for path in os.listdir(resPath):
  268. if not path.startswith('values'):
  269. continue
  270. absPath = os.path.join(resPath, path)
  271. for resFile in os.listdir(absPath):
  272. xml_utils.removeSameRes(os.path.join(absPath, resFile), resList)
  273. return 0
  274. def mergeManifestRes(game, sdk, subChannel, config):
  275. '''
  276. 合并主文件
  277. '''
  278. print('merge AndroidManifest...')
  279. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  280. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  281. sdkPath = file_utils.getFullSDKPath(sdk)
  282. libManifest = file_utils.getFullPath(sdkPath, 'manifest.xml')
  283. return xml_utils.mergeManifestRes(manifest, libManifest)
  284. def copyAppRes(game, sdk, subChannel, config):
  285. '''
  286. 拷贝app的资源,比如app icon、启动图等
  287. '''
  288. channelPath = file_utils.getSubChannelPath(game, sdk, subChannel)
  289. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  290. # assets
  291. print('copy assets...')
  292. #适配一剑斩仙
  293. if game in adaptApp:
  294. print('适配一剑斩仙...')
  295. decomplieAssetsPath = file_utils.getFullPath(decompliePath, 'assets')
  296. skinZipPath = os.path.join(decomplieAssetsPath, 'skin.zip')
  297. skinPath = os.path.join(decomplieAssetsPath, 'skin')
  298. if os.path.exists(skinZipPath):
  299. with zipfile.ZipFile(skinZipPath) as zf:
  300. zf.extractall(decomplieAssetsPath)
  301. print('create unzip skin' )
  302. assetsPath = file_utils.getFullPath(channelPath, 'assets')
  303. decomplieAssetsPath = file_utils.getFullPath(decompliePath, 'assets')
  304. if os.path.exists(assetsPath):
  305. ret = file_utils.copyFileAllDir(assetsPath, decomplieAssetsPath)
  306. with zipfile.ZipFile(skinZipPath, 'w') as z:
  307. for root, dirs, files in os.walk(skinPath):
  308. for single_file in files:
  309. filepath = os.path.join(root, single_file)
  310. print ('create zip ---> ' + filepath)
  311. temPath = 'skin/' + single_file
  312. z.write(filepath, temPath)
  313. z.close()
  314. if ret:
  315. return ret
  316. else:
  317. print('normal copy assets...')
  318. assetsPath = file_utils.getFullPath(channelPath, 'assets')
  319. decomplieAssetsPath = file_utils.getFullPath(decompliePath, 'assets')
  320. if os.path.exists(assetsPath):
  321. ret = file_utils.copyFileAllDir(assetsPath, decomplieAssetsPath)
  322. if ret:
  323. return ret
  324. else:
  325. print('normal copy assets...')
  326. assetsPath = file_utils.getFullPath(channelPath, 'assets')
  327. decomplieAssetsPath = file_utils.getFullPath(decompliePath, 'assets')
  328. if os.path.exists(assetsPath):
  329. ret = file_utils.copyFileAllDir(assetsPath, decomplieAssetsPath)
  330. if ret:
  331. return ret
  332. # icon
  333. print('copy icon...')
  334. ret = copyAppResWithType(decompliePath, channelPath, 'icon')
  335. if ret:
  336. return ret
  337. # 启动图
  338. print('copy splash...')
  339. ret = copyAppResWithType(decompliePath, channelPath, 'splash')
  340. if ret:
  341. return ret
  342. # 其他图片
  343. print('copy image...')
  344. ret = copyAppResWithType(decompliePath, channelPath, 'image')
  345. if ret:
  346. return ret
  347. return 0
  348. def copyAppResWithType(decompliePath, channelPath, typeName):
  349. decomplieResPath = os.path.join(decompliePath, 'res')
  350. iconPath = os.path.join(channelPath, typeName)
  351. if not os.path.exists(iconPath):
  352. print('dir "%s" not exists' % iconPath)
  353. return 0
  354. for d in os.listdir(iconPath):
  355. ret = copyResWithType(iconPath, decomplieResPath, d)
  356. if ret:
  357. return ret
  358. return 0
  359. def copyResWithType(resPath, decomplieResPath, typeName):
  360. # appt的打包目录会带-v4后缀
  361. resDir = os.path.join(resPath, typeName)
  362. target = os.path.join(decomplieResPath, typeName)
  363. targetV4 = os.path.join(decomplieResPath, typeName + '-v4')
  364. if not os.path.exists(target) and not os.path.exists(targetV4):
  365. os.makedirs(target)
  366. return file_utils.copyFileAllDir(resDir, target, False)
  367. elif not os.path.exists(target):
  368. return file_utils.copyFileAllDir(resDir, targetV4, False)
  369. else:
  370. return file_utils.copyFileAllDir(resDir, target, False)
  371. def removeNoSupportAttr(game, sdk, subChannel, config):
  372. '''
  373. 删除一些不支持的属性
  374. '''
  375. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  376. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  377. xml_utils.removeRootAttr(manifest, 'compileSdkVersion')
  378. xml_utils.removeRootAttr(manifest, 'compileSdkVersionCodename')
  379. return 0
  380. def fixUnSupportConfig(game, sdk, subChannel, config):
  381. '''
  382. 删除一些不支持的配置
  383. '''
  384. # 检查minSdkVersion
  385. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  386. yml = os.path.join(decompliePath, 'apktool.yml')
  387. minSdkVersion = 15
  388. file_utils.changeMinSdkVersion(yml, minSdkVersion)
  389. resPath = os.path.join(decompliePath, 'res')
  390. tag = '-v'
  391. for res in os.listdir(resPath):
  392. print('res = ' + res)
  393. if res.startswith('values') and tag in res:
  394. start = res.index(tag)
  395. version = res[start+len(tag):]
  396. if not version.isdigit():
  397. continue
  398. version = int(version)
  399. print('version = %d' % version)
  400. if version < minSdkVersion:
  401. unSopportPath = os.path.join(resPath, res)
  402. print('unSopportPath = ' + unSopportPath)
  403. file_utils.deleteFolder(unSopportPath)
  404. print('deleteFolder = ' + unSopportPath)
  405. return 0
  406. def changePackageName(game, sdk, subChannel, config):
  407. '''
  408. 更改包名
  409. '''
  410. # 全局替换AndroidManifest里面的包名
  411. newPackageName = config['packageName']
  412. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  413. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  414. packageName = xml_utils.getPackageName(manifest)
  415. xml_utils.changePackageName(manifest, newPackageName)
  416. print('change package name %s --> %s' % (packageName, newPackageName))
  417. return 0
  418. def changeAppName(game, sdk, subChannel, config):
  419. '''
  420. 更改app名
  421. '''
  422. # 生成string.xml文件
  423. name = config['name']
  424. resName = 'common_sdk_name'
  425. if 'outName' in config:
  426. resName = resName + '_' + config['outName']
  427. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  428. stringFile = os.path.join(decompliePath, 'res', 'values', 'sdk_strings_temp.xml')
  429. content = '<?xml version="1.0" encoding="utf-8"?><resources><string name="%s">%s</string></resources>' % (resName, name)
  430. file_utils.createFile(stringFile, content)
  431. # 修改主文件的app名的值
  432. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  433. xml_utils.changeAppName(manifest, '@string/%s' % resName)
  434. print('change app name %s' % name)
  435. return 0
  436. def changeAppIcon(game, sdk, subChannel, config):
  437. '''
  438. 更改app icon
  439. '''
  440. print('change app icon...')
  441. resName = 'common_sdk_icon'
  442. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  443. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  444. xml_utils.changeAppIcon(manifest, '@mipmap/%s' % resName)
  445. return 0
  446. def addMetaData(game, sdk, subChannel, config):
  447. '''
  448. 添加meta-data
  449. '''
  450. if 'metaData' not in config:
  451. return 0
  452. print('add meta-data...')
  453. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  454. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  455. xml_utils.addMetaData(manifest, config['metaData'])
  456. return 0
  457. def addConfig(game, sdk, subChannel, config):
  458. '''
  459. 添加config.json
  460. '''
  461. if 'configData' not in config:
  462. return 0
  463. print('add config.json...')
  464. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  465. configJson = os.path.join(decompliePath, 'assets', 'jmhy_config.json')
  466. jsonText = json.dumps(config['configData'], ensure_ascii=False)
  467. file_utils.createFile(configJson, jsonText)
  468. return 0
  469. def changePlaceholders(game, sdk, subChannel, config):
  470. '''
  471. 处理掉占位符
  472. '''
  473. if 'placeholders' not in config:
  474. return 0
  475. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  476. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  477. placeholders = config['placeholders']
  478. for placeholder in placeholders:
  479. oldText = '${%s}' % placeholder
  480. newText = placeholders[placeholder]
  481. print('change placeholder %s -> %s' % (oldText, newText))
  482. file_utils.replaceContent(manifest, oldText, newText)
  483. return 0
  484. def addLauncher(game, sdk, subChannel, config):
  485. '''
  486. 添加启动图
  487. '''
  488. # ysdk的特殊处理
  489. if sdk in ignoreLauncher:
  490. return 0
  491. channelPath = file_utils.getSubChannelPath(game, sdk, subChannel)
  492. splashPath = os.path.join(channelPath, 'splash')
  493. if len(os.listdir(splashPath)) == 0:
  494. print('dir splash is empty')
  495. return 0
  496. print('add launcher...')
  497. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  498. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  499. activity = xml_utils.getLauncherActivityName(manifest)
  500. if activity == 'com.jmhy.sdk.template.LauncherActivity':
  501. print('add launcher already exist...')
  502. return 1
  503. # 添加关联资源
  504. internalPath = file_utils.getFullInternalPath()
  505. ret = copyAppResWithType(decompliePath, internalPath, 'launcher_res')
  506. if ret:
  507. return ret
  508. # 拷贝代码
  509. print('copy launcher code...')
  510. codePath = os.path.join(internalPath, 'launcher_code', 'smali')
  511. smaliPath = file_utils.getFullPath(decompliePath, 'smali')
  512. ret = file_utils.copyFileAllDir(codePath, smaliPath)
  513. if ret:
  514. return ret
  515. # 修改主文件信息
  516. print('change launcher config...')
  517. orientation = xml_utils.getScreenOrientation(manifest)
  518. activity = xml_utils.removeLauncherActivity(manifest)
  519. xml_utils.addLauncherActivity(manifest, orientation, 'com.jmhy.sdk.template.LauncherActivity')
  520. # 修改跳转的
  521. launcherActivity = os.path.join(decompliePath, 'smali', 'com', 'jmhy', 'sdk', 'template', 'LauncherActivity.smali')
  522. file_utils.replaceContent(launcherActivity, '{class}', activity)
  523. print('change launcher %s to %s' % (activity, 'com.jmhy.sdk.template.LauncherActivity'))
  524. # config['oldLauncher'] = activity
  525. if 'launcherTime' in config:
  526. timeHex = formatHex(config['launcherTime'])
  527. file_utils.replaceContent(launcherActivity, '0x0BB8', timeHex)
  528. return 0
  529. def addMoreIcon(game, sdk, subChannel, config):
  530. '''
  531. 添加多个图标
  532. '''
  533. print('add more icon support...')
  534. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  535. icon = '@mipmap/common_sdk_icon'
  536. if not config['changeIcon']:
  537. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  538. icon = xml_utils.getApplicationAttr(manifest, 'icon')
  539. switchIcon = icon
  540. if config['switchIcon']:
  541. switchIcon = '@mipmap/common_sdk_icon2'
  542. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  543. return xml_utils.addMoreIcon(manifest, icon, switchIcon)
  544. def copyIcon(game, sdk, subChannel, config):
  545. '''
  546. 复制icon到res ,一键登录使用
  547. '''
  548. if config['changeIcon']:
  549. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  550. decomplieResPath = file_utils.getFullPath(decompliePath, 'res')
  551. iconPath = os.path.join(decomplieResPath, 'mipmap-xhdpi', 'common_sdk_icon.png')
  552. desPath = os.path.join(decomplieResPath, 'drawable-hdpi', 'jm_cmcc_icon.png')
  553. file_utils.copyFile(iconPath, desPath)
  554. else:
  555. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  556. decomplieResPath = file_utils.getFullPath(decompliePath, 'res')
  557. desPath = os.path.join(decomplieResPath, 'drawable-hdpi', 'jm_cmcc_icon.png')
  558. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  559. icon = xml_utils.getApplicationAttr(manifest, 'icon')
  560. tag = icon[1:].split("/")
  561. if len(tag) < 1:
  562. return
  563. hdpi = ['-xhdpi', '-xxhdpi', '-xxxhdpi']
  564. for h in hdpi:
  565. p1 = '%s%s' % (tag[0],h)
  566. png = tag[1] + ".png"
  567. iconPath = os.path.join(decomplieResPath, p1, png)
  568. if os.path.exists(iconPath):
  569. print ('iconPath = ' + iconPath)
  570. file_utils.copyFile(iconPath, desPath)
  571. break
  572. def formatHex(millisecond):
  573. '''
  574. 将毫秒转为16进制,4位格式
  575. '''
  576. timeHex = str(hex(millisecond)).upper()
  577. timeHex = timeHex[2:]
  578. formatHex = ''
  579. if len(timeHex) == 3:
  580. formatHex = '0x0' + timeHexaddLauncher
  581. elif len(timeHex) == 4:
  582. formatHex = '0x' + timeHex
  583. else:
  584. formatHex = '0x0BB8'
  585. return formatHex
  586. def doSDKPostScript(game, sdk, config):
  587. '''
  588. 执行sdk相关特殊处理脚本
  589. '''
  590. sdkPath = file_utils.getFullSDKPath(sdk)
  591. scriptPath = os.path.join(sdkPath, 'script')
  592. targetScript = os.path.join(scriptPath, 'sdk_script.py')
  593. if not os.path.exists(targetScript):
  594. print('sdk_script no exists')
  595. return 0
  596. print('doSDKPostScript...')
  597. sys.path.append(scriptPath)
  598. module = importlib.import_module('sdk_script')
  599. ret = module.execute(game, sdk, config)
  600. sys.path.remove(scriptPath)
  601. return ret
  602. def doGamePostScript(game, sdk, config):
  603. '''
  604. 执行游戏相关特殊处理脚本
  605. '''
  606. channelPath = file_utils.getFullGamePath(game)
  607. scriptPath = os.path.join(channelPath, 'script')
  608. targetScript = os.path.join(scriptPath, 'game_script.py')
  609. if not os.path.exists(targetScript):
  610. print('game_script no exists')
  611. return 0
  612. print('doGamePostScript...')
  613. sys.path.append(scriptPath)
  614. module = importlib.import_module('game_script')
  615. ret = module.execute(game, sdk, config)
  616. sys.path.remove(scriptPath)
  617. return ret
  618. def addLogSdk(game, sdk, subChannel, config, logSdk):
  619. # 拷贝jniLibs
  620. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  621. sdkPath = file_utils.getFullLogSDKPath(logSdk)
  622. print('copy log jniLibs...')
  623. jniPath = file_utils.getFullPath(sdkPath, 'jniLibs')
  624. decomplieJniPath = file_utils.getFullPath(decompliePath, 'lib')
  625. abiFilters = []
  626. if os.path.exists(decomplieJniPath):
  627. for abi in os.listdir(decomplieJniPath):
  628. if abi == 'armeabi-v7a' or abi == 'armeabi':
  629. abiFilters.append(abi)
  630. else:
  631. abiFilters = ['armeabi-v7a']
  632. if os.path.exists(jniPath):
  633. ret = file_utils.copyFileAllDir(jniPath, decomplieJniPath, False, abiFilters)
  634. if ret:
  635. return ret
  636. print('merge log AndroidManifest...')
  637. libManifest = file_utils.getFullPath(sdkPath, 'manifest.xml')
  638. if os.path.exists(libManifest):
  639. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  640. ret = xml_utils.mergeManifestRes(manifest, libManifest)
  641. if ret:
  642. return ret
  643. return packLogJar(game, sdk, subChannel, config, logSdk)
  644. def generateNewRFile(game, sdk, subChannel, config):
  645. '''
  646. 生成新的R文件
  647. '''
  648. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  649. androidPlatforms = file_utils.getAndroidCompileToolPath()
  650. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  651. decomplieResPath = file_utils.getFullPath(decompliePath, 'res')
  652. compliePath = file_utils.getFullPath(decompliePath, 'gen')
  653. if not os.path.exists(compliePath):
  654. os.makedirs(compliePath)
  655. # 生成R文件
  656. print('create R.java ...')
  657. if config['aapt2disable']:
  658. aapt = file_utils.getAAPTPath()
  659. ret = file_utils.getExecPermission(aapt)
  660. if ret:
  661. return ret
  662. createRCmd = '"%s" p -f -m -J "%s" -S "%s" -I "%s" -M "%s"' % (aapt, compliePath, decomplieResPath, androidPlatforms, manifest)
  663. ret = file_utils.execFormatCmd(createRCmd)
  664. if ret:
  665. return ret
  666. else:
  667. # compile
  668. aapt = file_utils.getAAPT2Path()
  669. ret = file_utils.getExecPermission(aapt)
  670. if ret:
  671. return ret
  672. print('compiled res ...')
  673. complieResPath = os.path.join(compliePath, 'compiled')
  674. complieResCmd = '"%s" compile --dir "%s" -o "%s"' % (aapt, decomplieResPath, complieResPath)
  675. file_utils.execFormatCmd(complieResCmd)
  676. # unzip
  677. print('unzip compiled res ...')
  678. unzipResPath = os.path.join(compliePath, 'aapt2_res')
  679. with zipfile.ZipFile(complieResPath) as zf:
  680. zf.extractall(unzipResPath)
  681. print('create unzip %s' % unzipResPath)
  682. # link
  683. print('link res ...')
  684. outApk = os.path.join(compliePath, 'res.apk')
  685. linkResCmd = '"%s" link -o "%s" -I "%s" --manifest "%s" --java "%s" --auto-add-overlay' % (aapt, outApk, androidPlatforms, manifest, compliePath)
  686. for filename in os.listdir(unzipResPath):
  687. linkResCmd += ' %s' % filename
  688. print('link cmd len is %s' % len(linkResCmd))
  689. ret = file_utils.execFormatCmd(linkResCmd, unzipResPath)
  690. if ret:
  691. return ret
  692. # 编译R文件
  693. print('complie R.java ...')
  694. packageName = xml_utils.getPackageName(manifest)
  695. packagePath = file_utils.getPackagePath(compliePath, packageName)
  696. RSourceFile = os.path.join(packagePath, 'R.java')
  697. complieRCmd = 'javac -source 1.8 -target 1.8 -encoding UTF-8 "%s"' % RSourceFile
  698. ret = file_utils.execFormatCmd(complieRCmd)
  699. if ret:
  700. return ret
  701. # 生成dex
  702. print('dex R.class ...')
  703. outDex = os.path.join(compliePath, 'classes.dex')
  704. if config['aapt2disable']:
  705. dx = file_utils.getDxPath()
  706. dexCmd = '--dex --no-warning --output="%s" "%s"' % (outDex, compliePath)
  707. else:
  708. dx = file_utils.getD8Path()
  709. clazz = os.path.join(packagePath, '*.class')
  710. dexCmd = '--lib "%s" --output "%s" %s' % (androidPlatforms, compliePath, clazz)
  711. ret = file_utils.execJarCmd(dx, dexCmd)
  712. if ret:
  713. return ret
  714. # 反向dex生成smali
  715. # 存放在out目录
  716. print('baksmali classes.dex ...')
  717. baksmaliPath = file_utils.getBaksmaliPath()
  718. outPath = file_utils.getFullPath(decompliePath, 'out')
  719. ret = file_utils.execJarCmd(baksmaliPath, 'd "%s" -o "%s"' % (outDex, outPath))
  720. if ret:
  721. return ret
  722. # 将生成的文件拷贝到目标目录
  723. print('copy R.smali ...')
  724. smaliPath = file_utils.getFullPath(decompliePath, 'smali')
  725. file_utils.copyFileAllDir(outPath, smaliPath)
  726. return 0
  727. def packJar(game, sdk, subChannel, config):
  728. '''
  729. 打包所有的jar
  730. '''
  731. splitDex = config['splitDex']
  732. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  733. outPath = file_utils.getFullPath(decompliePath, 'gen')
  734. if not os.path.exists(outPath):
  735. os.makedirs(outPath)
  736. if config['aapt2disable']:
  737. dx = file_utils.getDxPath()
  738. dexCmd = '--dex --multi-dex --no-warning --output="%s"' % outPath
  739. else:
  740. dx = file_utils.getD8Path()
  741. androidPlatforms = file_utils.getAndroidCompileToolPath()
  742. dexCmd = '--lib "%s" --output "%s"' % (androidPlatforms, outPath)
  743. # 找到所有lib依赖
  744. sdkPath = file_utils.getFullSDKPath(sdk)
  745. libs = os.path.join(sdkPath, 'libs')
  746. libConfig = os.path.join(libs, 'config.json')
  747. # 存在配置文件
  748. if os.path.exists(libConfig):
  749. jsonText = file_utils.readFile(libConfig)
  750. libConf = json.loads(jsonText)
  751. if 'libConfig' in config and config['libConfig'] in libConf:
  752. conf = config['libConfig']
  753. libList = libConf[conf]
  754. for jar in libList:
  755. dexCmd += ' ' + os.path.join(libs, jar)
  756. elif 'default' in libConf:
  757. libList = libConf['default']
  758. for jar in libList:
  759. dexCmd += ' ' + os.path.join(libs, jar)
  760. else:
  761. for jar in os.listdir(libs):
  762. if not jar.endswith('.jar'):
  763. continue
  764. dexCmd += ' ' + os.path.join(libs, jar)
  765. else:
  766. for jar in os.listdir(libs):
  767. if not jar.endswith('.jar'):
  768. continue
  769. dexCmd += ' ' + os.path.join(libs, jar)
  770. # multidex.jar
  771. if splitDex:
  772. dexCmd += ' ' + file_utils.getMultiDexPath()
  773. # sdk实现类
  774. print('packageing all jar ...')
  775. dexCmd += ' ' + os.path.join(sdkPath, '%s.jar' % sdk)
  776. ret = file_utils.execJarCmd(dx, dexCmd)
  777. if ret:
  778. return ret
  779. # 反向dex生成smali
  780. # 存放在out目录
  781. print('baksmali classes.dex ...')
  782. outDex = os.path.join(outPath, 'classes.dex')
  783. baksmaliPath = file_utils.getBaksmaliPath()
  784. outPath = file_utils.getFullPath(decompliePath, 'out')
  785. ret = file_utils.execJarCmd(baksmaliPath, 'd "%s" -o "%s"' % (outDex, outPath))
  786. if ret:
  787. return ret
  788. # 将生成的文件拷贝到目标目录
  789. print('copy all smali ...')
  790. smaliPath = file_utils.getFullPath(decompliePath, 'smali')
  791. ret = file_utils.copyFileAllDir(outPath, smaliPath, True)
  792. if ret:
  793. return ret
  794. return 0
  795. def packLogJar(game, sdk, subChannel, config, logSdk):
  796. '''
  797. 打包所有的jar
  798. '''
  799. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  800. outPath = file_utils.getFullPath(decompliePath, 'gen')
  801. if not os.path.exists(outPath):
  802. os.makedirs(outPath)
  803. if config['aapt2disable']:
  804. dx = file_utils.getDxPath()
  805. dexCmd = '--dex --multi-dex --no-warning --output="%s"' % outPath
  806. else:
  807. dx = file_utils.getD8Path()
  808. androidPlatforms = file_utils.getAndroidCompileToolPath()
  809. dexCmd = '--lib "%s" --output "%s"' % (androidPlatforms, outPath)
  810. # 找到所有lib依赖
  811. sdkPath = file_utils.getFullLogSDKPath(logSdk)
  812. libs = os.path.join(sdkPath, 'libs')
  813. libConfig = os.path.join(libs, 'config.json')
  814. # 存在配置文件
  815. if os.path.exists(libConfig):
  816. jsonText = file_utils.readFile(libConfig)
  817. libList = json.loads(jsonText)
  818. for jar in libList:
  819. if not jar.endswith('.jar'):
  820. continue
  821. dexCmd += ' ' + os.path.join(libs, jar)
  822. else:
  823. for jar in os.listdir(libs):
  824. if not jar.endswith('.jar'):
  825. continue
  826. dexCmd += ' ' + os.path.join(libs, jar)
  827. # sdk实现类
  828. print('packageing all log jar ...')
  829. dexCmd += ' ' + os.path.join(sdkPath, '%s.jar' % logSdk)
  830. ret = file_utils.execJarCmd(dx, dexCmd)
  831. if ret:
  832. return ret
  833. # 反向dex生成smali
  834. # 存放在out目录
  835. print('baksmali classes.dex ...')
  836. outDex = os.path.join(outPath, 'classes.dex')
  837. baksmaliPath = file_utils.getBaksmaliPath()
  838. outPath = file_utils.getFullPath(decompliePath, 'out')
  839. ret = file_utils.execJarCmd(baksmaliPath, 'd "%s" -o "%s"' % (outDex, outPath))
  840. if ret:
  841. return ret
  842. # 将生成的文件拷贝到目标目录
  843. print('copy all log smali ...')
  844. smaliPath = file_utils.getFullPath(decompliePath, 'smali')
  845. ret = file_utils.copyFileAllDir(outPath, smaliPath, True)
  846. if ret:
  847. return ret
  848. return 0
  849. def splitDex(game, sdk, subChannel, config):
  850. '''
  851. 分割dex
  852. '''
  853. # 判断是否已经存在application
  854. # 存在,则往原application添加内容
  855. # 不存在,则拷贝一个默认的android.support.multidex.MultiDexApplication
  856. print('add MultiDex support...')
  857. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  858. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  859. application = xml_utils.getApplicationAttr(manifest, 'name')
  860. if application is None:
  861. ret = xml_utils.changeApplicationAttr(manifest, 'name', 'android.support.multidex.MultiDexApplication')
  862. if ret:
  863. return ret
  864. else:
  865. smaliPath = os.path.join(decompliePath, 'smali')
  866. applicationFile = file_utils.getPackagePath(smaliPath, application)
  867. applicationFile += '.smali'
  868. ret = changeApplicationDex(applicationFile)
  869. if ret:
  870. return ret
  871. return splitSmali(game, sdk, subChannel, config, application)
  872. def changeApplicationDex(file):
  873. '''
  874. 修改application的smali文件,增加MultiDex操作
  875. '''
  876. index = file_utils.getApplicationSmaliIndex(file)
  877. file_utils.insertApplicationSmali(file, index)
  878. return 0
  879. def splitSmali(game, sdk, subChannel, config, application):
  880. '''
  881. 如果函数上限超过限制,自动拆分smali,以便生成多个dex文件
  882. '''
  883. print('splitSmali...')
  884. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  885. smaliPath = os.path.join(decompliePath, 'smali')
  886. appPackage = None
  887. if application:
  888. appPackage = application[:application.rfind('.')]
  889. appPackage = appPackage.replace('.', '/')
  890. allFiles = []
  891. allFiles = file_utils.list_files(smaliPath, allFiles)
  892. #print('file count is %d' % len(allFiles))
  893. #maxFuncNum = 65535
  894. # 留一点空间,防止计算误差
  895. maxFuncNum = 64000
  896. currFucNum = 0
  897. totalFucNum = 0
  898. currDexIndex = 1
  899. allRefs = []
  900. #保证Application等类在第一个classex.dex文件中
  901. for f in allFiles:
  902. f = f.replace('\\', '/')
  903. if (appPackage and appPackage in f) or '/android/support/multidex' in f:
  904. currFucNum += smali_utils.get_smali_method_count(f, allRefs)
  905. totalFucNum = currFucNum
  906. for f in allFiles:
  907. f = f.replace('\\', '/')
  908. if not f.endswith('.smali'):
  909. continue
  910. if (appPackage and appPackage in f) or '/android/support/multidex' in f:
  911. continue
  912. thisFucNum = smali_utils.get_smali_method_count(f, allRefs)
  913. totalFucNum += thisFucNum
  914. #print('%d # %d ==> %s' % (thisFucNum, currDexIndex, f))
  915. #print('totalFucNum is %d' % totalFucNum)
  916. if currFucNum + thisFucNum >= maxFuncNum:
  917. currFucNum = thisFucNum
  918. currDexIndex += 1
  919. newDexPath = os.path.join(decompliePath, 'smali_classes%d' % currDexIndex)
  920. os.makedirs(newDexPath)
  921. else:
  922. currFucNum += thisFucNum
  923. if currDexIndex > 1:
  924. newDexPath = os.path.join(decompliePath, 'smali_classes%d' % currDexIndex)
  925. targetFile = newDexPath + f[len(smaliPath):]
  926. file_utils.copyFile(f, targetFile, True)
  927. return 0
  928. def changeVersion(game, sdk, subChannel, config):
  929. '''
  930. 更改版本号
  931. '''
  932. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  933. yml = os.path.join(decompliePath, 'apktool.yml')
  934. versionCode = None
  935. versionName = None
  936. targetSdkVersion = None
  937. if 'versionCode' in config:
  938. versionCode = config['versionCode']
  939. if 'versionName' in config:
  940. versionName = config['versionName']
  941. if 'targetSdkVersion' in config:
  942. targetSdkVersion = config['targetSdkVersion']
  943. print('changeVersion versionCode=%s,versionName=%s,targetSdkVersion=%s' % (versionCode, versionName, targetSdkVersion))
  944. return file_utils.changeVersion(yml, versionCode, versionName, targetSdkVersion)
  945. def recomplie(game, sdk, subChannel, config):
  946. '''
  947. 回编译
  948. '''
  949. print('recomplie apk...')
  950. apktoolPath = file_utils.getApkToolPath()
  951. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  952. outApk = file_utils.getOutApkPath(game, sdk, subChannel, config['cache'])
  953. useAppt2 = ' --use-aapt2'
  954. if config['aapt2disable']:
  955. useAppt2 = ''
  956. return file_utils.execJarCmd(apktoolPath, 'b -f "%s" -o "%s"%s' % (decompliePath, outApk, useAppt2))
  957. def alignApk(game, sdk, subChannel, config):
  958. '''
  959. 对齐apk
  960. '''
  961. print('align apk...')
  962. outApk = file_utils.getOutApkPath(game, sdk, subChannel, config['cache'])
  963. alignApk = file_utils.getAlignApkPath(game, sdk, subChannel, config['cache'])
  964. alignapkTool = file_utils.getAlignPath()
  965. if os.path.exists(alignApk):
  966. os.remove(alignApk)
  967. ret = file_utils.getExecPermission(alignapkTool)
  968. if ret:
  969. return ret
  970. # zipalign.exe -v -p 4 input.apk output.apk
  971. return file_utils.execFormatCmd('"%s" -f -p 4 "%s" "%s"' % (alignapkTool, outApk, alignApk))
  972. def apksignerApk(game, sdk, subChannel, config):
  973. '''
  974. 签名apk
  975. '''
  976. print('sign apk...')
  977. print('game = %s, sdk = %s, subChannel = %s, ...' % (game,sdk,subChannel))
  978. path = os.path.join(file_utils.getCurrentPath(), 'keystore', 'key.json')
  979. jsonText = file_utils.readFile(path)
  980. signConfig = json.loads(jsonText)
  981. keystore = {}
  982. if game in signConfig:
  983. if sdk in signConfig[game] and subChannel in signConfig[game][sdk]:
  984. keystore = signConfig[game][sdk][subChannel]
  985. else:
  986. keystore = signConfig['default']
  987. else:
  988. keystore = signConfig['default']
  989. print('storeFile is "%s"' % keystore['storeFile'])
  990. apksigner = file_utils.getApksignerPath()
  991. alignApk = file_utils.getAlignApkPath(game, sdk, subChannel, config['cache'])
  992. signedApk = file_utils.getSignApkPath(game, sdk, subChannel, config['cache'])
  993. storeFile = os.path.join(file_utils.getCurrentPath(), 'keystore', keystore['storeFile'])
  994. if 'outName' in config and 'outPath' in config:
  995. if not os.path.exists(config['outPath']):
  996. os.makedirs(config['outPath'])
  997. signedApk = os.path.join(config['outPath'], config['outName'] + '.apk')
  998. elif 'outName' in config:
  999. signedApk = file_utils.getRenameApkPath(game, sdk, config['cache'], config['outName'])
  1000. # java -jar apksigner.jar sign --ks key.jks --ks-key-alias releasekey --ks-pass pass:pp123456 --key-pass pass:pp123456 --out output.apk input.apk
  1001. v2disable = ''
  1002. if 'v2disable' in config and config['v2disable']:
  1003. v2disable = ' --v2-signing-enabled=false'
  1004. return file_utils.execJarCmd(apksigner, 'sign%s --ks "%s" --ks-key-alias %s --ks-pass pass:%s --key-pass pass:%s --out "%s" "%s"' % (v2disable, storeFile, keystore['keyAlias'], keystore['storePassword'], keystore['keyPassword'], signedApk, alignApk))
  1005. def addChannel(game, sdk, subChannel, config):
  1006. '''
  1007. 添加渠道信息
  1008. '''
  1009. if 'v2disable' in config and config['v2disable']:
  1010. return 0
  1011. walle = file_utils.getWallePath()
  1012. signedApk = file_utils.getSignApkPath(game, sdk, subChannel, config['cache'])
  1013. if 'outName' in config and 'outPath' in config:
  1014. signedApk = os.path.join(config['outPath'], config['outName'] + '.apk')
  1015. elif 'outName' in config:
  1016. signedApk = file_utils.getRenameApkPath(game, sdk, config['cache'], config['outName'])
  1017. properties = config['properties']
  1018. appid = ''
  1019. appkey = ''
  1020. host = ''
  1021. if 'appid' in properties:
  1022. appid = properties['appid']
  1023. if 'appkey' in properties:
  1024. appkey = properties['appkey']
  1025. if 'host' in properties:
  1026. host = properties['host']
  1027. return file_utils.execJarCmd(walle, 'put -e version=%s,agent=%s,appid=%s,appkey=%s,host=%s "%s" "%s"' % (config_utils.getDate(), properties['agent'], appid, appkey, host, signedApk, signedApk))
  1028. def clearTemp(game, sdk, subChannel, config):
  1029. '''
  1030. 清空中间产生的文件
  1031. '''
  1032. print('clear temp...')
  1033. alignApk = file_utils.getAlignApkPath(game, sdk, subChannel, config['cache'])
  1034. outApk = file_utils.getOutApkPath(game, sdk, subChannel, config['cache'])
  1035. if os.path.exists(alignApk):
  1036. os.remove(alignApk)
  1037. if os.path.exists(outApk):
  1038. os.remove(outApk)
  1039. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  1040. file_utils.deleteFolder(decompliePath)
  1041. print('clear temp end')
  1042. def packConsoleInput():
  1043. '''
  1044. 控制台打包
  1045. '''
  1046. if len(sys.argv) < 3:
  1047. print('argument is missing')
  1048. return 1
  1049. # 校验参数
  1050. game = sys.argv[1]
  1051. sdk = sys.argv[2]
  1052. # 可选参数,没有则默认打全部渠道
  1053. subChannel = None
  1054. if len(sys.argv) > 3:
  1055. subChannel = sys.argv[3]
  1056. return packConsole(game, sdk, subChannel)
  1057. def packConsole(game, sdk, subChannel):
  1058. '''
  1059. 控制台打包
  1060. '''
  1061. if not os.path.exists(file_utils.getFullGameApk(game)):
  1062. print('game "%s" not exists' % game)
  1063. return 1
  1064. if not os.path.exists(file_utils.getFullSDKPath(sdk)):
  1065. print('sdk "%s" not exists' % sdk)
  1066. return 1
  1067. # 读取配置
  1068. channelPath = file_utils.getChannelPath(game, sdk)
  1069. configPath = os.path.join(channelPath, 'config.json')
  1070. if not os.path.exists(configPath):
  1071. print('%s not exists' % configPath)
  1072. return 1
  1073. jsonText = file_utils.readFile(configPath)
  1074. config = json.loads(jsonText)
  1075. # 检查参数
  1076. if not config_utils.checkConfig(config):
  1077. return 1
  1078. # 处理参数
  1079. config_utils.replaceArgs(config)
  1080. successCount = 0
  1081. failureCount = 0
  1082. if type(config) == dict:
  1083. if subChannel is None or config['subChannel'] == subChannel:
  1084. ret = pack(game, sdk, config)
  1085. if ret:
  1086. failureCount += 1
  1087. else:
  1088. successCount += 1
  1089. else:
  1090. print('subChannel "%s" no found' % subChannel)
  1091. return 1
  1092. elif type(config) == list:
  1093. found = False
  1094. for itemConfig in config:
  1095. if subChannel is None or itemConfig['subChannel'] == subChannel:
  1096. found = True
  1097. ret = pack(game, sdk, itemConfig)
  1098. if ret:
  1099. failureCount += 1
  1100. else:
  1101. successCount += 1
  1102. if not found:
  1103. print('subChannel "%s" no found' % subChannel)
  1104. return 1
  1105. print('success %d, failure %d' % (successCount, failureCount))
  1106. return 0