当多个设备连接时,如何使用ADB Shell ?错误:多个设备和模拟器

时间:2022-09-14 20:51:16
$ 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

?

吗?

8 个解决方案

#1


528  

Shouldn't you enter :

不该你输入:

adb -s 7f1c864e shell

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

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

#2


214  

adb -d shell (or adb -e shell if you're connecting to an emulator).

adb -d shell(或adb -e shell,如果您正在连接一个仿真器)。

This command will help you in most of the cases, if you are too lazy to type the full ID.

如果您懒得输入完整ID,那么此命令将在大多数情况下帮助您。

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

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

-d - Direct an adb command to the only attached USB device. Returns an error when more than one USB device is attached.

-d -将adb命令指向唯一附加的USB设备。当连接多个USB设备时返回错误。

-e - Direct an adb command to the only running emulator. Returns an error when more than one emulator is running.

-e -向唯一运行的仿真器发送adb命令。当多个仿真器正在运行时,返回一个错误。

#3


28  

Another alternative would be to set environment variable ANDROID_SERIAL to the relevant serial, here assuming you are using Windows:

另一种选择是将环境变量ANDROID_SERIAL设置为相关的串行,这里假设您正在使用Windows:

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

Then you can use adb.exe shell without any issues.

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

#4


23  

I found this question after seeing the 'more than one device' error, with 2 offline phones showing:

我是在看到“不止一个设备”的错误后发现这个问题的,两个离线手机显示:

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

If you only have one device connected, run the following commands to get rid of the offline connections:

如果您只有一个设备连接,运行以下命令来摆脱脱机连接:

adb kill-server
adb devices

#5


5  

This gist will do most of the work for you showing a menu when there are multiple devices connected:

当有多个设备连接时,本要点将为您展示菜单:

$ 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:

To avoid typing you can just create an alias that included the device selection as explained here.

为了避免输入,您可以创建一个包含此处解释的设备选择的别名。

#6


4  

Running adb commands on all connected devices

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

Create a bash (adb+)

创建一个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

done use it with

完成使用

adb+ //+ command

亚洲开发银行+ / / +命令

#7


0  

For Windows, here's a quick 1 liner example of how to install a file..on multiple devices

对于Windows,这里有一个如何安装文件的简单的1行示例。在多个设备上

FOR /F "skip=1"  %x IN ('adb devices') DO start adb -s %x install -r myandroidapp.apk

If you plan on including this in a batch file, replace %x with %%x, as below

如果您打算在批处理文件中包含此内容,请将%x替换为%x,如下所示

FOR /F "skip=1"  %%x IN ('adb devices') DO start adb -s %%x install -r myandroidapp.apk

#8


0  

Create a Bash (tools.sh) to select a serial from devices (or emulator):

创建一个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

Then in another option can use adb -s (global option -s use device with given serial number that overrides $ANDROID_SERIAL):

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

adb -s ${device} <command>

I tested this code on MacOS terminal, but I think it can be used on windows across Git Bash Terminal.

我在MacOS终端上测试了这段代码,但是我认为它可以在Git Bash终端的windows上使用。

Also remember configure environmental variables and Android SDK paths on .bash_profile file:

还记得在.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"

#1


528  

Shouldn't you enter :

不该你输入:

adb -s 7f1c864e shell

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

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

#2


214  

adb -d shell (or adb -e shell if you're connecting to an emulator).

adb -d shell(或adb -e shell,如果您正在连接一个仿真器)。

This command will help you in most of the cases, if you are too lazy to type the full ID.

如果您懒得输入完整ID,那么此命令将在大多数情况下帮助您。

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

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

-d - Direct an adb command to the only attached USB device. Returns an error when more than one USB device is attached.

-d -将adb命令指向唯一附加的USB设备。当连接多个USB设备时返回错误。

-e - Direct an adb command to the only running emulator. Returns an error when more than one emulator is running.

-e -向唯一运行的仿真器发送adb命令。当多个仿真器正在运行时,返回一个错误。

#3


28  

Another alternative would be to set environment variable ANDROID_SERIAL to the relevant serial, here assuming you are using Windows:

另一种选择是将环境变量ANDROID_SERIAL设置为相关的串行,这里假设您正在使用Windows:

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

Then you can use adb.exe shell without any issues.

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

#4


23  

I found this question after seeing the 'more than one device' error, with 2 offline phones showing:

我是在看到“不止一个设备”的错误后发现这个问题的,两个离线手机显示:

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

If you only have one device connected, run the following commands to get rid of the offline connections:

如果您只有一个设备连接,运行以下命令来摆脱脱机连接:

adb kill-server
adb devices

#5


5  

This gist will do most of the work for you showing a menu when there are multiple devices connected:

当有多个设备连接时,本要点将为您展示菜单:

$ 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:

To avoid typing you can just create an alias that included the device selection as explained here.

为了避免输入,您可以创建一个包含此处解释的设备选择的别名。

#6


4  

Running adb commands on all connected devices

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

Create a bash (adb+)

创建一个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

done use it with

完成使用

adb+ //+ command

亚洲开发银行+ / / +命令

#7


0  

For Windows, here's a quick 1 liner example of how to install a file..on multiple devices

对于Windows,这里有一个如何安装文件的简单的1行示例。在多个设备上

FOR /F "skip=1"  %x IN ('adb devices') DO start adb -s %x install -r myandroidapp.apk

If you plan on including this in a batch file, replace %x with %%x, as below

如果您打算在批处理文件中包含此内容,请将%x替换为%x,如下所示

FOR /F "skip=1"  %%x IN ('adb devices') DO start adb -s %%x install -r myandroidapp.apk

#8


0  

Create a Bash (tools.sh) to select a serial from devices (or emulator):

创建一个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

Then in another option can use adb -s (global option -s use device with given serial number that overrides $ANDROID_SERIAL):

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

adb -s ${device} <command>

I tested this code on MacOS terminal, but I think it can be used on windows across Git Bash Terminal.

我在MacOS终端上测试了这段代码,但是我认为它可以在Git Bash终端的windows上使用。

Also remember configure environmental variables and Android SDK paths on .bash_profile file:

还记得在.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"