adb: more than one device/emulator

I may be the odd one out here but when it comes to Android development, I often find myself in the terminal, which usually involves invoking adb (Android's universal tool for interacting with devices). I've written several shell aliases and functions to make this more pleasant, but unfortunately they tend to fall apart once I have more than one device connected (which I frequently do). Anyone else who finds themselves in a similar situation is probably all too familiar with the error message adb displays under such circumstances: adb: more than one device/emulator. I got so fed up of this that I decided to write a little wrapper script around adb to intercept this error message and then prompt me to select a device to re-run the same command on once more. I give you adb.

I didn't want to go rewriting all of my scripts/functions/aliases so the intention here is to install this little tool on your PATH, somewhere before the actual adb command. With that in place, a simple command like adb shell pm list packages goes from this:

$ adb shell pm list packages
adb: more than one device/emulator

to this:

$ adb shell pm list packages
0) Pixel 3a
1) Pixel 9 Pro XL
Select a device to run on: 1
package:com.android.systemui.auto_generated_rro_vendor__
...

You could also just run adb devices to get the serial number, then re-run your command with the -s flag (which is all my wrapper does). Be my guest! I can never remember the serial numbers for my devices though, so I find having the actual device name to be more useful. Additionally, emulator serial numbers aren't consistent, so depending on the order in which you launch them, they might have different serial numbers from day to day. Once you add emulators in the mix, you'll also get to choose them by the name you've given them in Android Studio as opposed to something generic like emulator-5554:

$ adb shell pm list packages
0) Medium_Phone
1) Pixel 3a
2) Pixel 9 Pro XL
Select a device to run on: 

It's still not a perfect solution (I've managed to break tab-complete for command completion) and I should probable try to detect if stdout is interactive or not (like when piping to a pager), but it works well enough to relieve some of my frustration. If you're interested in using it yourself, installation instructions (and some release artifacts) are available in the git repo. If you'd like to contribute, please write the code yourself (no LLMs please) and send me a patch via email.

As an aside, I decided to write this in Clojure, since that's the newest language in my toolbox. I figured it would be a good choice since Android developers almost always have Java installed and it would allow me to build something pretty simply. I'm rather pleased with the result, but I did end up relying on GraalVM to get the execution time back in the milliseconds range (as opposed to ~1 second without it) on Linux. On my Mac at work, the startup performance is good enough that I don't really feel the need for GraalVM. In all fairness to Clojure though, that's more on the JVM than anything else.