使用python编写脚本获取手机当前应用apk的信息

前提是已设置android_home环境变量,使用aapt工具获取apk的信息,保存至脚本所在目录下的packageinfo.txt文件中:

import os
import tempfile
import re
tempfile = tempfile.gettempdir()
def get_aapt():
if “android_home” in os.environ:
rootdir = os.path.join(os.environ[“android_home”], “build-tools”)
for path, subdir, files in os.walk(rootdir):
if “aapt.exe” in files:
return os.path.join(path, “aapt.exe”)
else:
return “android_home not exist”
def get_current_package_name():
pattern = re.compile(r”[a-za-z0-9\.]+/.[a-za-z0-9\.]+”)
os.popen(“adb wait-for-device”)
out = os.popen(“adb shell dumpsys input | findstr focusedapplication”).read()
package_name = pattern.findall(out)[0].split(“/”)[0]
return package_name
def get_match_apk(package_name):
list = []
for packages in os.popen(“adb shell pm list packages -f ” + package_name).readlines():
list.append(packages.split(“:”)[-1].split(“=”)[0])
apk_name = list[0].split(“/”)[-1]
os.popen(“adb pull ” + list[0] + ” ” + tempfile)
return tempfile + “\\” + apk_name
if __name__ == “__main__”:
os.popen(get_aapt() + \
” dump badging ” + \
get_match_apk(get_current_package_name()) + \
” > packageinfo.txt”)
os.popen(“del ” + tempfile + “\\*.apk”)

Posted in 未分类