adb shell and adb push for specific avd
Categories:
Targeting Specific Android Virtual Devices with ADB Shell and Push

Learn how to precisely target and interact with individual Android Virtual Devices (AVDs) using adb shell and adb push commands, essential for multi-device testing and development.
When working with Android development, especially in environments with multiple emulators or physical devices connected, it's crucial to direct your adb commands to the correct target. This article will guide you through the process of identifying and interacting with specific Android Virtual Devices (AVDs) using adb shell for command execution and adb push for file transfers.
Identifying Your Android Virtual Devices
Before you can target a specific AVD, you need to know its unique identifier. The adb devices command is your primary tool for listing all connected devices and emulators, along with their serial numbers. Each running AVD will have a serial number typically in the format emulator-XXXX, where XXXX is a port number.
adb devices
Listing all connected ADB devices and emulators
The output will show a list of devices. For example:
List of devices attached
emulator-5554 device
emulator-5556 device
In this example, emulator-5554 and emulator-5556 are the serial numbers of two running AVDs. You will use these serial numbers to specify your target device.
flowchart TD
A[Start ADB Interaction] --> B{Run 'adb devices'}
B --> C{Identify Target AVD Serial}
C --> D["Use '-s <serial>' with ADB commands"]
D --> E[Execute 'adb shell' or 'adb push']
E --> F[End ADB Interaction]Workflow for targeting a specific AVD with ADB
Executing Commands with adb shell on a Specific AVD
The adb shell command allows you to execute shell commands directly on an Android device. To target a specific AVD, you use the -s flag followed by the AVD's serial number. This ensures your command is executed only on the intended emulator.
adb -s emulator-5554 shell getprop ro.build.version.sdk
adb -s emulator-5556 shell ls /sdcard/
Executing getprop and ls commands on specific AVDs
In the examples above, the first command retrieves the SDK version from emulator-5554, and the second lists the contents of the /sdcard/ directory on emulator-5556. Without the -s flag, adb might either pick an arbitrary device (if only one is running) or return an error if multiple devices are present and no specific target is provided.
Pushing Files to a Specific AVD with adb push
The adb push command is used to copy files or directories from your development machine to an Android device. Similar to adb shell, you must use the -s flag to specify the target AVD when multiple devices are connected. This is crucial for deploying test data, configuration files, or other assets to a particular emulator.
adb -s emulator-5554 push /path/to/local/file.txt /sdcard/Download/file.txt
adb -s emulator-5556 push /path/to/local/test_data/ /data/local/tmp/test_data/
Pushing a file and a directory to specific AVDs
The first command pushes file.txt from your local machine to the /sdcard/Download/ directory on emulator-5554. The second command pushes the entire test_data directory to /data/local/tmp/ on emulator-5556. Always ensure the destination path on the AVD is writable and appropriate for your needs.
ANDROID_SERIAL environment variable to the AVD's serial number. This will make adb automatically target that device for subsequent commands without needing to use the -s flag repeatedly. Remember to unset it or change it when switching targets.export ANDROID_SERIAL=emulator-5554
adb shell getprop ro.build.version.sdk
unset ANDROID_SERIAL
Using the ANDROID_SERIAL environment variable
-s flag and multiple devices are connected, adb will return an error stating "more than one device/emulator". Always specify the target when ambiguity exists to avoid unexpected behavior or errors.