package_utils.py 43 KB

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