seanwalter
返回手册列表
🔍

ADB 命令使用手册

Android ADB + iOS libimobiledevice / tidevice 完整参考 — 测试工程师必备设备操控指南

16 章节120+ 命令Android & iOS12 测试场景

环境准备

Android
bash
# 下载 platform-tools 加入 PATH
# https://developer.android.com/studio/releases/platform-tools

adb version
adb devices
iOS
bash
# macOS(推荐)
brew install libimobiledevice ideviceinstaller

# Python 版(跨平台,阿里出品)
pip install tidevice

# 验证
idevice_id -l
tidevice list
📱

设备管理

设备管理
Android
  • adb devices列出已连接设备
  • adb devices -l列出设备详情(型号/状态)
  • adb connect <ip>:<port>无线连接设备
  • adb disconnect断开所有无线连接
  • adb -s <serial> shell指定设备执行 shell
  • adb root以 root 权限重启 adbd
  • adb remount重新挂载 /system 为可写
  • adb reboot重启设备
  • adb reboot recovery重启到 Recovery 模式
  • adb reboot bootloader重启到 Bootloader/Fastboot
设备管理
iOS
  • idevice_id -l列出已连接设备 UDID
  • ideviceinfo查看设备详细信息
  • ideviceinfo -k ProductType查看设备型号
  • ideviceinfo -k DeviceName查看设备名称
  • tidevice list列出已连接设备
  • tidevice -u <udid> info指定设备查看信息
  • idevicesyslog实时查看设备系统日志
  • idevicediagnostics restart重启设备
📦

应用(App)管理

App 管理
Android
bash — 安装/卸载
# 安装
adb install app.apk
adb install -r app.apk         # 覆盖安装(保留数据)
adb install -t app.apk         # 允许测试包
adb install -d app.apk         # 降级安装

# 卸载
adb uninstall com.example.app
adb uninstall -k com.example.app  # 卸载但保留数据
bash — 查询/启动
adb shell pm list packages -3    # 第三方包
adb shell pm path com.tencent.mm # 查看包路径
adb shell pm clear com.example.app # 清除数据
adb shell am force-stop com.example.app
adb shell am start -n com.example.app/.MainActivity
adb shell dumpsys activity activities | grep mResumedActivity
App 管理
iOS
bash
# 安装
ideviceinstaller -i com.example.app.ipa
tidevice install app.ipa

# 卸载
ideviceinstaller -U com.example.app
tidevice uninstall com.example.app

# 列出已安装
ideviceinstaller -l
tidevice applist

# 启动 / 停止
idevicediagnostics launch com.example.app
idevicediagnostics terminate com.example.app
📁

文件传输

文件传输
Android
  • adb push local.txt /sdcard/推送到设备
  • adb push ./assets/ /sdcard/Download/推送文件夹
  • adb pull /sdcard/photo.jpg ./从设备拉取
  • adb shell ls /sdcard/查看存储
  • adb shell df -h磁盘空间
文件传输
iOS
  • afcclient pull /Photo.jpg ./拉取文件
  • afcclient push ./test.txt /test.txt推送文件
  • afcclient ls /Documents/查看沙盒文件
  • afcclient ls /tmp/查看临时目录
>_

Shell 常用命令

Shell
Android
bash — 系统信息
adb shell getprop ro.build.version.release  # Android 版本
adb shell getprop ro.product.model          # 型号
adb shell getprop ro.serialno               # 序列号
adb shell cat /proc/cpuinfo                 # CPU 信息
adb shell cat /proc/meminfo                 # 内存信息
adb shell dumpsys battery                   # 电池信息
adb shell wm size                           # 屏幕分辨率
adb shell wm density                        # 屏幕密度
bash — 输入操作
adb shell input text "hello"            # 输入文本
adb shell input keyevent 26             # 电源键
adb shell input keyevent 3              # HOME 键
adb shell input keyevent 4              # 返回键
adb shell input keyevent 66             # 回车键
adb shell input swipe 300 500 300 100   # 滑动
adb shell input tap 500 800             # 点击坐标
Shell(tidevice)
iOS
bash
# 屏幕截图
tidevice screenshot screen.png

# 录屏(需开发者模式)
tidevice screenrecord screen.mp4

# 屏幕操作
tidevice tap 200 400
tidevice swipe 200 500 200 100
tidevice longpress 200 400
tidevice text "hello"
📸

屏幕截图与录屏

截图/录屏
Android
bash
# 截图
adb shell screencap -p /sdcard/screenshot.png
adb pull /sdcard/screenshot.png

