如何使用 adb 从多个连接的设备卸载 APK?

发布于 2024-12-23 04:15:53 字数 107 浏览 1 评论 0原文

adb uninstall 在连接 1 个设备时有效。

如何才能使此功能适用于连接的 5 个以上设备?

adb uninstall <package name> works when 1 device is connected.

How can I make this work for 5+ devices that are connected?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梨涡少年 2024-12-30 04:15:53

这是一个简单的脚本,我用来在我的所有设备上执行 adb 命令,应该可以在 Linux 和 MacOsX 下工作。

您可能需要使其适应您的开发环境。

#!/bin/bash
# Script adb+
# Usage
# You can run any command adb provide on all your current devices
# ./adb+ <command> is the equivalent of ./adb -s <serial number> <command>
#
# Examples
# ./adb+ version
# ./adb+ install apidemo.apk
# ./adb+ uninstall com.example.android.apis

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

Here is a simple script I use to execute adb commands over all my devices , should work under Linux and MacOsX .

You might need to adapt it to your development environment .

#!/bin/bash
# Script adb+
# Usage
# You can run any command adb provide on all your current devices
# ./adb+ <command> is the equivalent of ./adb -s <serial number> <command>
#
# Examples
# ./adb+ version
# ./adb+ install apidemo.apk
# ./adb+ uninstall com.example.android.apis

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
往事风中埋 2024-12-30 04:15:53

当连接多个设备时要卸载该软件包,可以使用以下命令。

  1. adb devices 这将输出已连接项目的列表。

    连接的设备列表  
    1234c112fsasfl 设备  
    53fsks22323233 设备  
    192.168.56.101:5555 设备
    
  2. adb -s your_device_key uninstall your_package_name

    $ adb -s 1234c112fsasfl uninstall com.test.sample
    
    success - (如果设备包含指定包名的apk)  
    failure - (如果设备不包含指定包名的apk)
    

To uninstall the package when multiple devices are connected, you can use the following commands.

  1. adb devices This will output a list of connected items.

    List of devices         attached  
    1234c112fsasfl          device  
    53fsks22323233          device  
    192.168.56.101:5555     device
    
  2. adb -s your_device_key uninstall your_package_name.

    $ adb -s 1234c112fsasfl uninstall com.test.sample
    
    success - (if the device contains the apk with the specified package name)  
    failure - (if the device did not contain the apk with the specified package name)
    
假情假意假温柔 2024-12-30 04:15:53

您必须编写一个多次调用 adb 的脚本,并在每次运行时使用 -s 开关指定每个连接设备的序列号。

另一种方法是使用 Android Maven 插件,它可以迭代所有附加的设备(或仅模拟器或设备)。请参阅与设备交互章节在我写的《Maven:完整参考》一书中。

另外,Android Maven 插件的多设备交互也适用于推送、拉取、安装和运行测试。

You would have to write a script that calls adb multiple times and on each run it specify the serial number for each attached device with the -s switch.

An alternative is to use the Android Maven plugin which can just iterate through all attached devices (or emulators or devices only). See the interaction with devices chapter in the book Maven: The Complete Reference I wrote.

Also not that the multi device interaction of the Android Maven plugin also works for push, pull, install and running tests..

感悟人生的甜 2024-12-30 04:15:53

在Java中:

public class main {
    private final static String packageName = "com.mypackage.xxx";

    public static void main(String[] args) throws IOException, InterruptedException {
        new main().doStuff();
    }

    private void doStuff() throws IOException, InterruptedException {

        Runtime rt = Runtime.getRuntime();

        String command = "adb devices -l";
        Process pr = rt.exec(command);

        ArrayList<HashMap<String, String>> devices = new ArrayList<HashMap<String, String>>();
        BufferedReader bf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String l = "";
        while ((l = bf.readLine()) != null) {
            String[] res = l.split("\\s{2,}");
            if (res.length == 2) {
                HashMap<String, String> device = new HashMap<String, String>();
                device.put("serial", res[0]);
                device.put("name", res[1]);
                devices.add(device);
            }
        }

        String commandUninstall = "adb -s %s uninstall %s";
        for (HashMap<String, String> map : devices) {
            String serial = map.get("serial");
            String finalCommanUnisntall = String.format(commandUninstall, serial, packageName);
            System.out.println(finalCommanUnisntall);

            Process pr2 = rt.exec(finalCommanUnisntall);
            BufferedReader bf2 = new BufferedReader(new InputStreamReader(pr2.getInputStream()));
            String l2 = "";
            while ((l2 = bf2.readLine()) != null) {
                System.out.println(l2);
            }
        }


    }
}

In JAVA:

public class main {
    private final static String packageName = "com.mypackage.xxx";

    public static void main(String[] args) throws IOException, InterruptedException {
        new main().doStuff();
    }

    private void doStuff() throws IOException, InterruptedException {

        Runtime rt = Runtime.getRuntime();

        String command = "adb devices -l";
        Process pr = rt.exec(command);

        ArrayList<HashMap<String, String>> devices = new ArrayList<HashMap<String, String>>();
        BufferedReader bf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String l = "";
        while ((l = bf.readLine()) != null) {
            String[] res = l.split("\\s{2,}");
            if (res.length == 2) {
                HashMap<String, String> device = new HashMap<String, String>();
                device.put("serial", res[0]);
                device.put("name", res[1]);
                devices.add(device);
            }
        }

        String commandUninstall = "adb -s %s uninstall %s";
        for (HashMap<String, String> map : devices) {
            String serial = map.get("serial");
            String finalCommanUnisntall = String.format(commandUninstall, serial, packageName);
            System.out.println(finalCommanUnisntall);

            Process pr2 = rt.exec(finalCommanUnisntall);
            BufferedReader bf2 = new BufferedReader(new InputStreamReader(pr2.getInputStream()));
            String l2 = "";
            while ((l2 = bf2.readLine()) != null) {
                System.out.println(l2);
            }
        }


    }
}
止于盛夏 2024-12-30 04:15:53

我意识到这个问题已经有了一个可接受的答案,但是:

for d in $(adb devices -l | sed '1d' | sed '$d' |  awk '{print $1}'); do adb -s $d uninstall your.pkg.id.here; done

首先子命令:

  1. 枚举所有连接的设备
  2. 剥离第一行
  3. 剥离最后一行
  4. 打印第一列(设备标识符)

然后外部for循环:

  1. 对于每个设备标识符
  2. 从指定设备卸载 your.pkg.id.here

I realise that this question already has an accepted answer, but:

for d in $(adb devices -l | sed '1d' | sed '$d' |  awk '{print $1}'); do adb -s $d uninstall your.pkg.id.here; done

The sub-command first:

  1. enumerate all connected devices
  2. strip the first line
  3. strip the final line
  4. print the first column (device identifier)

Then the outer for-loop:

  1. for each device identifier
  2. uninstall your.pkg.id.here from the specified device
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文