apk_签名_多渠道打包_Python脚本

这个是Python的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import zipfile
import shutil
import os
###############################
# Describe : 签名、渠道号
# D&P Author By: LiQiong
# Create Date: 2016-04-22
# Modify Date: 2016-04-22
###############################

src_apk_name="LoginActivity.apk" #未签名的apk
channel_file='channel.txt' #渠道号配置文件

signed_apk_name='signed_'+src_apk_name;

def signedApk():
print 'signeding..................'
command='jarsigner -verbose -keystore yourkey.keystore -storepass yourStorepass -keypass yourKeypass -signedjar '+signed_apk_name+' '+src_apk_name+' your.keystore -digestalg SHA1 -sigalg MD5withRSA'
os.system(command)
print 'singed complete!'

def MarkChannelApk(target_apk_name,channel_name):
zipped = zipfile.ZipFile(target_apk_name, 'a', zipfile.ZIP_DEFLATED)
empty_channel_file = "META-INF/mtchannel_{channel}".format(channel=channel_name)
zipped.write(empty_file, empty_channel_file)
zipped.close()

signedApk()

empty_file='empty.txt'
open(empty_file,'w').close()


f=open(channel_file)
lines=f.readlines()
f.close()
for x in lines:
x=x.strip('\n')
target_apk_name=x+'_'+signed_apk_name
print signed_apk_name+'===='+target_apk_name
shutil.copy(signed_apk_name,x+'_'+signed_apk_name)
MarkChannelApk(target_apk_name,x)

这个是java获取channel的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
private String getChannel(Context context) {
//从apk包中获取
ApplicationInfo appinfo = context.getApplicationInfo();
String sourceDir = appinfo.sourceDir;
//注意这里:默认放在meta-inf/里, 所以需要再拼接一下
String key = "META-INF/mtchannel" ;
String ret = "";
ZipFile zipfile = null;
try {
zipfile = new ZipFile(sourceDir);
Enumeration<?> entries = zipfile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
String entryName = entry.getName();
if (entryName.startsWith(key)) {
ret = entryName;
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (zipfile != null) {
try {
zipfile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String[] split = ret.split("_");
String channel = "";
if (split != null && split.length >= 2) {
channel = ret.substring(split[0].length() + 1);
}
return channel;
}

原理待添加,速度非常的快。