package_utils_yfsdk.py 44 KB

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