2011-12-29 33 views
11

adb uninstall <package name>在連接1臺設備時起作用。如何使用adb從多個連接的設備上卸載APK?

我該如何爲5臺以上的設備進行連接?

+3

有趣的是,你已經問過這個問題:http://stackoverflow.com/questions/8610733/how-can-i-adb-install-an-apk-to-multiple-connected-devices。用不同的命令也是一樣。 – aromero 2011-12-29 18:56:37

+0

它不會因爲安裝需要設備ID,因爲卸載不需要。 – 2012-01-14 17:59:48

回答

24

下面是一個簡單的腳本,我用了我所有的設備來執行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 [email protected] ..." 
     adb -s $device [email protected] 
    fi 
done 
+0

不錯的腳本;-) – 2011-12-29 19:54:31

+0

我得到錯誤'head:非法行數 - -1' – 2012-01-14 18:01:50

+0

@SheehanAlam你在使用什麼操作系統?似乎你沒有使用GNU coreutils的head命令。 – Rick 2012-01-14 19:26:28

1

您必須編寫調用亞行多次一個腳本,並在每次運行它指定與-s開關連接的每臺設備的序列號。

一種替代方法是使用Android Maven plugin可通過所有連接的設備(或者仿真器或器件)只是迭代。請參閱我寫的Maven:The Complete Reference中的i nteraction with devices chapter

也不認爲Android Maven插件的多設備互動也適用於推,拉,安裝和運行測試..

+0

我猜這個腳本是由Rabgs寫給你的;-) – 2011-12-29 19:54:04

14

要在多個設備連接時卸載軟件包,可以使用以下命令。

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


    } 
} 
0

我意識到,這個問題已經有一個公認的答案,但:

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
相關問題