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

时间:2011-12-29 18:31:18

标签: java android eclipse shell adb

adb uninstall <package name>在连接1台设备时有效。

如何为5台以上连接的设备进行此项工作?

5 个答案:

答案 0 :(得分:26)

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

答案 1 :(得分:18)

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

  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)
    

答案 2 :(得分:1)

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

另一种方法是使用Android Maven plugin,它可以遍历所有连接的设备(或仅限模拟器或设备)。请参阅Maven:我写的完整参考书中的i nteraction with devices chapter

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

答案 3 :(得分:1)

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


    }
}

答案 4 :(得分:1)

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

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. 打印第一列(设备标识符)
  5. 然后是外部for循环:

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