您的位置:首页 > 编程语言 > Python开发

android批量打包工具-python实现

2015-03-12 10:18 549 查看
所谓批量打包实质是:只需动态修改AndroidManifest.xml文件中的channel_value,添加你需要的渠道名称并重新打包成新的渠道包。

思路

1.导出一个未签名的apk包,其中渠道号配置如上图。

2.使用apkTool解压apk包

3.根据渠道号修改channel_value

4.使用apktool重新打包为未签名的apk包

5.给未签名的apk包签名

使用提示步骤:

一、下载python安装包,python 2.7.9下载地址:https://www.python.org/downloads/ 直接运行傻瓜式安装,只要稍稍注意安装的路径就可以了。

比如我是安装在 D:\Python\Python27,那么在我的电脑环境变量path中添加D:\Python\Python27;

打开cmd 并输入python可以正常查看到python的版本信息,则说明python安装成功。

二、在运行终端找到MakeTool.py的路径,并输入命令:python MakeTool.py 就可以成功进行批量打包啦

组织结构如下图:



在前人的基础上多加了aapt.exe和apktool.bat文件,如果没有aapt.exe会报jarsigner是不内部命令的提示哦。

其中channel为渠道号列表:

360

91

aliyun

android

anguanjia

appchina

gfan

goapk

hiapk

nduoa

nearme

tencent

wandoujia

xiaomi

keystore文件夹内放置着测试工程的签名文件ApkTest.keystore

ApkTest.apk是未签名的apk包

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

#!/usr/bin/python
# coding=utf-8
import os
import shutil

def readChannelfile(filename):
f = file(filename)
while True:
line = f.readline().strip('\n')
if len(line) == 0:
break
else:
channelList.append(line);
f.close()

def backUpManifest():
if os.path.exists('./AndroidManifest.xml'):
os.remove('./AndroidManifest.xml')
manifestPath = './temp/AndroidManifest.xml'
shutil.copyfile(manifestPath, './AndroidManifest.xml')

def modifyChannel(value):
tempXML = ''
f = file('./AndroidManifest.xml')
for line in f:
if line.find('channel_value') > 0:
line = line.replace('channel_value', value)
tempXML += line
f.close()

output = open('./temp/AndroidManifest.xml', 'w')
output.write(tempXML)
output.close()

unsignApk = r'./bin/%s_%s_unsigned.apk'% (easyName, value)
cmdPack = r'java -jar apktool.jar b temp %s'% (unsignApk)
os.system(cmdPack)

signedjar = r'./bin/%s_%s_signed.apk'% (easyName, value)
unsignedjar = r'./bin/%s_%s_unsigned.apk'% (easyName, value)
cmd_sign = r'jarsigner -verbose -keystore %s -storepass %s -signedjar %s %s %s'% (keystore, storepass, signedjar, unsignedjar, alianame)
os.system(cmd_sign)
os.remove(unsignedjar);

channelList = []
apkName = 'ApkTest.apk'
easyName = apkName.split('.apk')[0]
keystore='./keystore/ApkTest.keystore'
storepass='123456'
alianame='ApkTest.keystore'

output_apk_dir="./bin"
if os.path.exists(output_apk_dir):
shutil.rmtree(output_apk_dir)

readChannelfile('./channel')
print '-------------------- your channel values --------------------'
print 'channel list: ', channelList
cmdExtract = r'java -jar apktool.jar  d -f -s %s temp'% (apkName)
os.system(cmdExtract)

backUpManifest()
for channel in channelList:
modifyChannel(channel)

if os.path.exists('./temp'):
shutil.rmtree('./temp')
if os.path.exists('./AndroidManifest.xml'):
os.remove('./AndroidManifest.xml')
print '-------------------- Done --------------------'


执行也很简单 运行cmd进入终端,并切换路径到MakeTool.py的文件路径D:\batch_make_tool\batch_make_tool,执行python MakeTool.py就可以



执行后会在文件夹下生成bin文件夹,根据各个市场的签名文件夹就在里面



my_batch_apktool.zip下载:http://download.csdn.net/detail/etmanwenhan/8493739

参考:/article/7709780.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: