python运行android adb命令获取版本号

这个比较简单 主要是删除旧包 安装新包 获取版本号和md5号

如果按照之前我要在terminal里输入,点击起码5,6次

用python代码一次运行全部搞定 避免了频繁发版的生产力的浪费。

python 复制代码
import subprocess
# 第一个参数是 包名 例如 com.xxx.xxx  第二个参数是 文件在电脑上的路径
def install_app(path1,path2):
    try:
        # 执行adb命令并捕获输出
        cmd = 'adb uninstall '+ path1
        result = subprocess.run(cmd, shell=True, check=True,
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                              text=True)
        
        # 提取版本号 (格式示例: versionName=1.2.3)
        output = result.stdout.strip()
        if output == 'Success':
           print("卸载原"+path1+"包成功")
           cmd2 = 'adb install '+ path2
           result2 = subprocess.run(cmd2, shell=True, check=True,
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                              text=True)
           output2 =result2.stdout.strip()
           print("安装新apk结果:" + output2)
        return "安装失败"
        
    except subprocess.CalledProcessError as e:
        return f"命令执行失败: {e.stderr}"
    except Exception as e:
        return f"发生错误: {str(e)}"

def get_weather_app_version(path1):
    try:
        # 执行adb命令并捕获输出
        cmd = 'adb shell "dumpsys package '+path1+' | grep versionName"'
        result = subprocess.run(cmd, shell=True, check=True,
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                              text=True)
        
        # 提取版本号 (格式示例: versionName=1.2.3)
        output = result.stdout.strip()
        if output:
            version = output.split('=')[1] if '=' in output else output
            return version
        return "未找到版本信息"
        
    except subprocess.CalledProcessError as e:
        return f"命令执行失败: {e.stderr}"
    except Exception as e:
        return f"发生错误: {str(e)}"
    
# 文件在电脑上的路径
def get_app_md5(path):
    try:
        # 执行adb命令并捕获输出
        cmd = 'md5 '+ path
        result = subprocess.run(cmd, shell=True, check=True,
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                              text=True)
        
        # 提取md5 (格式示例: versionName=1.2.3)
        output = result.stdout.strip()
        if output:
            md5 = output.split('=')[1] if '=' in output else output
            return md5
        return "未找到md5"
        
    except subprocess.CalledProcessError as e:
        return f"命令执行失败: {e.stderr}"
    except Exception as e:
        return f"发生错误: {str(e)}"

if __name__ == "__main__":
    path1='com.xxx.xxx'
    path2='~/xxx/xxxxther.apk'
    print("安装新包路径:"+ path2)
    install_app(path1,path2)
    # # 提取现在的应用版本号
    version = get_weather_app_version(path1)
    print(f"应用版本: {version}")
    # # 提取md5号
    md5 = get_app_md5(path2)
    print(f"md5: {md5}")

其实也可以更精进一点把一些不需要的输出去掉或者写入csv里面 但是感觉这样也已经够用了把需要的东西都已经放进来了,先这样吧 足够用了

相关推荐
哈里谢顿2 分钟前
Python 依赖注入详解
python
冬天vs不冷30 分钟前
Java基础(九):Object核心类深度剖析
java·开发语言·python
TS的美梦31 分钟前
【1:1复刻R版】python版火山图函数一键出图
开发语言·python·r语言·scanpy·火山图
CF14年老兵42 分钟前
Python万物皆对象:从懵懂到顿悟的奇妙之旅
后端·python·trae
这里有鱼汤1 小时前
发现个用《道德经》+价值投资大咖的智慧,做A股的AI诊股神器,居然还开源了
python
陈天伟教授1 小时前
(二)Python + 地球信息科学与技术 (GeoICT)=?
开发语言·python
Kapaseker1 小时前
你一定会喜欢的 Compose 形变动画
android
之歆2 小时前
大模型微调分布式训练-大模型压缩训练(知识蒸馏)-大模型推理部署(分布式推理与量化部署)-大模型评估测试(OpenCompass)
人工智能·笔记·python
QuZhengRong2 小时前
【数据库】Navicat 导入 Excel 数据乱码问题的解决方法
android·数据库·excel
人工干智能2 小时前
pygame的帧处理中,涉及键盘的有`pg.event.get()`与`pg.key.get_pressed()` ,二者有什么区别与联系?
python·游戏·计算机外设·pygame