http://pinada.deviantart.com/art/Android-Cool-Wallpaper-201478868

Android Debug Bridge (ADB) command scripting

The Android Debug Bridge (ADB) is a useful tool for testing and debugging, though it can be cumbersome to use given its broad set of options. For example, testing an app might require frequent authentication, and just sending the username with adb looks like this:

adb shell input text username

Command aliases can simplify this. In bash, these can be added to the startup script, such as .bashrc:

alias adbusername=’adb shell input text username’

Now the username can be passed by simply typing:

adbusername

Windows users can do something similar using Doskey.

The password could be sent using a similar alias:

alias adbpassword=’adb shell input text password’

Since other text might be sent, and those aliases can be chained together, you could setup the following:

alias adbinput=’adb shell input’

alias adbtext=’adbinput text’

alias adbusername=’adbtext username’

alias adbpassword=’adbtext password’

Next, add in some keyevents for navigating between the fields:

alias adbkey=’adbinput keyevent’

alias adbtab=’adbkey 61’

alias adbenter=’adbkey 66’

Now, the whole login flow can be performed using a single command, such as applogin:

alias applogin=’adbusername;adbtab;adbpassword;adbtab;adbenter’

This can be handy for simple and frequent app interaction while testing. I use scripts such as these to navigate through initial activities during a fresh app launch to get me to the point where I need to test or debug quickly.

A similar approach to all of this is to use monkeyrunner, or continuing down that route, testing using something like calabash, but for something quick, I find that this fits the bill.

Here is a larger set of adb option aliases, comprising my basic day to day needs:

alias adbinput='adb shell input'

alias adbtext='adbinput text'

alias adbkey='adbinput keyevent'

alias adbuser='adbtext username'

alias adbpass='adbtext password'

alias adbtab='adbkey 61'

alias adbdown='adbkey 20'

alias adbback='adbkey 4'

alias adbenter='adbkey 66'

alias applogin='adbuser;adbback;adbtab;adbpass;adbtab;adbtab;adbenter'

# navigate splash screen

alias appconf=’for i in {1..3};do adbtab;done;adbenter’

# select test environment

alias apptest=’appconf;adbtab;adbenter’

# select production environment

alias appprod=’appconf;for i in {1..3};do adbtab;done;adbenter;

 

Interesting? 

Do you find this sort of work interesting? Do you love quaint Bucks County Pennsylvania? Are you an amazing Android Developer? Then we would love to hear from you!

Android Image Credit: Copyright Pinada, CC BY-ND 3.0

Leave a Reply

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