Quick Tip: How to install or uninstall APKs on multiple Android devices with just one command

Recently, while working on a mobile project, I found myself spending a lot of time connecting a device, uninstalling the previous version of the Application Under Test (AUT), installing the new one, and then disconnecting the device, just to do it again on another 6 or 7 devices (I would like to talk about why it is better to test on actual devices than in a simulator, but that topic deserves its own blog post).

For this post, I want to describe a solution I found to manage all of the attached devices with a single command. First I obtained this device, (you can get it thru Amazon here)which is just one of many different options available, depending on the arrangement, number of ports, shape, etc. I picked this one because it was cheap and the switches come in handy because we can exclude devices just by switching OFF that specific device and there’s no need to connect/disconnect any device.

usb hub

Just adding ports to your computer helps, but we need to get the most of them. When working with Android devices and we want, for example, to install an app, we have to type the following command in the terminal:

 This works fine when you have a single device attached because adb is designed for a single device, but when you have multiple devices attached, you get an error,  like so:

 We need to specify to adb the device we want to send the command to. To do so, we need to first get the device ID of every device connected by typing:

This command will return something like:

 Then to install on each device, we would need to copy each of these ids, and include them in the following command.

If I were to do this, I would need to copy each deviceID, type the above command seven times, pasting a different deviceID each time. Although this is better than connecting and disconnecting each device, this is still not the most productive way.

Searching the web for a solution, I found on Stackoverflow a script that solves this problem nicely.

Installation

First, copy and paste the following code in a new file called adb+

Then store this new file in the folder containing adb within your Android SDK installation folder. This should most likely be the  platform-tools folder.

Usage

Using the script is very simple. When we have more than one device connected (always use adb devices first, just to make sure all your devices are connected) we just need to work as if we were using adb, with any “adb” command replaced with “adb+”. For example, if we want to install an app:

This gives us something like:

Troubleshooting

If you find that you get the following when you attempt to run adb+

Your bash script simply isn’t executable. This is very easy to fix, run this command from the directory containing the script:

Now try it, you should see what you were expecting.

That’s it. The new script will go thru each device, performing the same command.  The app will install on all devices connected, and you will save a few minutes. What do you think? Is this going to help you with your mobile testing? Let us know!

13 thoughts on “Quick Tip: How to install or uninstall APKs on multiple Android devices with just one command”

  1. Pingback: Hacks | Pearltrees
  2. Hello, this script just takes the advantage of ADB and loops thru the devices, ADB doesn’t seem to accept more than 1 file as parameter, so this script doesn’t either.

    However I found that doing something like:


    adb+ install /path/to/the/first/apk/file & adb+ install /path/to/the/second/apk/file & adb+ install /path/to/the/third/apk & adb+ install /path/to/the/Nth/apk/file

    Would help, is not 1 command just the same one repeated several times 1 with each app, but is better than nothing.

    With help of my friend Joe Hansche, who also wrote an alternative for this script, to run commands in parallel here:

    http://stackoverflow.com/questions/17882474/running-adb-commands-on-all-connected-devices/23763079#2

    helped me to get something for you.

    If you want to install multiple apps on multiple devices you can just write another batch script

    for example create a file called adbBatchInstall and put this in it

    for file in "$@"; do adb+ install -r "$file"; done

    save that on your local installation on the same folder where adb and adb+ are

    He doesn’t recommend to add this directly to adb+ since the purpose of adb+ is to do *exactly* what adb does, but with several devices. You can use adb+ for getting versions of the devices for example, and some other stuff.

    For uninstalling, creating a file called adbBatchUninstall and adding

    for pkg; do adb+ uninstall "$pkg"; done

    inside of it, and saving it on the same place would do the trick.

    I tested it getting the following:

    $ adbBatchInstall /path/to/the/first.apk /path/to/the/second.apk
    04542dabf0b595dc install -r /path/to/the/first.apk ...
    3058 KB/s (13303777 bytes in 4.248s)
    pkg: /data/local/tmp/first.apk
    Success
    4d0023rf14c6c4181 install -r /path/to/the/first.apk ...
    3625 KB/s (13303777 bytes in 3.583s)
    pkg: /data/local/tmp/first.apk
    Success
    04542dabf0b595dc install -r /path/to/the/second.apk ...
    3172 KB/s (22565198 bytes in 6.946s)
    pkg: /data/local/tmp/second.apk.apk
    Success
    4d0023rf14c6c4181 install -r /path/to/the/second.apk ...
    4201 KB/s (22565198 bytes in 5.245s)
    pkg: /data/local/tmp/second.apk.apk
    Success

    Then to uninstall I ran

    $ adbBatchUninstall com.application1.something com.application2.something
    04542dabf0b595dc uninstall com.application1.something ...
    Success
    4d0023rf14c6c4181 uninstall com.application1.something ...
    Success
    04542dabf0b595dc uninstall com.application2.something ...
    Success
    4d0023rf14c6c4181 uninstall com.application2.something ...
    Success

    About running on different OS I just have run this on Mac OSX, but Im pretty confident that this will work on Linux also. However Im pretty sure it won’t work “as is” on Windows, you may have to need to tweak these commands a little bit for make it run there.

  3. This is a great blog post. A bit off the topic question, what was your experience of this USB hub like, on full load, with 7 android device connected? Have you tried that?

  4. Hello Gustavo,

    Please does this ADB works also on Windows? Or do I need to install package to windows? because now, the windows doesnt know the adb command. thanks

  5. I’m having trouble with the creation of the adb+. I’m running Mac OS X 10.9.5. When I try to run “adb+ install -r path/to/the/app/to/app.apk” it gives me several errors:
    ~/android/adb+: line 13:   if: command not found
    ~/android/adb+: line 14:   then: command not found
    ~/android/adb+: line 15:       device=List: command not found
    ~/android/adb+: line 16:       echo: command not found
    ~/android/adb+: line 17:       adb: command not found
    What am I doing wrong? I put he adb+ file in the same directory as adb and I ran the “sudo chmod 755 adb+” command.
    Please help!

    Thanks,
    John

      1. I can run the chmod command successfully. However when I run adb+ it gives me this output:
        ~/android/adb+: line 13: if: command not found
        ~/android/adb+: line 14: then: command not found
        ~/android/adb+: line 15: device=List: command not found
        ~/android/adb+: line 16: echo: command not found
        ~/android/adb+: line 17: adb: command not found

        why?

        1. Ok I got adb+ to work, and I got adbBatchInstall to work as well. For some reason it gives me an “Invalid APK file” error when the path or filename has spaces in it. If the apk file has no spaces it works fine – I installed 2 different apps on 2 different devices. I typically drag and drop the file onto terminal so it adds the proper syntax (“\” after spaces).

          Any thoughts?

          Thanks again Gustavo!

  6. 09:23 gusrivera@Gusmac: $ adb install -r /Users/user/Downloads/lat\ est.apk
    626 KB/s (20758246 bytes in 32.343s)
    pkg: /data/local/tmp/lat est.apk
    Success

    worked for me I just dragged it from the Downloads folder.

    You could try adding quotes at beggining and end of the path. With or without escaping the space (\)

Leave a Reply

Your email address will not be published. Required fields are marked *