Android Dev Tips: HOW TO INSTALL OR UNINSTALL APKS ON MULTIPLE ANDROID DEVICES WITH A SINGLE COMMAND

So what’s it all about?

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 3 or 4 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 through 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 android development Ireland

usb hub android

 

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:

1
adb install -r path/to/the/app/to/install.apk

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:

1
2
3
4
5
6
7
 
- waiting for device -
error: more than one device and emulator
- waiting for device -
error: more than one device and emulator
- waiting for device -
Failure

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:

1
adb devices

This command will return something like:

1
2
3
List of devices attached
04544fawebf0b595dc device
13928weff7c device

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

1
adb -s (deviceID) install -r path/to/the/app/to/install.apk

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+

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash
# Script adb+
# Usage
# You can run any command adb provides on all your currently connected 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 "adb -s $device $@ ..."
      adb -s $device $@
  fi
done

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:

1
adb+ install -r path/to/the/app/to/install.apk

This gives us something like:

1
2
3
4
5
6
7
8
9
04544fabs23f0b595dc install -r /path/to/your/app/name_of_the_app.apk ...
4619 KB/s (13249277 bytes in 2.800s)
pkg: /data/local/tmp/name_of_the_app.apk
Success
 
1399834vskj28f7c install -r/path/to/your/app/name_of_the_app.apk ...
3608 KB/s (13249277 bytes in 3.585s)
pkg: /data/local/tmp/name_of_the_app.apk
Success

Troubleshooting

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

1
-bash: adb+: Permission denied

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

1
chmod 755 ./adb+

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

That’s it. The new script will go through 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!