Shorter ADB commands for Android Studio Emulator Device targeting

To target my specific emulator devices on Android Studio with my ADB (Android Debug Bridge) commands, I usually need to type long ones, like in the commands below where i wanna list the contents of a directory:

adb -s emulator-5554 ls /sdcard/Android/sub/directory/
adb -s emulator-5556 ls /sdcard/Android/sub/directory/

When I am working on a new feature or a bug-fix ticket, I try to avoid typing long commands and args over and over again, especially args that are as error-prone as emulator-5554.

How to shorten the ADB commands that target emulators (Mac OS):

Open your Zsh config file with an editor. Mine by habit is Vim. Like so:
vim ~/.zshrc

Use your arrow keys to go to the very bottom of the file and copy-paste below exact code:


What it does is creates a new command adbs. I think the addition of s at the end is appropriate because the flag we are trying to work with is the -s flag which stands for “Serial”, for identifying specific emulators.

The way the new command works is,

  • the $1 captures the first argument you type after adbs and saves it — for example, the 54 in adbs 54.
  • The shift bit removes the first argument from the front so that the actual command to be run will NOT be adb -s "emulator-5554" 54 logcat (wrong!)
  • Finally, the last line adb -s "emulator-55$port" "$@" runs the correct command, inserting the first argument (54 in our example) where $port is, followed by "$@" which grabs the rest of the command after the 54

Save the changes to the config file and then exit the editor. In Vim I press Esc key, then type :wq! and hit Enter. IIRC my partner questions something about my :wq! usage, but what he points out, I can’t remember.

After saving the config file, run the following command to make the new shortcut work immediately:

Usage Example:

Instead of typing the long version:
adb -s emulator-5554 ls /sdcard/Android/sub/directory/

We can just type:
adb -s 54 ls /sdcard/Android/sub/directory/

Extra: to know what serial numbers of running devices you got, run below command

adb devices

Random thoughts: I am grateful to my very first team lead as a software developer (Peng) because he’s the one who inspired me to become comfortable with command line interfaces. Remembering how much I grew as a dev in that team due to high-traffic production databases that we were managing and debugging in realtime. Oh, the source command on the other hand, was something I learned from my current team lead, Maru. I like how much I am growing in this current team, not only as a dev, but also as a person.

Credit goes to Google Gemini for the above adbs custom command. I had no idea how to write that until today. hehe

Related Posts:

Posts that may be related to "Shorter ADB commands for Android Studio Emulator Device targeting":

Catzie

An odd human being who happens to have a variety of ever-changing interests, but right now they are programming, making up silly song/rap lyrics, K-pop, drawing, creating unique dessert/drink flavors, obsessing about finding out how some things works, automation, anime, video games... Ran online dessert shops Cookies PH and Catzie's Cakery in her past life.

Leave a Reply

Your email address will not be published. Required fields are marked *