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里面 但是感觉这样也已经够用了把需要的东西都已经放进来了,先这样吧 足够用了

相关推荐
我的xiaodoujiao10 分钟前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 38--Allure 测试报告
python·学习·测试工具·pytest
TA远方1 小时前
【Android】adb常用的命令用法详解
android·adb·管理·控制·命令
沈浩(种子思维作者)6 小时前
真的能精准医疗吗?癌症能提前发现吗?
人工智能·python·网络安全·健康医疗·量子计算
njsgcs7 小时前
ue python二次开发启动教程+ 导入fbx到指定文件夹
开发语言·python·unreal engine·ue
io_T_T7 小时前
迭代器 iteration、iter 与 多线程 concurrent 交叉实践(详细)
python
华研前沿标杆游学8 小时前
2026年走进洛阳格力工厂参观游学
python
Carl_奕然8 小时前
【数据挖掘】数据挖掘必会技能之:A/B测试
人工智能·python·数据挖掘·数据分析
AI小怪兽8 小时前
基于YOLOv13的汽车零件分割系统(Python源码+数据集+Pyside6界面)
开发语言·python·yolo·无人机
wszy18098 小时前
新文章标签:让用户一眼发现最新内容
java·python·harmonyos
Eric.Lee20218 小时前
python实现 mp4转gif文件
开发语言·python·手势识别·手势交互·手势建模·xr混合现实