欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-14 08:50:59
前提是已设置android_home环境变量,使用aapt工具获取apk的信息,保存至脚本所在目录下的packageinfo.txt文件中: import os...

前提是已设置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")