多台设备连接时如何使用ADB Shell? 因“错误:多个设备和模拟器”失败

>adb --help ... -s <specific device> - directs command to the device or emulator with the given serial number or qualifier. Overrides ANDROID_SERIAL environment variable. ... >adb devices List of devices attached emulator-5554 device 7f1c864e device ... >adb shell -s 7f1c864e error: more than one device and emulator 

你不应该input:

 adb -s 7f1c864e shell 


http://developer.android.com/tools/help/adb.html#directingcommands

adb -d shell

如果你懒得input完整的ID,这个命令将在大多数情况下帮助你

http://developer.android.com/tools/help/adb.html#commandsummary您将会看到,;

目标设备
-d将adb命令指向唯一连接的USB设备。
-e将adb命令指向唯一运行的模拟器实例。

如果每个types的多个实例正在运行,则两者都会返回一个错误。

另一种方法是将环境variablesANDROID_SERIAL设置为相关的串行,这里假定您使用的是Windows:

 set ANDROID_SERIAL="7f1c864e" echo %ANDROID_SERIAL% "7f1c864e" 

然后你可以使用adb.exe shell没有任何问题。

看到“多个设备”错误后,我发现这个问题,2个离线手机显示:

 C:\Program Files (x86)\Android\android-sdk\android-tools>adb devices List of devices attached SH436WM01785 offline SH436WM01785 offline SH436WM01785 sideload 

如果您只连接了一台设备,请运行以下命令以摆脱脱机连接:

 adb kill-server adb devices 

在所有连接的设备上运行adb命令

创build一个bash(adb +)

 adb devices | while read line do if [ ! "$line" = "" ] && [ `echo $line | awk '{print $2}'` = "device" ] then device=`echo $line | awk '{print $1}'` echo "$device $@ ..." adb -s $device $@ fi 

完成使用它

adb + // +命令

当有多个设备连接时,这个要点将为你做大部分的工作:

 $ adb $(android-select-device) shell 1) 02783201431feeee device 3) emulator-5554 2) 3832380FA5F30000 device 4) emulator-5556 Select the device to use, <Q> to quit: 

为了避免input,您可以创build一个包含设备select的别名,如此处所述。

创build一个Bash(tools.sh)从设备(或模拟器)中select一个序列:

 clear; echo "===================================================================================================="; echo " ADB DEVICES"; echo "===================================================================================================="; echo ""; adb_devices=( $(adb devices | grep -v devices | grep device | cut -f 1)#$(adb devices | grep -v devices | grep device | cut -f 2) ); if [ $((${#adb_devices[@]})) -eq "1" ] && [ "${adb_devices[0]}" == "#" ] then echo "No device found"; echo ""; echo "===================================================================================================="; device="" fxMenu; else read -p "$( f=0 for dev in "${adb_devices[@]}"; do nm="$(echo ${dev} | cut -f1 -d#)"; tp="$(echo ${dev} | cut -f2 -d#)"; echo " $((++f)). ${nm} [${tp}]"; done echo ""; echo " 0. Quit" echo ""; echo "===================================================================================================="; echo ""; echo ' Please select a device: ' )" selection error="You think it's over just because I am dead. It's not over. The games have just begun."; fxValidationNumberMenu ${#adb_devices[@]} ${selection} "${error}" case "${selection}" in 0) fxMenu;; *) device="$(echo ${adb_devices[$((selection-1))]} | cut -f1 -d#)"; fxMenu;; esac fi 

然后在另一个选项可以使用adb -s (全局选项-s使用设备与给定的序列号,覆盖$ ANDROID_SERIAL):

 adb -s ${device} <command> 

我在MacOSterminal上testing了这个代码,但是我认为它可以在Git Bashterminal的窗口上使用。

另请记住在.bash_profile文件上configuration环境variables和Android SDKpath:

 export ANDROID_HOME="/usr/local/opt/android-sdk/" export PATH="$ANDROID_HOME/platform-tools:$PATH" export PATH="$ANDROID_HOME/tools:$PATH"