background

As android developers, ADB is a debugging tool that is frequently used. However, when the PC is connected to multiple Android phones or virtual machine devices, each time you use ADB, you need to manually specify which device to debug through the -S parameter, which is cumbersome to input. Such as:

adb -s E0014021600090 logcat
Copy the code

Some people may say that you can disconnect the device that you do not want to debug temporarily, and ensure that only one debugged device is connected at a time. However, this can be cumbersome in scenarios where frequent debugging across multiple devices is required.

At this point, we hope that when we enter adb command, if there are more than one device, we will be prompted to choose which device (enter a simple device number) to debug, without the input of lengthy device number parameters, and without frequent plug and unplug data cable.

Script implementation

Write an ADB wrapper script for this (MAC and Linux only) :

#! /bin/bash
export PATH

Configure the local ADB path before using it
adb='/Users/luliang/Library/Android/sdk/platform-tools/adb'

if [ ! -f $adb ]; then
    echo "Adb path configuration error, please correct adb path in this script"
    exit 1
fi

Some ADB parameters do not need to select a specific device whitelist list
whiteList=(
    devices
    connect
    disconnect
    start-server
    kill-server
    help
    version
    pair
    forward
    reverse
)

if [[ "$@"= =""]].then
    "$adb" "$@"
    exit $?
fi

if [[ "${whiteList[@]}"= ~"The $1"]].then
    "$adb" "$@"
    exit $?
fi

if [[ The $1= ="-s"]].then
    "$adb" "$@"
    exit $?
fi

devices=`$adb devices | sed -n '2,$p' | cut -d '	' -f 1`
count=`echo "$devices" | wc -l | xargs`

if [ $count -gt 1 ]; then
    i=1
    hint="N0.\tSN (Device)"
    while(($i< ="$count"));do
        device=`echo "$devices" | sed -n "${i}p"`
        info=`adb devices | grep $device | cut -d '	' -f 2`
        brand=`$adb -s $device shell getprop ro.product.brand 2> /dev/null || echo $info`
        model=`$adb -s $device shell getprop ro.product.model 2> /dev/null`
        hint=$hint"\n $i\t$device ($brand $model)"

        i=$(($i+1))
    done

    hint=$hint"\n---------------------------"
    msg=`echo -e $hint`
    read -p "${msg}'echo $'\n' 'select the device serial number [1-$count]: " ID

    Check whether the ID is a valid number
    expr $ID "+" 1 &> /dev/null

    if [ $? -ne 0 ];then
        echo "Please enter a valid device number [1-$count]. ""
        exit 1
    fi

    if [ $ID -lt 1 -o $ID -gt $count ]; then
        echo "Please enter a valid device number [1-$count]. ""
        exit 1
    fi

    device=`echo "$devices" | sed -n "${ID}p"`
    "$adb" "-s" $device "$@"
else
    "$adb" "$@"
fi
Copy the code

Directions for use

Step 1: The ADB path variable in the script needs to change the ADB path in the local location. If you have configured the ADB environment variable before, you can quickly obtain this by using the command.

which adb
Copy the code

Step 2: Name the script ADB and save it to a specific path, such as directory Shells (~/ Shells) in the home directory. Add executable permissions to the file.

chmod a+x ~/shells/adb
Copy the code

Step 3: Add the path to the environment variable configuration file ~/.bashrc or ~/.zshrc (if using ZSH), for example

export PATH=/Users/luliang/shells:$PATH
Copy the code

In addition, if the ADB path has been configured previously, you need to remove the previous environment variable configuration. Make sure you run a script file when executing ADB.

You can then run the script as you normally would with ADB. When less than two Android devices are connected, it works as usual with ADB, and when there are multiple devices, it prompts you to select which device to use for debugging.

Running effect

Take adb Logcat as an example:

$ adb logcat N0. SN (Device) 1 988d50414347355443 (samsung SM-G9500) 2 E0014021600090 (smartisan yq601) --------------------------- Select device number [1-2]: 2Copy the code

In order to distinguish different devices, the device brand and model are provided after the serial number of the device list.

Similarly, other ADB commands such as ADB shell, ADB reboot, and ADB install xxx.apk are also used. You only need to enter the serial number of the specified debugging device, and the command similar to adb -s {SN} XXX is automatically executed, which greatly improves the debugging efficiency of multiple devices.