使用python编写脚本获取手机当前应用apk的信息
前提是已设置ANDROID_HOME环境变量,使用aapt工具获取apk的信息,保存至脚本所在目录下的PackageInfo.txt文件中:
importos importtempfile importre tempFile=tempfile.gettempdir() defget_aapt(): if"ANDROID_HOME"inos.environ: rootDir=os.path.join(os.environ["ANDROID_HOME"],"build-tools") forpath,subdir,filesinos.walk(rootDir): if"aapt.exe"infiles: returnos.path.join(path,"aapt.exe") else: return"ANDROID_HOMEnotexist" defget_current_package_name(): pattern=re.compile(r"[a-zA-Z0-9\.]+/.[a-zA-Z0-9\.]+") os.popen("adbwait-for-device") out=os.popen("adbshelldumpsysinput|findstrFocusedApplication").read() package_name=pattern.findall(out)[0].split("/")[0] returnpackage_name defget_match_apk(package_name): list=[] forpackagesinos.popen("adbshellpmlistpackages-f"+package_name).readlines(): list.append(packages.split(":")[-1].split("=")[0]) apk_name=list[0].split("/")[-1] os.popen("adbpull"+list[0]+""+tempFile) returntempFile+"\\"+apk_name if__name__=="__main__": os.popen(get_aapt()+\ "dumpbadging"+\ get_match_apk(get_current_package_name())+\ ">PackageInfo.txt") os.popen("del"+tempFile+"\\*.apk")