DEV Community

Kabue Charles
Kabue Charles

Posted on

2

Connect/debug android with adb (Android Debug Bridge) commands in terminal easily, with one or many devices/emulators.

Alt Text

If you are developing/debugging an android app, sometimes you may want to run some commands from the terminal (using adb). If you have one device/emulator attached, then you are okay to run commands like adb install -r app.apk.

But when you do have several devices attached, that command will not work. You would have to specify the ID of the specific device you want to run a command on: adb {deviceid} install -r app.apk. Now, to get a list of attached devices, you simply run the command: adb devices

$ adb devices
List of devices attached
emulator-5556 device product:sdk_google_phone_x86_64 model:Android_SDK_built_for_x86_64 device:generic_x86_64
emulator-5554 device product:sdk_google_phone_x86 model:Android_SDK_built_for_x86 device:generic_x86
0a388e93      device usb:1-1 product:razor model:Nexus_7 device:flo

This is why I wrote below bash script:

#!/bin/bash
devices=(`adb devices | grep -v devices | grep device | cut -f 1`)
devicesCount=${#devices[@]}
devicesToUse=()
if [ $devicesCount -eq 0 ]
then
echo "No attached devices.";
elif [ $devicesCount -eq 1 ]
then
name=`adb -s ${devices[0]} shell getprop ro.product.model`
api=`adb -s ${devices[0]} shell getprop ro.build.version.sdk`
version=`adb -s ${devices[0]} shell getprop ro.build.version.release`
echo "One attached device.";
read -p "Use \"$name [$version - API $api]\"?[yes/no] (default: yes): " useDevice
useDevice=${useDevice,,}
if [ "$useDevice" = "yes" ] || [ -z "$useDevice" ]
then
devicesToUse=(${devices[0]})
fi
else
echo "$devicesCount attached devices."
defaultDeviceName=`adb -s ${devices[0]} shell getprop ro.product.model`
defaultDeviceApi=`adb -s ${devices[0]} shell getprop ro.build.version.sdk`
defaultDeviceVersion=`adb -s ${devices[0]} shell getprop ro.build.version.release`
echo "Which device do you want to use? (default: \"$defaultDeviceName [$defaultDeviceVersion - API $defaultDeviceApi]\")"
for i in ${!devices[@]};
do
device=${devices[$i]}
name=`adb -s $device shell getprop ro.product.model`
api=`adb -s $device shell getprop ro.build.version.sdk`
version=`adb -s $device shell getprop ro.build.version.release`
echo "$(($i+1))) $name [$version - API $api]"
done
echo "$(($devicesCount+1))) all"
read -p "Choose a number: " deviceIndex
deviceIndex=$((${deviceIndex,,}-1))
if [ $deviceIndex -gt 0 ] && [ $deviceIndex -lt $devicesCount ];
then
devicesToUse=(${devices[$deviceIndex]})
elif [ $deviceIndex -eq $devicesCount ]
then
devicesToUse=("${devices[@]}")
else
devicesToUse=(${devices[0]})
fi
fi
devicesToUseCount=${#devicesToUse[@]}
if [ $devicesToUseCount -gt 0 ]
then
for i in ${!devicesToUse[@]};
do
device=${devicesToUse[$i]}
name=`adb -s $device shell getprop ro.product.model`
api=`adb -s $device shell getprop ro.build.version.sdk`
version=`adb -s $device shell getprop ro.build.version.release`
echo "Using \"$name [$version - API $api]\""
adb -s $device "$@"
done
fi



This script allows you to connect/debug android with adb commands in terminal easily, with one or many devices/emulators.

bash <(curl -s https://gist.githubusercontent.com/McKabue/ac124f736085e9650fdbf63b1d9ca5ab/raw/ccfba9fad913baebc1f08cbcd7e525cc3827dd5a/debug-android-from-terminal.sh) install -r app.apk

This will prompt you to run the command on a specific device, or all of them.

Sentry mobile image

Mobile Vitals: A first step to Faster Apps

Slow startup times, UI hangs, and frozen frames frustrate users—but they’re also fixable. Mobile Vitals help you measure and understand these performance issues so you can optimize your app’s speed and responsiveness. Learn how to use them to reduce friction and improve user experience.

Read the guide

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs