package_utils.py 43 KB

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