package_utils.py 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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. if game in signConfig:
  991. if sdk in signConfig[game]:
  992. keystore = signConfig[game][sdk]
  993. else:
  994. keystore = signConfig['default']
  995. else:
  996. keystore = signConfig['default']
  997. print('storeFile is "%s"' % keystore['storeFile'])
  998. apksigner = file_utils.getApksignerPath()
  999. alignApk = file_utils.getAlignApkPath(game, sdk, subChannel, config['cache'])
  1000. signedApk = file_utils.getSignApkPath(game, sdk, subChannel, config['cache'])
  1001. storeFile = os.path.join(file_utils.getCurrentPath(), 'keystore', keystore['storeFile'])
  1002. if 'outName' in config and 'outPath' in config:
  1003. if not os.path.exists(config['outPath']):
  1004. os.makedirs(config['outPath'])
  1005. signedApk = os.path.join(config['outPath'], config['outName'] + '.apk')
  1006. elif 'outName' in config:
  1007. signedApk = file_utils.getRenameApkPath(game, sdk, config['cache'], config['outName'])
  1008. # 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
  1009. v2disable = ''
  1010. if 'v2disable' in config and config['v2disable']:
  1011. v2disable = ' --v2-signing-enabled=false'
  1012. 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))
  1013. def addChannel(game, sdk, subChannel, config):
  1014. '''
  1015. 添加渠道信息
  1016. '''
  1017. if 'v2disable' in config and config['v2disable']:
  1018. return 0
  1019. walle = file_utils.getWallePath()
  1020. signedApk = file_utils.getSignApkPath(game, sdk, subChannel, config['cache'])
  1021. if 'outName' in config and 'outPath' in config:
  1022. signedApk = os.path.join(config['outPath'], config['outName'] + '.apk')
  1023. elif 'outName' in config:
  1024. signedApk = file_utils.getRenameApkPath(game, sdk, config['cache'], config['outName'])
  1025. properties = config['properties']
  1026. appid = ''
  1027. appkey = ''
  1028. host = ''
  1029. if 'appid' in properties:
  1030. appid = properties['appid']
  1031. if 'appkey' in properties:
  1032. appkey = properties['appkey']
  1033. if 'host' in properties:
  1034. host = properties['host']
  1035. 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))
  1036. def clearTemp(game, sdk, subChannel, config):
  1037. '''
  1038. 清空中间产生的文件
  1039. '''
  1040. print('clear temp...')
  1041. alignApk = file_utils.getAlignApkPath(game, sdk, subChannel, config['cache'])
  1042. outApk = file_utils.getOutApkPath(game, sdk, subChannel, config['cache'])
  1043. if os.path.exists(alignApk):
  1044. os.remove(alignApk)
  1045. if os.path.exists(outApk):
  1046. os.remove(outApk)
  1047. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  1048. file_utils.deleteFolder(decompliePath)
  1049. print('clear temp end')
  1050. def packConsoleInput():
  1051. '''
  1052. 控制台打包
  1053. '''
  1054. if len(sys.argv) < 3:
  1055. print('argument is missing')
  1056. return 1
  1057. # 校验参数
  1058. game = sys.argv[1]
  1059. sdk = sys.argv[2]
  1060. # 可选参数,没有则默认打全部渠道
  1061. subChannel = None
  1062. if len(sys.argv) > 3:
  1063. subChannel = sys.argv[3]
  1064. return packConsole(game, sdk, subChannel)
  1065. def packConsole(game, sdk, subChannel):
  1066. '''
  1067. 控制台打包
  1068. '''
  1069. if not os.path.exists(file_utils.getFullGameApk(game)):
  1070. print('game "%s" not exists' % game)
  1071. return 1
  1072. if not os.path.exists(file_utils.getFullSDKPath(sdk)):
  1073. print('sdk "%s" not exists' % sdk)
  1074. return 1
  1075. global startTime
  1076. startTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  1077. # 读取配置
  1078. channelPath = file_utils.getChannelPath(game, sdk)
  1079. configPath = os.path.join(channelPath, 'config.json')
  1080. if not os.path.exists(configPath):
  1081. print('%s not exists' % configPath)
  1082. return 1
  1083. jsonText = file_utils.readFile(configPath)
  1084. config = json.loads(jsonText)
  1085. # 检查参数
  1086. if not config_utils.checkConfig(config):
  1087. return 1
  1088. # 处理参数
  1089. config_utils.replaceArgs(config)
  1090. successCount = 0
  1091. failureCount = 0
  1092. if type(config) == dict:
  1093. if subChannel is None or config['subChannel'] == subChannel:
  1094. ret = pack(game, sdk, config)
  1095. if ret:
  1096. failureCount += 1
  1097. else:
  1098. successCount += 1
  1099. else:
  1100. print('subChannel "%s" no found' % subChannel)
  1101. return 1
  1102. elif type(config) == list:
  1103. found = False
  1104. for itemConfig in config:
  1105. if subChannel is None or itemConfig['subChannel'] == subChannel:
  1106. found = True
  1107. ret = pack(game, sdk, itemConfig)
  1108. if ret:
  1109. failureCount += 1
  1110. else:
  1111. successCount += 1
  1112. if not found:
  1113. print('subChannel "%s" no found' % subChannel)
  1114. return 1
  1115. print('success %d, failure %d' % (successCount, failureCount))
  1116. return 0