14

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

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

Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
  • 3
    Funny, you've already asked this question: http://stackoverflow.com/questions/8610733/how-can-i-adb-install-an-apk-to-multiple-connected-devices . It's the same with a different command. – aromero Dec 29 '11 at 18:56
  • it doesn't because install requires the device id, as uninstall does not. – Sheehan Alam Jan 14 '12 at 17:59

5 Answers5

27

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)
    
Anil
  • 21,730
  • 9
  • 73
  • 100
Jaya
  • 999
  • 11
  • 8
27

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
Rick
  • 1,124
  • 9
  • 13
  • I get the error `head: illegal line count -- -1` – Sheehan Alam Jan 14 '12 at 18:01
  • @SheehanAlam what os are you using ? seems you are not using the head command from the GNU coreutils . – Rick Jan 14 '12 at 19:26
  • @SheehanAlam I updated it the script it should work now with MacOSX . – Rick Jan 15 '12 at 09:50
  • Thanks Rabgs! I tried running this `sh uninstall.sh` but now not getting any of the echoes etc. – Sheehan Alam Jan 15 '12 at 17:48
  • Is `adb devices` giving you any active devices ? and what does uninstall.sh contain ? – Rick Jan 15 '12 at 21:08
  • @SheehanAlam Put the script code provided by Rabgs into a file named adb+, then add it to your PATH in .bashrc or .bash_profile, then you can run the adb commands like normal, but just use adb+ instead of adb. The script takes in the parameters from adb+ {arg1} {arg2} and runs your adb command for each device. – Christopher Perry Jul 10 '12 at 22:12
  • Awesome. I've put it as a function to .bashrc (running linux Mint17.3). Use this link as guidance: http://stackoverflow.com/a/6212408/2369484 – fada21 Jul 01 '16 at 11:37
2

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
ant
  • 1,140
  • 12
  • 19
1

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


    }
}
deadfish
  • 11,996
  • 12
  • 87
  • 136
1

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..

Manfred Moser
  • 29,539
  • 13
  • 92
  • 123