14

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

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

2

5 Answers 5

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
7
  • I get the error head: illegal line count -- -1 Jan 14, 2012 at 18:01
  • @SheehanAlam what os are you using ? seems you are not using the head command from the GNU coreutils .
    – Rick
    Jan 14, 2012 at 19:26
  • @SheehanAlam I updated it the script it should work now with MacOSX .
    – Rick
    Jan 15, 2012 at 9:50
  • Thanks Rabgs! I tried running this sh uninstall.sh but now not getting any of the echoes etc. Jan 15, 2012 at 17:48
  • Is adb devices giving you any active devices ? and what does uninstall.sh contain ?
    – Rick
    Jan 15, 2012 at 21:08
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)
    
1
  • This is the perfect solution, saved me hours of debugging
    – Edgar256
    Oct 28, 2021 at 6:41
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..

1
  • I guess the script was just written by Rabgs for you ;-) Dec 29, 2011 at 19:54
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);
            }
        }


    }
}
1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.