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

相关推荐
ANYOUZHEN41 分钟前
bugku shell
android
a1117761 小时前
医院挂号预约系统(开源 Fastapi+vue2)
前端·vue.js·python·html5·fastapi
0思必得01 小时前
[Web自动化] Selenium处理iframe和frame
前端·爬虫·python·selenium·自动化·web自动化
摘星编程3 小时前
OpenHarmony + RN:Calendar日期选择功能
python
Yvonne爱编码3 小时前
JAVA数据结构 DAY3-List接口
java·开发语言·windows·python
一方_self3 小时前
了解和使用python的click命令行cli工具
开发语言·python
小芳矶4 小时前
Dify本地docker部署踩坑记录
python·docker·容器
南宫码农4 小时前
我的电视 - Android原生电视直播软件 完整使用教程
android·开发语言·windows·电视盒子
道亦无名4 小时前
音频数据特征值提取 方法和步骤
android·音视频
Lancker4 小时前
定制侠 一个国产纯血鸿蒙APP的诞生过程
android·华为·智能手机·鸿蒙·国产操作系统·纯血鸿蒙·华为鸿蒙