$ adb --help
-s SERIAL use device with given serial (overrides $ANDROID_SERIAL)
$ adb devices
List of devices attached
emulator-5554 device
7f1c864e device
$ adb shell -s 7f1c864e
error: more than one device and emulator
$ adb --help
-s SERIAL use device with given serial (overrides $ANDROID_SERIAL)
$ adb devices
List of devices attached
emulator-5554 device
7f1c864e device
$ adb shell -s 7f1c864e
error: more than one device and emulator
当前回答
在命令前使用-s选项指定设备,例如:
adb -s 7f1c864e shell
对于多个模拟器,使用进程的IP和端口作为id,例如:
adb -s 192.168.232.2:5555 <command>
参见如何获取Android模拟器的IP地址? 但如果只有一个模拟器,请尝试: Adb -e <命令>
参见http://developer.android.com/tools/help/adb.html#directingcommands
其他回答
在命令前使用-s选项指定设备,例如:
adb -s 7f1c864e shell
对于多个模拟器,使用进程的IP和端口作为id,例如:
adb -s 192.168.232.2:5555 <command>
参见如何获取Android模拟器的IP地址? 但如果只有一个模拟器,请尝试: Adb -e <命令>
参见http://developer.android.com/tools/help/adb.html#directingcommands
https://developer.android.com/studio/command-line/adb#directingcommands
对我的测试有效的方法:
Ubuntu bash终端:
$ adb devices
List of devices attached
646269f0 device
8a928c2 device
$ export ANDROID_SERIAL=646269f0
$ echo $ANDROID_SERIAL
646269f0
$ adb reboot bootloader
Windows命令提示符:
$ adb devices
List of devices attached
646269f0 device
8a928c2 device
$ set ANDROID_SERIAL=646269f0
$ echo $ANDROID_SERIAL$
646269f0
$ adb reboot bootloader
这使您可以使用正常的命令和脚本,就好像只有ANDROID_SERIAL设备附加。
或者,您可以每次都提到设备序列号。
$ adb -s 646269f0 shell
当有多个设备连接时,这个要点将为你显示菜单做大部分工作:
$ 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:
为了避免输入,您可以创建一个别名,其中包括此处解释的设备选择。
创建一个Bash (tools.sh)从设备(或模拟器)中选择一个串行:
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=""
// Call Main Menu function 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.";
// Call Validation Numbers fxValidationNumberMenu ${#adb_devices[@]} ${selection} "${error}"
case "${selection}" in
0)
// Call Main Menu function fxMenu;
*)
device="$(echo ${adb_devices[$((selection-1))]} | cut -f1 -d#)";
// Call Main Menu function fxMenu;
esac
fi
然后在另一个选项中可以使用adb -s(全局选项-s使用设备与给定的序列号,覆盖$ANDROID_SERIAL):
adb -s ${device} <command>
我在MacOS终端上测试了这段代码,但我认为它可以跨Git Bash终端在windows上使用。
还记得在.bash_profile文件中配置环境变量和Android SDK路径:
export ANDROID_HOME="/usr/local/opt/android-sdk/"
export PATH="$ANDROID_HOME/platform-tools:$PATH"
export PATH="$ANDROID_HOME/tools:$PATH"
在看到“不止一个设备”错误后,我发现了这个问题,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