python批量打多渠道包

python批量打多渠道包

安卓市場太多,一個一個手動打包太累而且容易出錯。

也有用ant打包的,來看看python的批量打包方式:


舉個例子,如友盟(真心不是給他打廣告)的多渠道統計


我們只是需要動態的修改這個channel_value,然後打包就可以了。


思路

1.導出一個未簽名的apk包,其中渠道號配置如上圖。

2.使用apkTool解壓apk包

3.根據渠道號修改channel_value

4.使用apktool重新打包爲未簽名的apk包

5.給未簽名的apk包簽名


這裏選擇使用python來實現這些功能,感謝金兄讓我認識python(吐槽一下,python爲什麼沒有大括號)

組織結構如下圖:


其中channel爲渠道號列表:


keystore文件夾內放置着測試工程的簽名文件ApkTest.keystore

ApkTest.apk是未簽名的apk包


下面就是最重要的MakeTool.py文件

[python] view plaincopy
  1. #!/usr/bin/python  
  2. # coding=utf-8  
  3. import os  
  4. import shutil  
  5.   
  6. def readChannelfile(filename):  
  7.     f = file(filename)  
  8.     while True:  
  9.         line = f.readline().strip('\n')  
  10.         if len(line) == 0:  
  11.             break  
  12.         else:  
  13.             channelList.append(line);  
  14.     f.close()  
  15.   
  16. def backUpManifest():  
  17.     if os.path.exists('./AndroidManifest.xml'):  
  18.         os.remove('./AndroidManifest.xml')  
  19.     manifestPath = './temp/AndroidManifest.xml'  
  20.     shutil.copyfile(manifestPath, './AndroidManifest.xml')  
  21.   
  22. def modifyChannel(value):  
  23.     tempXML = ''  
  24.     f = file('./AndroidManifest.xml')  
  25.     for line in f:  
  26.         if line.find('channel_value') > 0:  
  27.             line = line.replace('channel_value', value)  
  28.         tempXML += line  
  29.     f.close()  
  30.   
  31.     output = open('./temp/AndroidManifest.xml''w')  
  32.     output.write(tempXML)  
  33.     output.close()  
  34.       
  35.     unsignApk = r'./bin/%s_%s_unsigned.apk'% (easyName, value)  
  36.     cmdPack = r'java -jar apktool.jar b temp %s'% (unsignApk)  
  37.     os.system(cmdPack)  
  38.       
  39.     unsignedjar = r'./bin/%s_%s_unsigned.apk'% (easyName, value)  
  40.     signed_unalignedjar = r'./bin/%s_%s_signed_unaligned.apk'% (easyName, value)  
  41.     signed_alignedjar = r'./bin/%s_%s.apk'% (easyName, value)  
  42.     cmd_sign = r'jarsigner -verbose -keystore %s -storepass %s -signedjar %s %s %s'% (keystore, storepass, signed_unalignedjar, unsignedjar, alianame)  
  43.     cmd_align = r'zipalign -v 4 %s %s' % (signed_unalignedjar, signed_alignedjar)  
  44.     os.system(cmd_sign)  
  45.     os.remove(unsignedjar)  
  46.     os.system(cmd_align)  
  47.     os.remove(signed_unalignedjar)  
  48.       
  49.   
  50. channelList = []  
  51. apkName = 'ApkTest.apk'  
  52. easyName = apkName.split('.apk')[0]  
  53. keystore='./keystore/ApkTest.keystore'  
  54. storepass='123456'  
  55. alianame='ApkTest.keystore'  
  56.   
  57. output_apk_dir="./bin"  
  58. if os.path.exists(output_apk_dir):  
  59.     shutil.rmtree(output_apk_dir)  
  60.   
  61. readChannelfile('./channel')  
  62. print '-------------------- your channel values --------------------'  
  63. print 'channel list: ', channelList  
  64. cmdExtract = r'java -jar apktool.jar  d -f -s %s temp'% (apkName)  
  65. os.system(cmdExtract)  
  66.   
  67. backUpManifest()  
  68. for channel in channelList:  
  69.     modifyChannel(channel)  
  70.   
  71. if os.path.exists('./temp'):  
  72.     shutil.rmtree('./temp')  
  73. if os.path.exists('./AndroidManifest.xml'):  
  74.     os.remove('./AndroidManifest.xml')  
  75. print '-------------------- Done --------------------'  



執行也很簡單  shell終端進入這個文件夾,執行python MakeTool.py就可以



執行後會在文件夾下生成bin文件夾,根據各個市場的簽名文件夾就在裏面



0資源分下載:http://download.csdn.net/detail/luck_apple/5108421

關於python可以參考:http://woodpecker.org.cn/abyteofpython_cn/chinese/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章