The Almighty ADB Tool For Android

Iulian

Iulian

QA
Iulian joined Softvision in the autumn of 2014 with a 3 years experience in mobile quality assurance at the time. Since then he's been part of a great team focused on Android Quality assurance in which he contributed by helping the team work more effectively with the available tools. He enjoys reading about new technologies, helping others understand them and in his free time, he's known to be in his man cave playing video games.
Iulian

Latest posts by Iulian

This article does not intend to tell you how to do your job or that this is the way to do certain things, it’s only meant to open your eyes to the many possibilities that can exist with the use of ADB, the rest is up to you to make the most of it.

ADB is a versatile command-line tool that lets you communicate with an Android device (long version here) that helps a user communicate with an Android device. With it, you can do anything a normal user can do, but faster and from a mac/pc.

To use it you need to install Android Studio than with the ‘cd‘ command navigate to the same directory that the ADB tool is located in, by default it’s located in /Users/<name>/Library/Android/sdk/platform-tools/ for mac and C:\Users\username\AppData\Local\Android\sdk\platform-tools\  to avoid having to move to the folder that the ADB is located in every single time you want to work with it you can set the ADB path in Mac and Windows. Now that we know how to use ADB, connect a device that has USB debugging onto your PC and you can start using ADB commands, like ./adb devices. You should use it because it saves you time, with the ADB tool you can;

  • Install/uninstall/upgrade the app with a few simple commands as opposed to copying the .apk file to the device and using a file manager to install the app after you find it
  • Make screenshots/videos (on Android 4.4+), Clear Data/Force close an app, send a deeplink to an app, find out the Android version is running on the device, find out the version for a specific app, simulate different taps or swipes like hitting the Home button or switching text fields, open the app, switch the orientation of the device and my favourite,paste whatever you write in Terminal into a text field on the device (makes it very easy to write long strings of characters like UUIDs into the device) Open the System App Info screen
  • It can also take away some of the boredom that comes with doing the same thing over and over again – an example would be when you have to uninstall the previous version of the app + install a new one + open it | Create a new or enter valid information for a credit card by entering valid information in multiple text fields. This (and much more) can be done with ADB and a simple bash script and you can not only speed up the process, but you won’t have to manually input or remember specific data (like credit card number or expiration date) ever again, so again IT SAVES YOU TIME.

Going forward I’ll show you just a few useful commands but remember, you can do everything that a normal user can and much more, so if you want to do something that’s not here, just google it and you shall receive.

As mentioned above we can make all sorts of things and before showing you a few commands with the ADB tool, here’s a color map to help you better understand how the commands are composed:

  • The parts of the commands that shouldn’t be changed are in Black
  • Paths to different objects are marked with Green
  • Application builds (.apk files) or package names (Google equivalent of App ID) are marked with Red
  • Links are marked with Blue
  • Examples of text or examples of how to name newly created files are marked with Magenta
  • Key events that can will be exchanged with different values for different experiences are marked with Orange

Install the app

  • adb install /path/to/the/build/name.of.the.apk

Upgrade the app

  • adb install -r /path/to/the/build/name.of.the.apk

Uninstall the app

  • adb uninstall package.name

Uninstall the app but keep the secret screen settings + location for future installs

  • adb shell pm uninstall -k package.name

Make a screenshot and store it on the device

  • adb shell screencap -p /sdcard/example.png

Copy the screenshot (or anything else) from the device into the folder from which you’ve run the adb pull…” command in Terminal

  • adb pull /sdcard/example.png

Start video recording, by default it lasts for 3 min, if you want it to stop earlier than that just press Ctrl+c

  • adb shell screenrecord /sdcard/example.mp4

Copy the video (or anything else) from the device into the folder from which you’ve run the adb pull…” command in Terminal

  • adb pull /sdcard/example.mp4

Force close the app

  • adb shell am force-stop package.name

Open the app

  • adb shell monkey -p package.name -c
    android.intent.category.LAUNCHER 1

Clear data

  • adb shell pm clear package.name

Send a deeplink to the device (this one takes you to Google on Chrome)

  • adb shell am start -W -a android.intent.action.VIEW -d
    http://www.google.comcom.android.chrome

Paste what you wrote in Terminal directly in the device

  • adb shell input text
    This_will_be_wrote_into_the_selected_text_field

    • Note that for inputting some special characters like ( ) < > | ; & * \ ~ ” ‘ you need to write them with encapsulated ( ” ” ) and with  \ in front, for the space character you need to replace it with ”%s”, for example, a command that wants to say I am in my room & mom is at work. needs to be written like below:
  • adb shell input text
    “I%sam%sin%smy%sroom%s\&%smom%sis%sat%swork.”

Find out what version of the a specific app app you have installed

  • adb shell dumpsys package package.name | grep versionName

Find out the Android version running on the device

  • adb shell getprop ro.build.version.release

Open the System App Info screen within the Settings of the device, this screen:

  • adb shell am start -a android.settings.APPLICATION_DETAILS_SETTINGS package:package.name

Simulate different Key events like Home, Back, switch to the next field and many, many others. You can either use its key event name or its corresponding int value as shown below:

  • Simulate the Home button:
    • adb shell input keyevent 3
    • adb shell input keyevent KEYCODE_HOME
  • Simulate the Back button:
    • adb shell input keyevent 4
    • adb shell input keyevent KEYCODE_BACK
  • Simulate the Enter button
    • adb shell input keyevent 66
    • adb shell input keyevent KEYCODE_ENTER

These key events and many others can be found here.

Switch orientation of the device with ‘adb’, for this you must disable auto rotation first, then do your rotation change, then leave it off or turn auto rotation on again. You can do all this with the commands below:

    • Auto rotation off:
      • adb shell content insert –uri content://settings/system –bind name:s:accelerometer_rotation –bind value:i:0
    • Auto rotation on
      • adb shell content insert –uri content://settings/system –bind name:s:accelerometer_rotation –bind value:i:1
    • Rotate to landscape
      • adb shell content insert —uri content://settings/system –bind name:s:user_rotation –bind value:i:1
    • Rotate portrait
      • adb shell content insert –uri content://settings/system –bind name:s:user_rotation –bind value:i:0

You can get more efficient results when combining the ADB tool with simple, easy to write and maintain bash scripts + alias set in .bash_profile, this way you can combine multiple ADB commands and you won’t have to run the commands by hand one by one.

For example you can group a deep link command to take you to a specific screen within the app then use the “adb shell input text” (type within the text field on the device whatever you wrote in Terminal) command to input different info, you can use this to input Credit Card information when needed, it helps because you don’t have to remember the details of the card nor do you have to type the info by hand. Another example would be when you have to uninstall a previous build + install a new one + open the app, this can also be done with a simple script using the install uninstall and open ADB commands.

The possibilities are endless and the time that this might save you might be overwhelming. Your imagination is the limit.

 

 

 

 

 

Share This Article


Iulian

Iulian

QA
Iulian joined Softvision in the autumn of 2014 with a 3 years experience in mobile quality assurance at the time. Since then he's been part of a great team focused on Android Quality assurance in which he contributed by helping the team work more effectively with the available tools. He enjoys reading about new technologies, helping others understand them and in his free time, he's known to be in his man cave playing video games.
Iulian

Latest posts by Iulian

1Comment
  • Mythri B S
    Posted at 09:27h, 29 June Reply

    Please share if you have any demo/video on this.

Post A Comment