Skip to content

uiautomator2

webdesc
github

安装adb

  • Windows:通过 ScoopChocolatey 安装:

    powershell
    scoop install adb
    # 或
    choco install adb
  • macOS/Linux:用包管理器安装:

    bash
    # macOS(Homebrew)
    brew install android-platform-tools
    
    # Linux(Debian/Ubuntu)
    sudo apt install adb

验证安装

安装完成后,重启终端并运行:

bash
adb --version

通过 WiFi 连接设备的步骤

先用 USB 线连接手机(首次配置)

  1. 开启 USB 调试

    • 手机进入 开发者选项 → 启用 USB 调试
  2. 通过 USB 初始化 ADB over TCP

    sh
    adb -s 127.0.0.1:62025 tcpip 5555   # 将 ADB 切换到 TCP 模式(端口默认 5555)
  3. 查询手机 IP 地址

    在手机 设置 → 关于手机 → 状态信息 中查看 IP 地址(或执行命令):

    sh
    adb shell ip route | awk '{print $9}'  # 获取手机局域网 IP
  4. 拔掉 USB 线,通过 WiFi 连接

    sh
    adb connect <手机IP>:5555  # 例如 adb connect 192.168.1.100:5555
  5. 验证连接

    sh
    adb devices

    输出应包含:

    List of devices attached
    192.168.1.100:5555    device
  6. 查看包名

    sh
    adb shell pm list packages
    
    # or
    adb -s 127.0.0.1:62001 shell
    cd /data/app
    ls -al
  7. aapt安装

    sh
    apt install aapt -y
    
    # 显示包信息
    aapt dump badging aap.apk

在 uiautomator2 中使用 WiFi 连接

安装

sh
pip install uiautomator2

#初始化,安装uiautomator-server,atx-agent atx-agent
 python -m uiautomator2 init

操作

python
import uiautomator2 as u2

# 方式1:直接通过 IP 连接
d = u2.connect("192.168.1.100:5555")  # 替换为你的手机 IP

# 方式2:使用设备序列号(需提前通过 USB 初始化)
d = u2.connect("R58MB0PRW8N")  # 序列号需在 `adb devices` 中可见

d.app_start("com.example.app")  # 测试操作

weditro

安装

sh
pip install weditor