Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running adb commands on all connected devices

Tags:

android

adb

Is there a way of running adb commands on all connected devices? To uninstall an app from all connected devices with "adb uninstall com.example.android".

The commands I am interested in is mainly install and uninstall.

I was thinking about writing a bash script for this, but I feel like someone should have done it already :)

like image 291
Heinrisch Avatar asked Jul 26 '13 13:07

Heinrisch


People also ask

Which adb option should be used if multiple devices are connected?

If there\'s only one emulator running or only one device connected, the adb command is sent to that device by default. 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.

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.

How do I enable adb over TCP IP?

Type adb tcpip 5555 in the command line or Terminal and press Enter. Find your phone's IP address in Settings > About Phone > Status > IP Address. Back in the command line or Terminal, type adb connect [your Android's IP address]. Finally, press Enter again.


2 Answers

Create a bash file and name it e.g. adb+:

#!/bin/bash adb devices | while read -r line do if [ ! "$line" = "" ] && [ "$(echo "$line" | awk '{print $2}')" = "device" ] then     device=$(echo "$line" | awk '{print $1}')     echo "$device" "$@" ...     adb -s "$device" "$@" fi done 

Usage: ./adb+ <command>

like image 197
Oli Avatar answered Oct 03 '22 19:10

Oli


Building on @Oli's answer, this will also let the command(s) run in parallel, using xargs. Just add this to your .bashrc file:

function adball() {     adb devices | egrep '\t(device|emulator)' | cut -f 1 | xargs -t -J% -n1 -P5 \           adb -s % "$@" } 

and apply it by opening a new shell terminal, . ~/.bashrc, or source ~/.bashrc.

  1. If you only want to run on devices (or only on emulators), you can change the (device|emulator) grep by removing the one you don't want. This command as written above will run on all attached devices and emulators.
  2. the -J% argument specifies that you want xargs to replace the first occurrence of % in the utility with the value from the left side of the pipe (stdin).
    NOTE: this is for BSD (Darwin / Mac OS X) xargs. For GNU/Linux xargs, the option is -I%.
  3. -t will cause xargs to print the command it is about to run immediately before running it.
  4. -n1 means xargs should only use at most 1 argument in each invocation of the command (as opposed to some utilities which can take multiple arguments, like rm for example).
  5. -P5 allows up to 5 parallel processes to run simultaneously. If you want instead to run the commands sequentially, simply remove the entire -P5 argument. This also allows you to have two variations of the command (adball and adbseq, for example) -- one that runs in parallel, the other sequentially.

To prove that it is parallel, you can run a shell command that includes a sleep in it, for example:

$ adball shell "getprop ro.serialno ; date ; sleep 1 ; date ; getprop ro.serialno" 

You can use this to run any adb command you want (yes, even adball logcat will work! but it might look a little strange because both logs will be streaming to your console in parallel, so you won't be able to distinguish which device a given log line is coming from).


The benefit of this approach over @dtmilano's & approach is that xargs will continue to block the shell as long as at least one of the parallel processes is still running: that means you can break out of both commands by simply using ^C, just like you're used to doing. With dtmilano's approach, if you were to run adb+ logcat, then both logcat processes would be backgrounded, and so you would have to manually kill the logcat process yourself using ps and kill or pkill. Using xargs makes it look and feel just like a regular blocking command line, and if you only have one device, then it will work exactly like adb.

like image 30
Joe Avatar answered Oct 03 '22 21:10

Joe