Managing ADB Shell with Multiple Connected Devices

Managing ADB Shell with Multiple Connected Devices

22 April 2024 Stephan Petzl Leave a comment Tech-Help

When working with Android devices, developers often need to execute commands through the Android Debug Bridge (ADB). However, when multiple devices are connected, specifying the target device for ADB commands becomes essential. This guide will help you understand how to direct ADB commands to the correct device.

Identifying Connected Devices

First, list all connected devices using the following command:

adb devices

This will display a list of devices attached to your computer. Each device is associated with a unique serial number or an IP address if connected over Wi-Fi.

Directing ADB Commands

To direct an ADB command to a specific device, use the -s flag followed by the device’s serial number:

adb -s SERIAL_NUMBER shell

For example, if the serial number is 7f1c864e, the command would be:

adb -s 7f1c864e shell

If you’re working with emulators and know the IP address and the port, the command format is similar:

adb -s IP_ADDRESS:PORT shell

For instance:

adb -s 192.168.232.2:5555 shell

Special Cases

When only one emulator is running or only one USB device is connected, you can use the -e or -d flags, respectively:

  • To target the only running emulator:
adb -e shell
  • To target the only attached USB device:
  • adb -d shell

    Setting the Environment Variable

    Another approach is to set the ANDROID_SERIAL environment variable to the serial number of the desired device. This way, you won’t need to specify the serial number with each command.

    On Windows, you can set the environment variable like this:

    set ANDROID_SERIAL=7f1c864e

    On UNIX-based systems, including Mac and Linux:

    export ANDROID_SERIAL=7f1c864e

    Integrating with Repeato

    When it comes to test automation, specifying devices is just one part of the equation. Our product, Repeato, simplifies the test automation process for iOS and Android applications. As a no-code test automation tool, Repeato allows you to create, run, and maintain automated tests quickly and efficiently.

    Repeato’s compatibility with various app frameworks such as React Native, Flutter, and Unity, coupled with the ability to execute ADB commands through script steps, makes it an invaluable asset in your development toolkit. Whether you’re ensuring your app’s UI behaves correctly or validating functionality across different devices, Repeato’s use of computer vision and AI streamlines the test automation process.

    Discover how Repeato can enhance your app’s quality and your team’s productivity by visiting our comparison with Appium and other Android automation tools.

    Like this article? there’s more where that came from!