How can I adb install an apk to multiple connected

2019-01-10 03:56发布

问题:

I have 7 devices plugged into my development machine.

Normally I do adb install <path to apk> and can install to just a single device.

Now I would like to install my apk on all of my 7 connected devices. How can I do this in a single command? I'd like to run a script perhaps.

回答1:

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


回答2:

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.



回答3:

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



回答4:

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


回答5:

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


回答6:

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.



回答7:

The following command should work:

$ adb devices | tail -n +2 | head -n -1 | cut -f 1 | xargs -I X adb -s X install -r path/to/your/package.apk

adb devices returns the list of devices. Use tail -n +2 to start from the 2nd line and head -n -1 to remove the last blank line at the end. Piping through cut with the default tab delimiter gets us the first column which are the serials.

xargs is used to run the adb command for each serial. Remove the -r option if you are not re-installing.



回答8:

With this script you can just do:

adb+ install <path to apk>

Clean, simple.



回答9:

If you don't want use the devices that have not enabled adb; use this

Mac/Linux

adb devices | grep device | grep -v devices | awk '{print$1}' | xargs -I {} adb -s {} install path/to/yourApp.apk 

adb devices | grep device | grep -v devices | cut -sf 1 | xargs -I {} adb -s {} install path/to/yourApp.apk


回答10:

Use this command-line utility: adb-foreach



回答11:

With Android Debug Bridge version 1.0.29, try this bash script:

APK=$1

if [ ! -f `which adb` ]; then
    echo 'You need to install the Android SDK before running this script.';
    exit;
fi

if [ ! $APK ]; then
    echo 'Please provide an .apk file to install.'
else
    for d in `adb devices | ack -o '^\S+\t'`; do
        adb -s $d install $APK;
    done
fi

Not sure if it works with earlier versions.



回答12:

PowerShell solution

function global:adba() {
    $deviceIds = iex "adb devices" | select -skip 1 |  %{$_.Split([char]0x9)[0].Trim() } | where {$_ -ne "" }
    foreach ($deviceId in $deviceIds) {
        Echo ("--Executing on device " + $deviceId + ":---")
        iex ("adb -s $deviceId " + $args)
    }
}

Put this in your profile file (notepad $PROFILE), restart your shell and you can invoke installations with :

adba install yourApp.apk


回答13:

The key is to launch adb in a separate process (&).

I came up with the following script to simultaneously fire-off installation on all of the connected devices of mine and finally launch installed application on each of them:

#!/bin/sh

function install_job { 

    adb -s ${x[0]} install -r PATH_TO_YOUR_APK
    adb -s ${x[0]} shell am start -n "com.example.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER

}


#iterate over devices IP-addresses or serial numbers and start a job 

while read LINE
do
    eval x=($LINE)
    install_job ${x[0]} > /dev/null 2>&1 &
done <<< "`adb devices |  cut -sf 1`"

echo "WATING FOR INSTALLATION PROCESSES TO COMPLETE"
wait

echo "DONE INSTALLING"

Note 1: the STDOUT and STDERR are suppressed. You won't see any "adb install" operation result. This may be improved, I guess, if you really have to

Note 2: you could also improve script by providing args instead of hardcoded path and activity names.

That way you:

  1. Don't have to manually perform install on each device
  2. Don't have to wait for one install to finish in order to execute another one (adb tasks are launched in parallel)


回答14:

Originated from here: Make The Previous Post A Mass APK Installer That Does Not Uses ADB Install-Multi Syntax


@echo off

:loop
      ::-------------------------- has argument ?
      if ["%~1"]==[""] (
        echo done.
        goto end
      )
      ::-------------------------- argument exist ?
      if not exist %~s1 (
        echo error "%~1" does not exist in file-system.
      ) else (
        echo "%~1" exist
        if exist %~s1\NUL (
          echo "%~1" is a directory
        ) else (
          echo "%~1" is a file! - time to install:
          call adb install %~s1
        )
      )
      ::--------------------------
      shift
      goto loop


:end

pause

::: ##########################################################################
::: ##                                                                      ##
::: ##  0. run:   adb devices   - to start the deamon and list your device  ##
::: ##                                                                      ##
::: ##  1. drag&drop ANY amount of files (APK) over this batch files,       ##
::: ##                                                                      ##
::: ##       - it will install them one by one.                             ##
::: ##       - it just checks if file exists.                               ##
::: ##       - it does not checks if it is a valid APK package              ##
::: ##       - it does not checks if package-already-installed              ##
::: ##       - if there is an error you can always press [CTRL]+[C]         ##
::: ##           to stop the script, and continue from the next one,        ##
::: ##           some other time.                                           ##
::: ##       - the file is copied as DOS's 8.3 naming to you                ##
::: ##           don't need to worry about wrapping file names or renaming  ##
::: ##           them, just drag&drop them over this batch.                 ##
::: ##                                                                      ##
::: ##                                   Elad Karako 1/1/2016               ##
::: ##                                   http://icompile.eladkarako.com     ##
::: ##########################################################################



回答15:

This command works perfect adb devices | awk 'NR>1{print $1}' | xargs -n1 -I% adb -s % install foo.apk



回答16:

Since I can't comment on the answer by @Tom, this worked for me on OSX 10.13

adb devices | tail -n +2 | cut -sf 1 | xargs -IX adb -s X install -r path/to/apk.apk

(Change the little i to a big I)



回答17:

-Get all the apk stored in .apk folder

-Install and replace app on devices

getBuild() {
    for entry in .apk/*
    do
        echo "$entry"
    done
    return "$entry"
}

newBuild="$(getBuild)"

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 install -r $newbuild"
      adb -s $device install -r $newbuild
  fi
done