-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportActivities.py
More file actions
137 lines (125 loc) · 4.09 KB
/
exportActivities.py
File metadata and controls
137 lines (125 loc) · 4.09 KB
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
import subprocess, os, tempfile, shutil, sys
import xml.etree.ElementTree as ET
XML_NS = {'android':'http://schemas.android.com/apk/res/android'}
class GlobalConfigs:
def __init__(self):
self.APK_NAME=""
self.ADK_ROOT=""
self.ZIPALIGN_PATH = '/build-tools/27.0.0/zipalign'
self.APKSIGNER_PATH = '/build-tools/27.0.0/apksigner'
self.KEYSTORE_PATH = ""
self.APKTOOL_PATH=""
self.OUTPUT_PATH=""
self.KEEP_DECODE=False
def fatalError(str):
print(str)
sys.exit(1)
pass
def unpackApk(apkFile, unpackDir):
callList = ['java',\
'-jar',\
'apktool.jar',\
'd', apkFile,\
'-o', unpackDir,\
'-f']
ret = subprocess.call(callList, stdout = None, stderr = None)
if ret != 0:
fatalError("APK unpack error")
pass
def packApk(apkFile, unpackDir):
callList = ['java',\
'-jar',\
'apktool.jar',\
'b', unpackDir,\
'-o', apkFile,\
'-f']
ret = subprocess.call(callList, stdout = None, stderr = None)
if ret != 0:
fatalError("APK repack error")
pass
def signApk(apkFileUnsign, apkFileSign, configs):
if os.access(apkFileSign, os.F_OK):
os.remove(apkFileSign)
callList = [configs.ZIPALIGN_PATH, \
'-v',\
'-p',\
'4', \
apkFileUnsign,\
apkFileSign]
ret = subprocess.call(callList, stdout = None, stderr = None)
if ret != 0:
fatalError("APK zipalign error");
shutil.move(apkFileSign, apkFileUnsign)
callList = [configs.APKSIGNER_PATH, \
'sign',\
'--ks',\
configs.KEYSTORE_PATH,\
'--ks-pass',\
'pass:android',\
'--out',\
apkFileSign,\
apkFileUnsign]
ret = subprocess.call(callList, stdout = None, stderr = None)
if ret != 0:
fatalError("APK signing failed")
pass
def patchManifest(manifestFile):
ET.register_namespace('android', XML_NS['android'])
xmlTree = ET.parse(manifestFile)
root = xmlTree.getroot()
for applicationNode in root.iter('application'):
#if '{' + XML_NS['android'] + '}' + 'debuggable' not in activityNode.attrib:
applicationNode.set('{' + XML_NS['android'] + '}' + "debuggable", "true")
for activityNode in root.iter('activity'):
if '{' + XML_NS['android'] + '}' + 'exported' not in activityNode.attrib:
activityNode.set('{' + XML_NS['android'] + '}' + "exported", "true")
if '{' + XML_NS['android'] + '}' + 'permission' in activityNode.attrib:
del activityNode.attrib['{' + XML_NS['android'] + '}' + 'permission']
xmlTree.write(manifestFile)
pass
def parseMainParam():
params = sys.argv
homeDir = os.path.expanduser('~')
configs = GlobalConfigs()
if 'ANDROID_HOME' in os.environ:
configs.ADK_ROOT = os.environ['ANDROID_HOME']
elif 'ADK' in os.environ:
configs.ADK_ROOT = os.environ['ADK']
configs.KEYSTORE_PATH = homeDir + "/.android/debug.keystore"
if len(params) == 1:
exit(-1)
i = 0
while i < len(params) - 1:
i += 1
var = params[i]
if var == "-o" or var == "--out":
i += 1
configs.OUTPUT_PATH = params[i]
continue
if (var[-4:] == ".apk") and (configs.APK_NAME == ""):
configs.APK_NAME = var
continue
pass
if len(configs.ADK_ROOT) == 0:
fatalError("Missing Android SDK location information")
if len(configs.APK_NAME) == 0:
fatalError("Missing APK location")
if len(configs.OUTPUT_PATH) == 0:
fatalError("Missing Output location")
configs.ZIPALIGN_PATH = configs.ADK_ROOT + configs.ZIPALIGN_PATH
configs.APKSIGNER_PATH = configs.ADK_ROOT + configs.APKSIGNER_PATH
return configs
def main():
configs = parseMainParam()
unpackDir = tempfile.mkdtemp()
apkDir = tempfile.mkdtemp()
unpackApk(configs.APK_NAME, unpackDir)
patchManifest(unpackDir + "/AndroidManifest.xml")
packApk(apkDir + "/packedApk.apk", unpackDir)
signApk(apkDir + "/packedApk.apk", configs.OUTPUT_PATH, configs)
shutil.rmtree(unpackDir)
shutil.rmtree(apkDir)
print("Done")
pass
if __name__ == "__main__":
main()