-
Notifications
You must be signed in to change notification settings - Fork 113
/
ApkResigner.py
139 lines (109 loc) · 3.99 KB
/
ApkResigner.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python
#-*-coding:utf-8-*-
# /**
# * ================================================
# * 作 者:JayGoo
# * 版 本:1.0.1
# * 更新日期:2017/12/29
# * 邮 箱: [email protected]
# * ================================================
# */
import os
import sys
import config
import platform
import shutil
#获取脚本文件的当前路径
def curFileDir():
#获取脚本路径
path = sys.path[0]
#判断为脚本文件还是py2exe编译后的文件,
#如果是脚本文件,则返回的是脚本的目录,
#如果是编译后的文件,则返回的是编译后的文件路径
if os.path.isdir(path):
return path
elif os.path.isfile(path):
return os.path.dirname(path)
#判断当前系统
def isWindows():
sysstr = platform.system()
if("Windows" in sysstr):
return 1
else:
return 0
#兼容不同系统的路径分隔符
def getBackslash():
if(isWindows() == 1):
return "\\"
else:
return "/"
# 清空临时资源
def cleanTempResource():
try:
os.remove(zipalignedApkPath)
os.remove(signedApkPath)
pass
except Exception:
pass
# 清空渠道信息
def cleanChannelsFiles():
try:
os.makedirs(channelsOutputFilePath)
pass
except Exception:
pass
# 创建Channels输出文件夹
def createChannelsDir():
try:
os.makedirs(channelsOutputFilePath)
pass
except Exception:
pass
#当前脚本文件所在目录
parentPath = curFileDir() + getBackslash()
#config
libPath = parentPath + "lib" + getBackslash()
buildToolsPath = config.sdkBuildToolPath + getBackslash()
checkAndroidV2SignaturePath = libPath + "CheckAndroidV2Signature.jar"
walleChannelWritterPath = libPath + "walle-cli-all.jar"
keystorePath = config.keystorePath
keyAlias = config.keyAlias
keystorePassword = config.keystorePassword
keyPassword = config.keyPassword
channelsOutputFilePath = parentPath + "channels"
channelFilePath = parentPath +"channel"
protectedSourceApkPath = parentPath + config.protectedSourceApkName
# 检查自定义路径,并作替换
if len(config.protectedSourceApkDirPath) > 0:
protectedSourceApkPath = config.protectedSourceApkDirPath + getBackslash() + config.protectedSourceApkName
if len(config.channelsOutputFilePath) > 0:
channelsOutputFilePath = config.channelsOutputFilePath
if len(config.channelFilePath) > 0:
channelFilePath = config.channelFilePath
zipalignedApkPath = protectedSourceApkPath[0 : -4] + "_aligned.apk"
signedApkPath = zipalignedApkPath[0 : -4] + "_signed.apk"
# 创建Channels输出文件夹
createChannelsDir()
#清空Channels输出文件夹
cleanChannelsFiles()
#对齐
zipalignShell = buildToolsPath + "zipalign -v 4 " + protectedSourceApkPath + " " + zipalignedApkPath
os.system(zipalignShell)
#签名
signShell = buildToolsPath + "apksigner sign --ks "+ keystorePath + " --ks-key-alias " + keyAlias + " --ks-pass pass:" + keystorePassword + " --key-pass pass:" + keyPassword + " --out " + signedApkPath + " " + zipalignedApkPath
os.system(signShell)
print(signShell)
#检查V2签名是否正确
checkV2Shell = "java -jar " + checkAndroidV2SignaturePath + " " + signedApkPath;
os.system(checkV2Shell)
#写入渠道
if len(config.extraChannelFilePath) > 0:
writeChannelShell = "java -jar " + walleChannelWritterPath + " batch2 -f " + config.extraChannelFilePath + " " + signedApkPath + " " + channelsOutputFilePath
else:
writeChannelShell = "java -jar " + walleChannelWritterPath + " batch -f " + channelFilePath + " " + signedApkPath + " " + channelsOutputFilePath
os.system(writeChannelShell)
cleanTempResource()
print ("\n**** =============================TASK FINISHED=================================== ****\n")
print ("\n↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ Please check channels in the path ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓\n")
print ("\n"+channelsOutputFilePath+"\n")
print ("\n**** =============================TASK FINISHED=================================== ****\n")