返回手册列表
🔍
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 devicesiOS
bash
# macOS(推荐)
brew install libimobiledevice ideviceinstaller
# Python 版(跨平台,阿里出品)
pip install tidevice
# 验证
idevice_id -l
tidevice list📱
设备管理
设备管理
Androidadb devices列出已连接设备adb devices -l列出设备详情(型号/状态)adb connect <ip>:<port>无线连接设备adb disconnect断开所有无线连接adb -s <serial> shell指定设备执行 shelladb root以 root 权限重启 adbdadb remount重新挂载 /system 为可写adb reboot重启设备adb reboot recovery重启到 Recovery 模式adb reboot bootloader重启到 Bootloader/Fastboot
设备管理
iOSidevice_id -l列出已连接设备 UDIDideviceinfo查看设备详细信息ideviceinfo -k ProductType查看设备型号ideviceinfo -k DeviceName查看设备名称tidevice list列出已连接设备tidevice -u <udid> info指定设备查看信息idevicesyslog实时查看设备系统日志idevicediagnostics restart重启设备
📦
应用(App)管理
App 管理
Androidbash — 安装/卸载
# 安装
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 mResumedActivityApp 管理
iOSbash
# 安装
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📁
文件传输
文件传输
Androidadb push local.txt /sdcard/推送到设备adb push ./assets/ /sdcard/Download/推送文件夹adb pull /sdcard/photo.jpg ./从设备拉取adb shell ls /sdcard/查看存储adb shell df -h磁盘空间
文件传输
iOSafcclient pull /Photo.jpg ./拉取文件afcclient push ./test.txt /test.txt推送文件afcclient ls /Documents/查看沙盒文件afcclient ls /tmp/查看临时目录
>_
Shell 常用命令
Shell
Androidbash — 系统信息
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)
iOSbash
# 屏幕截图
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"📸
屏幕截图与录屏
截图/录屏
Androidbash
# 截图
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截图/录屏
iOSbash
# 截图
idevicescreenshot screenshot.png
tidevice screenshot screen.png
# 录屏
tidevice screenrecord record.mp4 --time 30
# macOS 可用 QuickTime 实时预览📋
日志与调试
Logcat
Androidbash
# 实时查看日志
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日志
iOSidevicesyslog实时查看日志idevicesyslog | grep -i "error"过滤错误日志idevicesyslog > ios_log.txt导出日志
性能分析
BothAndroid
adb shell top -n 1 | head -20
adb shell dumpsys cpuinfo
adb shell dumpsys meminfo com.example.appiOS 使用 Xcode Instruments 获取详细性能数据。
🔌
端口转发与调试桥
端口转发
Androidbash
# 本地端口转发
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 命令
Androidbash
# 查看已安装包
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 命令
Androidbash
# 启动 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🔧
设备信息速查
系统属性
Androidgetprop 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 架构
设备信息
iOSideviceinfo -k ProductType型号ideviceinfo -k DeviceName设备名称ideviceinfo -k ProductVersioniOS 版本ideviceinfo -k SerialNumber序列号ideviceinfo -k UniqueDeviceIDUDIDideviceinfo -k ModelNumber型号号码ideviceinfo -k RegionInfo地区版本ideviceinfo -k All查看所有信息
🤖
自动化测试辅助
UI Automator & Monkey
Androidbash — 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.AndroidJUnitRunnerbash — 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 500Appium 对接 — 关闭/恢复动画
Bothbash
# 关闭动画(提升自动化稳定性)
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+)
无线调试
Androidbash
# 开启无线调试(需在设置中手动开启)
# 设置 → 开发者选项 → 无线调试
# 获取无线调试的 IP 和端口后连接
adb pair <ip>:<pairing_port> # 配对(输入配对码)
adb connect <ip>:<connect_port> # 连接
# 查看连接状态
adb devices📋
常用场景速查表
| 场景 | Android 命令 | iOS 命令 |
|---|---|---|
| 清除 App 数据 | adb shell pm clear com.xx | 卸载重装 |
| 强制停止 App | adb shell am force-stop com.xx | idevicediagnostics 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.png | idevicescreenshot x.png |
| 录屏 | adb shell screenrecord /sdcard/x.mp4 | tidevice screenrecord x.mp4 |
| 查看日志 | adb logcat | idevicesyslog |
| 安装 App | adb install app.apk | ideviceinstaller -i app.ipa |
| 卸载 App | adb uninstall com.xx | ideviceinstaller -U com.xx |
| 查看内存 | adb shell dumpsys meminfo com.xx | idevicediagnostics |
| 远程调试 WebView | adb forward tcp:9222 localabstract:webview_devtools_remote_0 | Safari 开发者工具 |
⭐
技巧速查
ADB Server 管理
Androidadb start-server启动 ADB 服务adb kill-server停止 ADB 服务adb version查看版本
高级技巧
Bothbash
# 屏幕常亮
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/adblibimobiledevicelibimobiledevice.orgtidevice(阿里开源)github.com/nicehash/tideviceAppium 文档appium.io/docs/en/latest