# 一键截图到本地
adb shell screencap -p > screenshot.png

# 录屏(最长 180 秒)
adb shell screenrecord /sdcard/record.mp4 --time-limit 30
adb shell screenrecord /sdcard/record.mp4 --size 720x1280
adb shell screenrecord /sdcard/record.mp4 --bit-rate 4000000

# 结束录屏:Ctrl+C
截图/录屏
iOS
bash
# 截图
idevicescreenshot screenshot.png
tidevice screenshot screen.png

# 录屏
tidevice screenrecord record.mp4 --time 30

# macOS 可用 QuickTime 实时预览
📋

日志与调试

Logcat
Android
bash
# 实时查看日志
adb logcat

# 过滤特定标签
adb logcat -s ActivityManager:I *:S
adb logcat | grep -i "error"
adb logcat | grep -i "crash"

# 清除日志
adb logcat -c

# 输出到文件
adb logcat -f /sdcard/logcat.txt

# 按优先级过滤
adb logcat *:E          # 只看 error 及以上
adb logcat *:W          # 只看 warn 及以上

# 查看崩溃日志
adb logcat -b crash
adb logcat -b main -b system -b crash

# 查看 ANR 日志
adb shell cat /data/anr/traces.txt
日志
iOS
  • idevicesyslog实时查看日志
  • idevicesyslog | grep -i "error"过滤错误日志
  • idevicesyslog > ios_log.txt导出日志
性能分析
Both
Android
adb shell top -n 1 | head -20
adb shell dumpsys cpuinfo
adb shell dumpsys meminfo com.example.app

iOS 使用 Xcode Instruments 获取详细性能数据。

🔌

端口转发与调试桥

端口转发
Android
bash
# 本地端口转发
adb forward tcp:8080 tcp:8080
adb forward tcp:9222 localabstract:webview_devtools_remote_0

# 查看所有转发
adb forward --list
adb forward --remove tcp:8080
adb forward --remove-all

# 反向转发(设备访问电脑)
adb reverse tcp:8080 tcp:3000
adb reverse --list
adb reverse --remove-all

使用场景:设备上的 App 调试电脑上的 Web 页面。Chrome DevTools 远程调试流程:开启 USB 调试 → forward 9222 端口 → 电脑 Chrome 打开 chrome://inspect

📦

Package Manager(包管理)

PM 命令
Android
bash
# 查看已安装包
adb shell pm list packages -f     # 包名 + 路径
adb shell pm list packages -3     # 第三方包
adb shell pm list packages -s     # 系统包
adb shell pm list packages -d     # 禁用的包
adb shell pm list packages -e     # 启用的包

# 启用/禁用
adb shell pm disable-user com.example.app
adb shell pm enable com.example.app

# 查看包详情
adb shell dumpsys package com.example.app

# 权限管理
adb shell pm list permissions
adb shell pm list permissions -g

# 清除缓存
adb shell pm clear com.example.app

# APK 信息
aapt dump badging app.apk | head -20
🚀

Activity Manager(活动管理)

AM 命令
Android
bash
# 启动 Activity
adb shell am start -n com.example.app/.MainActivity
adb shell am start -a android.intent.action.VIEW -d "https://example.com"
adb shell am start -n com.android.settings/.Settings

# 带参数启动
adb shell am start -n com.example.app/.MainActivity --es key "value"
adb shell am start -n com.example.app/.MainActivity --ei count 42
adb shell am start -n com.example.app/.MainActivity --ez flag true

# 强制停止 / 杀进程
adb shell am force-stop com.example.app
adb shell am kill com.example.app
adb shell am kill-all

# 发送广播
adb shell am broadcast -a com.example.ACTION --es data "test"

# 查看当前 Activity
adb shell dumpsys activity activities | grep mResumedActivity
🔧

设备信息速查

系统属性
Android
  • getprop ro.product.model型号
  • getprop ro.product.brand品牌
  • getprop ro.product.manufacturer制造商
  • getprop ro.serialno序列号
  • getprop ro.build.display.id构建号
  • getprop ro.build.version.releaseAndroid 版本
  • getprop ro.build.version.sdkSDK 版本
  • getprop ro.product.cpu.abiCPU 架构
设备信息
iOS
  • ideviceinfo -k ProductType型号
  • ideviceinfo -k DeviceName设备名称
  • ideviceinfo -k ProductVersioniOS 版本
  • ideviceinfo -k SerialNumber序列号
  • ideviceinfo -k UniqueDeviceIDUDID
  • ideviceinfo -k ModelNumber型号号码
  • ideviceinfo -k RegionInfo地区版本
  • ideviceinfo -k All查看所有信息
