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

相关推荐
Austin_YB1 分钟前
VScode中配置Python环境
ide·vscode·python
qq_452396232 分钟前
【Python × AI】LangChain 深度剖析:从组件解耦到 LCEL 的逻辑美学
人工智能·python·ai·langchain
ChineHe3 分钟前
基础篇003_Python基础语法
开发语言·人工智能·python
oem1104 分钟前
Python Web爬虫入门:使用Requests和BeautifulSoup
jvm·数据库·python
CSDN_Colinw14 分钟前
Python GUI开发:Tkinter入门教程
jvm·数据库·python
chase。20 分钟前
Python包构建工具完全指南:python -m build 使用详解
开发语言·chrome·python
xin_yao_xin25 分钟前
PaddleOCR系列——《文本检测、文本识别》模型训练
人工智能·python·paddlepaddle·ppocr
2401_8331977325 分钟前
用Python制作一个文字冒险游戏
jvm·数据库·python
耶叶26 分钟前
Android 新权限申请模型(Activity Result API)
android
阿拉斯攀登31 分钟前
【RK3576 安卓 JNI/NDK 系列 04】JNI 核心语法(下):字符串、数组与对象操作
android·驱动开发·rk3568·瑞芯微·rk安卓驱动·jni字符串操作