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

相关推荐
A__tao3 小时前
Elasticsearch Mapping 一键生成 Java 实体类(支持嵌套 + 自动过滤注释)
java·python·elasticsearch
研究点啥好呢3 小时前
Github热门项目推荐 | 创建你的像素风格!
c++·python·node.js·github·开源软件
SharpCJ3 小时前
Android 开发者为什么必须掌握 AI 能力?端侧视角下的技术变革
android·ai·aigc
迷藏4943 小时前
**发散创新:基于Rust实现的开源合规权限管理框架设计与实践**在现代软件架构中,**权限控制(RBAC)** 已成为保障
java·开发语言·python·rust·开源
明日清晨4 小时前
python扫码登录dy
开发语言·python
_李小白4 小时前
【OSG学习笔记】Day 38: TextureVisitor(纹理访问器)
android·笔记·学习
JJay.4 小时前
Kotlin 高阶函数学习指南
android·开发语言·kotlin
bazhange4 小时前
python如何像matlab一样使用向量化替代for循环
开发语言·python·matlab
jinanwuhuaguo4 小时前
截止到4月8日,OpenClaw 2026年4月更新深度解读剖析:从“能力回归”到“信任内建”的范式跃迁
android·开发语言·人工智能·深度学习·kotlin
人工干智能4 小时前
科普:python中你写的模块找不到了——`ModuleNotFoundError`
服务器·python