关于 android-adb
提供使用原始 adb 命令控制 Android 设备的参考,面向需要检查设备状态、操作应用或进行界面输入的开发者。内容涵盖设备选择、连接、生命周期、输入和截图等常见任务。
主要功能
- 01检查设备与屏幕状态
- 02通过 USB 或 Wi-Fi 连接
- 03启动和卸载应用
- 04执行点击与文本输入
- 05截取屏幕并导出界面层级
适用场景
- 01检查已连接的 Android 设备
- 02启动应用并操作界面
- 03向应用输入文本
- 04保存屏幕截图或 UI 层级
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ httprunner/ skills --skill android-adb安装后在 Claude Code 中输入/android-adb确认已加载。
详细介绍
Android ADB
使用原始 adb 命令控制 Android 设备的参考指南。
执行限制
- 连接了多台设备时,务必使用
adb -s <serial>。 - 执行坐标操作前,确认屏幕分辨率:
adb -s SERIAL shell wm size。 - 执行前,询问缺失的必需信息(序列号、软件包/Activity、坐标、APK 路径)。
- 失败时,提供可采取操作的标准错误信息(授权、线缆/网络、tcpip 状态)。
- 安装了 ADB Keyboard 时,输入 CJK 和特殊字符优先使用 ADB Keyboard 广播。
设备与服务
# Start ADB server
adb start-server
# Stop ADB server
adb kill-server
# List devices
adb devices -l
# Target a specific device (use when multiple devices are online)
adb -s SERIAL <command>
Wi-Fi 连接
# Enable tcpip (USB required first)
adb -s SERIAL tcpip 5555
# Get device IP
adb -s SERIAL shell ip route | grep src
# or
adb -s SERIAL shell ip addr show wlan0
# Connect over network
adb connect <ip>:5555
# Disconnect
adb disconnect <ip>:5555
设备状态
# Screen size
adb -s SERIAL shell wm size
# Current foreground app (package/activity from mCurrentFocus)
adb -s SERIAL shell dumpsys window | grep -E 'mCurrentFocus|mFocusedApp'
# Get home launcher package (for back-home detection)
adb -s SERIAL shell cmd package resolve-activity --brief -c android.intent.category.HOME
# Raw shell command
adb -s SERIAL shell <command>
应用生命周期
# Verify package installed
adb -s SERIAL shell pm list packages | grep <package>
# Launch by package (via monkey)
adb -s SERIAL shell monkey -p <package> -c android.intent.category.LAUNCHER 1
# Launch by activity
adb -s SERIAL shell am start -W -n <package>/<activity>
# Launch by URI/scheme
adb -s SERIAL shell am start -W -a android.intent.action.VIEW -d "<scheme://path>"
# Force-stop
adb -s SERIAL shell am force-stop <package>
输入操作
# Tap
adb -s SERIAL shell input tap X Y
# Double tap (two taps with short delay)
adb -s SERIAL shell input tap X Y && sleep 0.1 && adb -s SERIAL shell input tap X Y
# Long press (swipe to same point with duration)
adb -s SERIAL shell input swipe X Y X Y 3000
# Swipe
adb -s SERIAL shell input swipe X1 Y1 X2 Y2 [duration_ms]
# Key event
adb -s SERIAL shell input keyevent KEYCODE_BACK
adb -s SERIAL shell input keyevent KEYCODE_HOME
adb -s SERIAL shell input keyevent KEYCODE_ENTER
# Return to home (press BACK repeatedly, check foreground against home launcher)
for i in $(seq 1 20); do
adb -s SERIAL shell input keyevent KEYCODE_BACK
sleep 0.5
# Check if home reached by comparing current focus to home package
CURRENT=$(adb -s SERIAL shell dumpsys window | grep mCurrentFocus)
echo "Round $i: $CURRENT"
done
文本输入
# Plain text input (ASCII only, spaces escaped as %s)
adb -s SERIAL shell input text "hello%sworld"
# Input via ADB Keyboard (supports CJK and special characters)
# First ensure ADB Keyboard is active:
adb -s SERIAL shell ime set com.android.adbkeyboard/.AdbIME
# Then send text (base64 encoded):
adb -s SERIAL shell am broadcast -a ADB_INPUT_B64 --es msg "$(echo -n 'your text' | base64)"
# Clear text via ADB Keyboard
adb -s SERIAL shell am broadcast -a ADB_CLEAR_TEXT
屏幕截图与 UI 树
# Screenshot to local file
adb -s SERIAL exec-out screencap -p > screenshot.png
# Screenshot to device then pull
adb -s SERIAL shell screencap -p /sdcard/screen.png
adb -s SERIAL pull /sdcard/screen.png ./screen.png
# Dump UI hierarchy XML
adb -s SERIAL shell uiautomator dump /sdcard/window_dump.xml
adb -s SERIAL pull /sdcard/window_dump.xml ./window_dump.xml
应用安装与卸载
# Install APK (replace existing)
adb -s SERIAL install -r /path/to/app.apk
# Uninstall
adb -s SERIAL uninstall <package>
常用键码
| 键码 | 描述 |
|---|---|
KEYCODE_BACK | 返回按钮 |
KEYCODE_HOME | 主页按钮 |
KEYCODE_ENTER | 回车/确认 |
KEYCODE_DEL | 删除/退格 |
KEYCODE_VOLUME_UP | 提高音量 |
KEYCODE_VOLUME_DOWN | 降低音量 |
KEYCODE_POWER | 电源按钮 |
KEYCODE_TAB | Tab 键 |
KEYCODE_ESCAPE | Escape 键 |
评论
支持 Markdown