Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I adb install an apk to multiple connected devices?

People also ask

Which adb option should you use if you have multiple devices connected?

If multiple emulators are running and/or multiple devices are attached, you need to use the -d, -e, or -s option to specify the target device to which the command should be directed. The table below lists all of the supported adb commands and explains their meaning and usage.

Can you install APK from adb?

ADB (Android Debugging Bridge) can be used to execute commands on your VR device. It's mostly used to install applications (APK files) from a Windows PC or Mac with a device connected with USB.

How do I run adb from a specific device?

Use the -s option followed by a device name to select on which device the adb command should run. The -s options should be first in line, before the command.


You can use adb devices to get a list of connected devices and then run adb -s DEVICE_SERIAL_NUM install... for every device listed.

Something like (bash):

adb devices | tail -n +3 | cut -sf 1 -d " " | xargs -iX adb -s X install ...

Comments suggest this might work better for newer versions:

adb devices | tail -n +2 | cut -sf 1 | xargs -iX adb -s X install ...

For Mac OSX(not tested on Linux):

adb devices | tail -n +2 | cut -sf 1 | xargs -I {} adb -s {} install ...

The other answers were very useful however didn't quite do what I needed. I thought I'd post my solution (a shell script) in case it provides more clarity for other readers. It installs multiple apks and any mp4s

echo "Installatron"

for SERIAL in $(adb devices | tail -n +2 | cut -sf 1);
do 
  for APKLIST in $(ls *.apk);
  do
  echo "Installatroning $APKLIST on $SERIAL"
  adb -s $SERIAL install $APKLIST
  done

  for MP4LIST in $(ls *.mp4);
  do
  echo "Installatroning $MP4LIST to $SERIAL"
  adb -s $SERIAL push $MP4LIST sdcard/
  done
done

echo "Installatron has left the building"

Thank you for all the other answers that got me to this point.


Here's a functional one line command tailored from kichik's response (thanks!):

adb devices | tail -n +2 | cut -sf 1 | xargs -iX adb -s X install -r *.apk

But if you happen to be using Maven it's even simpler:

mvn android:deploy


Another short option... I stumbled on this page to learn that the -s $SERIAL has to come before the actual adb command! Thanks stackoverflow!

for SERIAL in $(adb devices | grep -v List | cut -f 1);
do `adb -s $SERIAL install -r /path/to/product.apk`;
done

Generalized solution from Dave Owens to run any command on all devices:

for SERIAL in $(adb devices | grep -v List | cut -f 1);
do echo adb -s $SERIAL $@;
done

Put it in some script like "adb_all" and use same way as adb for single device.

Another good thing i've found is to fork background processes for each command, and wait for their completion:

for SERIAL in $(adb devices | grep -v List | cut -f 1);
do adb -s $SERIAL $@ &
done

for job in `jobs -p`
do wait $job
done

Then you can easily create a script to install app and start the activity

./adb_all_fork install myApp.apk
./adb_all_fork shell am start -a android.intent.action.MAIN -n my.package.app/.MainActivity

I liked workingMatt's script but thought it could be improved a bit, here's my modified version:

#!/bin/bash

install_to_device(){
local prettyName=$(adb -s $1 shell getprop ro.product.model)
echo "Starting Installatroning on $prettyName"
for APKLIST in $(find . -name "*.apk" -not -name "*unaligned*");
  do
  echo "Installatroning $APKLIST on $prettyName"
  adb -s $1 install -r $APKLIST
  adb -s $1 shell am start -n com.foo.barr/.FirstActivity;
  adb -s $1 shell input keyevent KEYCODE_WAKEUP
  done
  echo "Finished Installatroning on $prettyName"
}

echo "Installatron"
gradlew assembleProdDebug

for SERIAL in $(adb devices | tail -n +2 | cut -sf 1);
do 
  install_to_device $SERIAL&
done

My version does the same thing except:

  • it finds the apks from the root of the project
  • it installs to every device simultaneously
  • it excludes the "unaligned" versions of the apks (these were just being installed over by the aligned versions anyway
  • it shows the readable names for the phones instead if their device ids

There's a few ways it could still be improved but I'm quite happy with it.