package_utils.py 44 KB

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