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

yipeiwu_com6年前Python基础

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

相关文章

win10环境下配置vscode python开发环境的教程详解

win10环境下配置vscode python开发环境的教程详解

前言 VScode是一个相当优秀的IDE,具备开源、跨平台、模块化、插件丰富、启动时间快、颜值高、可高度定制等等优秀的特质,不愧是微软爸爸的私生子。 所以用VScode来编写Pyth...

python的格式化输出(format,%)实例详解

皇城PK Python中格式化字符串目前有两种阵营:%和format,我们应该选择哪种呢? 自从Python2.6引入了format这个格式化字符串的方法之后,我认为%还是format这...

Python通过正则表达式选取callback的方法

本文实例讲述了Python通过正则表达式选取callback的方法。分享给大家供大家参考。具体如下: 最近在瞎想怎么通过xpath去精确抓取文章的正文,跟parselets类似的想法,只...

Python文件操作函数用法实例详解

这篇文章主要介绍了Python文件操作函数用法实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 字符编码 二进制和字符之间的转换...

pandas中遍历dataframe的每一个元素的实现

假如有一个需求场景需要遍历一个csv或excel中的每一个元素,判断这个元素是否含有某个关键字 那么可以用python的pandas库来实现。 方法一: pandas的dataframe...