|
| 1 | +--- |
| 2 | +name: android-adb |
| 3 | +description: Android device control via raw ADB commands. Use for device/emulator discovery, USB or Wi-Fi connection, app launch/force-stop, tap/swipe/keyevent/text input, screenshots, UI hierarchy dump, and ADB troubleshooting. |
| 4 | +--- |
| 5 | + |
| 6 | +# Android ADB |
| 7 | + |
| 8 | +Reference for controlling Android devices with raw `adb` commands. |
| 9 | + |
| 10 | +## Execution Constraints |
| 11 | + |
| 12 | +- Use `adb -s <serial>` whenever more than one device is connected. |
| 13 | +- Confirm screen resolution before coordinate actions: `adb -s SERIAL shell wm size`. |
| 14 | +- Ask for missing required inputs before executing (serial, package/activity, coordinates, APK path). |
| 15 | +- Surface actionable stderr on failure (authorization, cable/network, tcpip state). |
| 16 | +- Prefer ADB Keyboard broadcast for CJK and special character input when ADB Keyboard is installed. |
| 17 | + |
| 18 | +## Device And Server |
| 19 | + |
| 20 | +```bash |
| 21 | +# Start ADB server |
| 22 | +adb start-server |
| 23 | + |
| 24 | +# Stop ADB server |
| 25 | +adb kill-server |
| 26 | + |
| 27 | +# List devices |
| 28 | +adb devices -l |
| 29 | + |
| 30 | +# Target a specific device (use when multiple devices are online) |
| 31 | +adb -s SERIAL <command> |
| 32 | +``` |
| 33 | + |
| 34 | +## Wi-Fi Connection |
| 35 | + |
| 36 | +```bash |
| 37 | +# Enable tcpip (USB required first) |
| 38 | +adb -s SERIAL tcpip 5555 |
| 39 | + |
| 40 | +# Get device IP |
| 41 | +adb -s SERIAL shell ip route | grep src |
| 42 | +# or |
| 43 | +adb -s SERIAL shell ip addr show wlan0 |
| 44 | + |
| 45 | +# Connect over network |
| 46 | +adb connect <ip>:5555 |
| 47 | + |
| 48 | +# Disconnect |
| 49 | +adb disconnect <ip>:5555 |
| 50 | +``` |
| 51 | + |
| 52 | +## Device State |
| 53 | + |
| 54 | +```bash |
| 55 | +# Screen size |
| 56 | +adb -s SERIAL shell wm size |
| 57 | + |
| 58 | +# Current foreground app (package/activity from mCurrentFocus) |
| 59 | +adb -s SERIAL shell dumpsys window | grep -E 'mCurrentFocus|mFocusedApp' |
| 60 | + |
| 61 | +# Get home launcher package (for back-home detection) |
| 62 | +adb -s SERIAL shell cmd package resolve-activity --brief -c android.intent.category.HOME |
| 63 | + |
| 64 | +# Raw shell command |
| 65 | +adb -s SERIAL shell <command> |
| 66 | +``` |
| 67 | + |
| 68 | +## App Lifecycle |
| 69 | + |
| 70 | +```bash |
| 71 | +# Verify package installed |
| 72 | +adb -s SERIAL shell pm list packages | grep <package> |
| 73 | + |
| 74 | +# Launch by package (via monkey) |
| 75 | +adb -s SERIAL shell monkey -p <package> -c android.intent.category.LAUNCHER 1 |
| 76 | + |
| 77 | +# Launch by activity |
| 78 | +adb -s SERIAL shell am start -W -n <package>/<activity> |
| 79 | + |
| 80 | +# Launch by URI/scheme |
| 81 | +adb -s SERIAL shell am start -W -a android.intent.action.VIEW -d "<scheme://path>" |
| 82 | + |
| 83 | +# Force-stop |
| 84 | +adb -s SERIAL shell am force-stop <package> |
| 85 | +``` |
| 86 | + |
| 87 | +## Input Actions |
| 88 | + |
| 89 | +```bash |
| 90 | +# Tap |
| 91 | +adb -s SERIAL shell input tap X Y |
| 92 | + |
| 93 | +# Double tap (two taps with short delay) |
| 94 | +adb -s SERIAL shell input tap X Y && sleep 0.1 && adb -s SERIAL shell input tap X Y |
| 95 | + |
| 96 | +# Long press (swipe to same point with duration) |
| 97 | +adb -s SERIAL shell input swipe X Y X Y 3000 |
| 98 | + |
| 99 | +# Swipe |
| 100 | +adb -s SERIAL shell input swipe X1 Y1 X2 Y2 [duration_ms] |
| 101 | + |
| 102 | +# Key event |
| 103 | +adb -s SERIAL shell input keyevent KEYCODE_BACK |
| 104 | +adb -s SERIAL shell input keyevent KEYCODE_HOME |
| 105 | +adb -s SERIAL shell input keyevent KEYCODE_ENTER |
| 106 | + |
| 107 | +# Return to home (press BACK repeatedly, check foreground against home launcher) |
| 108 | +for i in $(seq 1 20); do |
| 109 | + adb -s SERIAL shell input keyevent KEYCODE_BACK |
| 110 | + sleep 0.5 |
| 111 | + # Check if home reached by comparing current focus to home package |
| 112 | + CURRENT=$(adb -s SERIAL shell dumpsys window | grep mCurrentFocus) |
| 113 | + echo "Round $i: $CURRENT" |
| 114 | +done |
| 115 | +``` |
| 116 | + |
| 117 | +## Text Input |
| 118 | + |
| 119 | +```bash |
| 120 | +# Plain text input (ASCII only, spaces escaped as %s) |
| 121 | +adb -s SERIAL shell input text "hello%sworld" |
| 122 | + |
| 123 | +# Input via ADB Keyboard (supports CJK and special characters) |
| 124 | +# First ensure ADB Keyboard is active: |
| 125 | +adb -s SERIAL shell ime set com.android.adbkeyboard/.AdbIME |
| 126 | +# Then send text (base64 encoded): |
| 127 | +adb -s SERIAL shell am broadcast -a ADB_INPUT_B64 --es msg "$(echo -n 'your text' | base64)" |
| 128 | + |
| 129 | +# Clear text via ADB Keyboard |
| 130 | +adb -s SERIAL shell am broadcast -a ADB_CLEAR_TEXT |
| 131 | +``` |
| 132 | + |
| 133 | +## Screenshot And UI Tree |
| 134 | + |
| 135 | +```bash |
| 136 | +# Screenshot to local file |
| 137 | +adb -s SERIAL exec-out screencap -p > screenshot.png |
| 138 | + |
| 139 | +# Screenshot to device then pull |
| 140 | +adb -s SERIAL shell screencap -p /sdcard/screen.png |
| 141 | +adb -s SERIAL pull /sdcard/screen.png ./screen.png |
| 142 | + |
| 143 | +# Dump UI hierarchy XML |
| 144 | +adb -s SERIAL shell uiautomator dump /sdcard/window_dump.xml |
| 145 | +adb -s SERIAL pull /sdcard/window_dump.xml ./window_dump.xml |
| 146 | +``` |
| 147 | + |
| 148 | +## App Install And Uninstall |
| 149 | + |
| 150 | +```bash |
| 151 | +# Install APK (replace existing) |
| 152 | +adb -s SERIAL install -r /path/to/app.apk |
| 153 | + |
| 154 | +# Uninstall |
| 155 | +adb -s SERIAL uninstall <package> |
| 156 | +``` |
| 157 | + |
| 158 | +## Common Keycodes |
| 159 | + |
| 160 | +| Keycode | Description | |
| 161 | +|---------|-------------| |
| 162 | +| `KEYCODE_BACK` | Back button | |
| 163 | +| `KEYCODE_HOME` | Home button | |
| 164 | +| `KEYCODE_ENTER` | Enter/confirm | |
| 165 | +| `KEYCODE_DEL` | Delete/backspace | |
| 166 | +| `KEYCODE_VOLUME_UP` | Volume up | |
| 167 | +| `KEYCODE_VOLUME_DOWN` | Volume down | |
| 168 | +| `KEYCODE_POWER` | Power button | |
| 169 | +| `KEYCODE_TAB` | Tab key | |
| 170 | +| `KEYCODE_ESCAPE` | Escape key | |
0 commit comments