🤖

自动化测试辅助

UI Automator & Monkey
Android
bash — UI Automator
# 导出 UI 层级(用于 UI 自动化分析)
adb shell uiautomator dump /sdcard/ui.xml
adb pull /sdcard/ui.xml

# 查看 UI 元素
adb shell cat /sdcard/ui.xml | grep -o 'text="[^"]*"'
adb shell cat /sdcard/ui.xml | grep -o 'resource-id="[^"]*"'

# 运行 UI Automator 测试
adb shell am instrument -w com.example.test/android.support.test.runner.AndroidJUnitRunner
bash — Monkey 测试
# 基本随机测试
adb shell monkey -p com.example.app -v 1000

# 指定事件数和延迟
adb shell monkey -p com.example.app --throttle 500 -v -v 5000

# 伪随机种子(可复现)
adb shell monkey -p com.example.app -s 12345 -v 1000

# 忽略崩溃和超时
adb shell monkey -p com.example.app --ignore-crashes --ignore-timeouts -v 1000

# 触摸事件比例调整
adb shell monkey -p com.example.app --pct-touch 40 --pct-motion 25 -v 500
Appium 对接 — 关闭/恢复动画
Both
bash
# 关闭动画(提升自动化稳定性)
adb shell settings put global window_animation_scale 0
adb shell settings put global transition_animation_scale 0
adb shell settings put global animator_duration_scale 0

# 恢复动画
adb shell settings put global window_animation_scale 1
adb shell settings put global transition_animation_scale 1
adb shell settings put global animator_duration_scale 1
📶

无线调试(Android 11+)

无线调试
Android
bash
# 开启无线调试(需在设置中手动开启)
# 设置 → 开发者选项 → 无线调试

# 获取无线调试的 IP 和端口后连接
adb pair <ip>:<pairing_port>    # 配对(输入配对码)
adb connect <ip>:<connect_port>  # 连接

# 查看连接状态
adb devices
📋

常用场景速查表

场景Android 命令iOS 命令
清除 App 数据adb shell pm clear com.xx卸载重装
强制停止 Appadb shell am force-stop com.xxidevicediagnostics terminate com.xx
模拟返回键adb shell input keyevent 4不直接支持
模拟 HOME 键adb shell input keyevent 3不直接支持
模拟音量键adb shell input keyevent 24/25不直接支持
截图adb shell screencap -p > x.pngidevicescreenshot x.png
录屏adb shell screenrecord /sdcard/x.mp4tidevice screenrecord x.mp4
查看日志adb logcatidevicesyslog
安装 Appadb install app.apkideviceinstaller -i app.ipa
卸载 Appadb uninstall com.xxideviceinstaller -U com.xx
查看内存adb shell dumpsys meminfo com.xxidevicediagnostics
远程调试 WebViewadb forward tcp:9222 localabstract:webview_devtools_remote_0Safari 开发者工具

技巧速查

ADB Server 管理
Android
  • adb start-server启动 ADB 服务
  • adb kill-server停止 ADB 服务
  • adb version查看版本
高级技巧
Both
bash
# 屏幕常亮
adb shell svc power stayon true

# 关闭屏幕常亮
adb shell svc power stayon usb

# 设置屏幕超时(毫秒)
adb shell settings put system screen_off_timeout 60000

# 切换输入法
adb shell ime list -a
adb shell ime set com.example.ime/.ImeService

# 批量安装 APK
for f in *.apk; do adb install "$f"; done

# 批量卸载
for pkg in com.app1 com.app2 com.app3; do adb uninstall "$pkg"; done
🚨

常见问题排查

adb devices 空
检查 USB 连接 + 开启 USB 调试 + 重新插拔数据线
unauthorized
手机上点击「允许 USB 调试」弹窗
device offline
adb kill-server && adb start-server
insufficient storage
adb shell pm clear com.xx 清缓存
INSTALL_FAILED_ALREADY_EXISTS
adb install -r 覆盖安装
INSTALL_FAILED_VERSION_DOWNGRADE
adb install -d 允许降级安装
Permission denied
adb root 获取 root 权限
iOS 设备不可见
确认「信任此电脑」+ 重装 libimobiledevice
tidevice 找不到设备
确认 USB 信任 + tidevice list 检查
📖

参考文档

  • Android ADB 官方文档developer.android.com/studio/command-line/adb
  • libimobiledevicelibimobiledevice.org
  • tidevice(阿里开源)github.com/nicehash/tidevice
  • Appium 文档appium.io/docs/en/latest