Loading...
Loading...
Use this skill to attach a USB device or emulator to ADB, list transports with `adb devices` / `adb devices -l`, disambiguate among multiple devices using `-s SERIAL`, `-d` (single USB), `-e` (single TCP/IP), or `-t TRANSPORT_ID`, gate scripts on a transport with the canonical `adb wait-for[-TRANSPORT]-<state>` syntax (TRANSPORT in {usb, local, any}; state in {device, recovery, rescue, sideload, bootloader, disconnect}), interpret device states (`device`, `offline`, `unauthorized`, `no permissions`, `recovery`, `sideload`, `bootloader`, `rescue`), accept the RSA fingerprint dialog on first connect, and install Linux udev rules. Use when the user mentions `error: more than one device/emulator`, `error: device not found`, `unauthorized`, `no permissions`, `daemon not running`, "wait for device to boot", `wait-for-device-online` (which is not a real subcommand), or asks how to script around emulator startup.
npx skill4agent add skydoves/android-testing-skills connecting-to-devicesadb devicesmore than one device/emulatorudevwait-for-device-onlineadb shellerror: more than one device/emulatorunauthorizedofflineno permissionsadb devicesadb wait-for-device-onlineadb devices../../architecture/understanding-adb-architecture/SKILL.md../connecting-over-wifi/SKILL.md../../apps/installing-and-managing-apps/SKILL.md../../architecture/understanding-adb-architecture/SKILL.mdSettings → System → Developer optionsAbout phone/etc/udev/rules.d/51-android.rulesadb devices<serial> <state> [details...]-ladb devices -l
# List of devices attached
# emulator-5556 device product:sdk_google_phone_x86_64 model:Android_SDK_built_for_x86_64 device:generic_x86_64 transport_id:1
# 0a388e93 device usb:1-1 product:razor model:Nexus_7 device:flo transport_id:2-lproductmodeldevicetransport_id| Flag | Meaning | Fails when |
|---|---|---|
| Explicit serial. Works for USB serials ( | Serial not connected. |
| The single USB device. | Zero or more than one USB device. |
| The single TCP/IP device (covers emulators and | Zero or more than one TCP device. |
| Numeric transport ID from | Transport ID not present. |
-ttimeoutgtimeoutadb devices | awk '$2=="device"{print $1}'adb get-serialnoadb get-stateoffline | bootloader | deviceadb get-devpathadb deviceswait-for-*adb helpscriptingwait-for[-TRANSPORT]-STATE
TRANSPORT ∈ { usb, local, any } (transport defaults to any)
STATE ∈ { device, recovery, rescue, sideload, bootloader, disconnect }| Command | Blocks until... |
|---|---|
| Any transport, state |
| A USB-attached device reaches state |
| A TCP/IP device (emulator or wireless) reaches state |
| Device shows up in |
| Same idea for those modes. |
| The currently selected device disconnects (e.g. after |
wait-for-device-onlinewait-for-*sys.boot_completedadb wait-for-device
until [[ "$(adb shell getprop sys.boot_completed | tr -d '\r')" == "1" ]]; do
sleep 1
done
adb shell input keyevent 82 # unlock the AOSP slide-up screenadb shell reboot
adb wait-for-disconnect
adb wait-for-deviceQuery for devicesadb help| State | Meaning | Recovery |
|---|---|---|
| Online, | n/a |
| Transport exists but | |
| Host RSA key not yet accepted on-device. | Unlock device, tap Allow on the on-device dialog. Try a different USB cable if no dialog appears (some "charge-only" cables block data). |
| Booted into recovery. Only a small subset of | n/a |
| OTA sideload mode. Use | n/a |
| At the bootloader (fastboot). | n/a |
| Rescue Party mode (Android 10+). | n/a |
| Transient TLS handshake state over wireless. | wait. |
| (Linux only) USB visible but blocked by missing udev rules. | install rules — see step 7. |
"When you connect a device running Android 4.2.2 (API level 17) or higher, the system shows a dialog asking whether to accept an RSA key that allows debugging through this computer." — developer.android.com/tools/adb Sequence:
adbdadb devicesunauthorized/data/misc/adb/adb_keys~/.android/adbkeySettings → Developer options → Revoke USB debugging authorizationsno permissions/etc/udev/rules.d/51-android.rules# Google
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", MODE="0660", GROUP="plugdev", TAG+="uaccess"
# Samsung
SUBSYSTEM=="usb", ATTR{idVendor}=="04e8", MODE="0660", GROUP="plugdev", TAG+="uaccess"sudo udevadm control --reload-rules
sudo udevadm trigger
# unplug and re-plug the deviceandroid-udevandroid-sdk-platform-tools-common/lib/udev/rules.d/SDK Manager → SDK Tools → Google USB Driverandroid_sdk\extras\google\usb_driver\https://developer.android.com/tools/extras/oem-usb--all# Sequential
for s in $(adb devices | awk '$2=="device"{print $1}'); do
adb -s "$s" install -r app.apk
done
# Parallel (4 jobs at a time)
adb devices | awk '$2=="device"{print $1}' \
| xargs -I{} -P 4 adb -s {} install -r app.apkadb --one-device 0a388e93 start-servermore than one device/emulator# WRONG
adb shell getprop ro.product.model
# adb: more than one device/emulator
# WRONG because: with two transports attached (e.g. an emulator + a USB phone),
# every non-server adb command needs an explicit selector. The command did not
# fail; it never even ran.# RIGHT
adb -s emulator-5554 shell getprop ro.product.model
# Or, if there is exactly one USB phone:
adb -d shell getprop ro.product.model
# Or set $ANDROID_SERIAL once and stop typing -s:
export ANDROID_SERIAL=emulator-5554
adb shell getprop ro.product.modelwait-for-device-online# WRONG
adb wait-for-device-online
# error: usage: ...
# WRONG because: this command does not exist. The canonical syntax is
# wait-for[-TRANSPORT]-<state> with state in {device, recovery, rescue,
# sideload, bootloader, disconnect}. There is no -online state.# RIGHT
adb wait-for-device # any transport, state=device
# Then poll boot complete (transport up != system booted):
until [[ "$(adb shell getprop sys.boot_completed | tr -d '\r')" == "1" ]]; do
sleep 1
done# WRONG
emulator -avd Pixel_API_34 &
adb install -r app.apk # races: emulator may not be online yet
# Often: error: device 'emulator-5554' not found# RIGHT
adb start-server # important: see corner case below
emulator -avd Pixel_API_34 &
adb -s emulator-5554 wait-for-device
until [[ "$(adb -s emulator-5554 shell getprop sys.boot_completed | tr -d '\r')" == "1" ]]; do
sleep 1
done
adb -s emulator-5554 install -r app.apkCorner case (verbatim from the adb page): "running emulators [may] not show up in... when all of the following are true: the adb server is not running; you use theadb devicescommand with theemulatoror-portoption with an odd-numbered port between 5554 and 5584; ... You start the adb server after you start the emulator." Workaround:-portsBEFOREadb start-server.emulator
transport_id-t# WRONG
adb -t 60 shell getprop ro.product.model
# WRONG because: -t is transport_id, not a 60-second timeout. Either there is
# no transport with id=60 (error: device not found) or the command runs against
# the wrong device.# RIGHT
# Real timeout via the shell tool:
timeout 60 adb -s emulator-5554 shell getprop ro.product.model # GNU timeout
gtimeout 60 adb -s emulator-5554 shell getprop ro.product.model # macOS (coreutils via brew)
# Use -t only with a transport_id from adb devices -l:
adb -t 2 shell getprop ro.product.model-s-d-e-t$ANDROID_SERIALwait-for[-TRANSPORT]-<state>wait-for-device-onlinegetprop sys.boot_completedwait-for-deviceam instrument-t TRANSPORT_IDtimeoutgtimeout~/.android/adbkey*unauthorized../../architecture/understanding-adb-architecture/SKILL.mdadb devices -ltransport_idandroid-udev51-android.rulesadb devices -ldeviceofflineunauthorizedno permissionsadb devices | awk '$2=="device"{print $1}'adb -s <serial> get-statedeviceadb wait-for-devicedeviceadb shell getprop sys.boot_completed1adb -d shell echo ok-e-smore than one device/emulatorlsusbadb devicesdeviceno permissionsadb deviceswait-for-*adbtasks/research/A1-adb-architecture-devices.mdwait-for-*wait-for-device-onlinedocs/CORPUS.md../../architecture/understanding-adb-architecture/SKILL.md../connecting-over-wifi/SKILL.md../../apps/installing-and-managing-apps/SKILL.md../../tests/running-instrumented-tests-via-adb/SKILL.mdAndroidJUnit4../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.md../../../jvm-tests/runner/configuring-junit4-on-android/SKILL.md../../../fundamentals/strategies/organizing-test-source-sets/SKILL.md