【问题标题】:How can I use adb to uninstall an APK from multiple connected devices? [duplicate]如何使用 adb 从多个连接的设备上卸载 APK? [复制]
【发布时间】:2011-12-29 18:31:17
【问题描述】:

adb uninstall <package name> 在连接 1 个设备时工作。

我怎样才能使这项工作适用于 5 台以上的连接设备?

【问题讨论】:

标签: java android eclipse shell adb


【解决方案1】:

这是我用来在所有设备上执行 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

【讨论】:

  • 我收到错误head: illegal line count -- -1
  • @SheehanAlam 你用的是什么操作系统?看来您没有使用 GNU coreutils 中的 head 命令。
  • @SheehanAlam 我更新了它现在应该与 MacOSX 一起使用的脚本。
  • 感谢拉布格斯!我尝试运行这个sh uninstall.sh,但现在没有得到任何回声等。
  • adb devices 是否为您提供任何活动设备? uninstall.sh 包含什么?
【解决方案2】:

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

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

    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)
    

【讨论】:

  • 这是完美的解决方案,节省了我数小时的调试时间
【解决方案3】:

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

另一种方法是使用Android Maven plugin,它可以遍历所有连接的设备(或仅模拟器或设备)。请参阅 Maven: The Complete Reference 一书中的 interaction with devices chapter

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

【讨论】:

  • 我猜这个脚本是 Rabgs 为你写的 ;-)
【解决方案4】:

在 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);
            }
        }


    }
}

【讨论】:

    【解决方案5】:

    我意识到这个问题已经有一个公认的答案,但是:

    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

    【讨论】:

      猜你喜欢
      • 2016-02-29
      • 2012-01-26
      • 2013-06-14
      • 1970-01-01
      • 1970-01-01
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多