ADB

Helpful ADB commands

Alex Zhukovich 4 min read
Helpful ADB commands
Table of Contents

The Android Debug Bridge is better known as ADB. It is a command-line utility, included in Android SDK, which allows us to interact with an Android device or emulator over a USB connection, allowing files to be transferred, installing applications, changing permissions of applications, taking screenshots, and much more.

If you are not familiar with ADB (Android Debug Bridge), I recommend starting with the official ADB page.

This article contains information about a few ADB commands that allow us to interact with devices and test applications more efficiently.

We can interact with any device or emulator when connected via USB or Wi-Fi.

ADB over Wi-Fi

To connect an Android device via Wi-Fi we need to use the adb connect <ip-address> command.

We can find the current ip address by going into the "Settings > About device > Status" screen on Android device.

Let's imaging that the IP address is the following: 192.168.1.42.

# Connect to the device by its IP address
adb connect 192.168.1.42

# Connect to the device by its IP address with specific port
adb connect 192.168.1.42:5554

# Disconnect from the device by it's IP address
adb disconnect 192.168.1.42

Note: The adb connect and adb disconnect use a default port: 5555. If you cannot connect to the device by it's IP address, this is most probably due to your device not being able to listen for a TCP/IP connection on 5555 port. To fix this issue you will need to connect the device via USB and execute the following command:

adb tcpip 5555

Helpful ADB commands

The ADB supports many commands which allow us to interact with Android devices or emulators. You can find the description of commands here.

However, I will share a few commands that will give you benefits in some particular scenarios when testing Android applications.

Send input event

Sometimes you can have a situation when you need to enter specific text during the manual verification of the application, like a voucher code for example. This can be a real life case that can be used when you don't have access to resources with such code on the device.

We can enter any text to the selected input field by adb shell input text "<text>" command.

# all spaces should be replaced with "%s"
adb shell input text "insert%syour%stext"

We can emulate pressing the hardware buttons with adb shell input keyevent <key|action-name> command. You can find the keys of hardware buttons here.

adb shell input keyevent 26
adb shell input keyevent POWER

Testing app with Monkey testing approach

Monkey testing is a technique in software testing where a user tests the application or a system by proving random inputs (type text, clicks, pressing hardware button, etc) and checking for the application behaviour, or by seeing if the application will crash.

ADB has a possibility for such type of testing. We can use the adb shell monkey -p <package> -v <event-count> -s <seed>. The seed parameter is needed if we want to reproduce the testing session, which in that case, similar events will be generated during the next run.

adb shell monkey -p com.alexzh.mapnotes -v 10000 -s 100

Changing permissions

We can change permissions of installed application by using the adb shell pm grant <package> <permission> or adb shell pm revoke <package> <permission> commands.

To grant permissions(s) we can use the adb shell pm grant <package> <permission> command.

adb shell pm grant com.alexzh.mapnotes android.permission.ACCESS_FINE_LOCATION
adb shell pm grant com.alexzh.mapnotes android.permission.ACCESS_COARSE_LOCATION

We can grand all requested permissions to application by adding -g to the adb shell pm grant command.

adb shell pm grant -g com.alexzh.mapnotes 

To revoke permissions(s) we can use the adb shell pm revoke <package> <permission> command.

adb shell pm revoke com.alexzh.mapnotes android.permission.ACCESS_FINE_LOCATION
adb shell pm revoke com.alexzh.mapnotes android.permission.ACCESS_COARSE_LOCATION

Note: When we install an application with the adb install <apk> command, we can add the -g parameter, and all permissions for the application will be granted.

adb install -g mapnotes.apk

Simulate process death

Android OS can decide to kill a process when the application is running in the background and the device is running low on memory. This is to free up memory and give it to another application or service who have more priority.

We want to be ready for such situation and want to avoid any potential problem when our app will be killed by the OS.

We can simulate such situation with the adb shell am kill <package> command.

adb shell am kill com.alexzh.mapnotes

Summary

ADB tool has many possibilities, which can help interact with the device, install applications, verify different use cases, etc.

We discussed commands, which can be helpful to developers and QA engineers.

  • adb shell input provides the possibility to enter text, emulate pressing hardware buttons, do swipe gestures, etc.
    • adb shell input text "insert%syour%stext"
    • adb shell input keyevent 26
    • adb shell input keyevent POWER
  • adb shell monkey -p <package> -v <event-count> -s <seed> command allows us to execute tests use the monkey testing approach which can verify stability of our application by sending random inputs to the device (click, enter text, pressing hardware buttons, etc).
  • adb shell pm grant <package> <permission> and adb shell pm revoke <package> <permission> provide us with the possibility to change the permissions of installed application. It helps to start application with a required set of permissions. This approach can then be used for reproducing issues with specific permissions.
  • adb shell am kill <package-name> helps to simulate the process "death" which can happen when the OS decides to kill an application because of running into low memory.

Documentation for ADB commands can be found here.


Mobile development with Alex

A blog about Android development & testing, Best Practices, Tips and Tricks

Share
More from Mobile development with Alex
ADB commands: Accessibility
ADB

ADB commands: Accessibility

Many accessibility features are integrated into the Android, and others can be installed and used via apps. We will explore how to change them via UI or ADB commands.
Alex Zhukovich 6 min read

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Mobile development with Alex.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.