-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuninstallapk.py
82 lines (70 loc) · 1.8 KB
/
uninstallapk.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
# coding:utf8
# Author:daben_chen
"""
批量卸载apk
"""
import os
import shutil
import sys
import subprocess
import zipfile
import re
import mobileinfo
def uninstall_apk(device):
"""
:params:批量卸载apk
:return:
"""
apklist = []
apkdir = "uninstallapk"
if len(sys.argv) == 1:
for f in os.listdir(apkdir):
extname = os.path.splitext(f)[1]
if extname == ".apk" :
apklist.append(f)
for apk in apklist:
print("uninstall "+apk+" on "+device)
pkgname = get_packaganame("uninstallapk" +"/"+apk)
res = os.system("adb -s " + device + " uninstall " + pkgname)
res = res>>8
if res != 0:
print("Failed to uninstall " + apk +"\n")
return False
else:
print("Success to uninstall " + apk +"\n")
elif len(sys.argv) > 1:
argvlen = len(sys.argv)
for l in range(argvlen):
if l != 0 :
apk = sys.argv[l]
print("uninstall "+apk+" on "+device)
pkgname = get_packaganame("uninstallapk" +"/"+apk)
res = os.system("adb -s " + device + " uninstall " + pkgname)
res = res>>8
if res != 0:
print("Failed to uninstall " + apk +"\n")
return False
else:
print("Success to uninstall " + apk +"\n")
def get_packaganame(apk):
"""
:params:获取应用主包名
:return:package_name
"""
zf = zipfile.ZipFile(apk, "r")
zf.extract("AndroidManifest.xml", "temp")
zf.close()
child = subprocess.Popen("java -jar AXMLPrinter2.jar temp/AndroidManifest.xml", stdout=subprocess.PIPE, shell=True)
message = child.stdout.read().decode()
reg = re.compile(r"(?<=package=)\"(.*)\"")
m = re.search(reg, message)
package_name = m.group(1)
print(package_name)
return package_name
def main():
mounted_devices = mobileinfo.find_devices()
if mounted_devices is not None:
for device in mounted_devices:
uninstall_apk(device)
if __name__ == '__main__':
main()