package_utils.py 43 KB

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