package_utils_yfsdk.py 44 KB

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