Set Up Android Debug Bridge (ADB) for Debugging Our App

Introduction

The Android SDK Platform-Tools package offers a rich command-line tool, Android Debug Bridge (ADB), that lets us communicate with your Android device. This tool allows you to install and debug applications using an easy-to-use command and exposes a UNIX shell that lets you run various commands.

Connecting device with ADB

To use ADB with a USB-connected Android device, we need to ensure that “USB Debugging” is enabled in the device system settings under “Developer Options.”

Note: This option is hidden by default. To make it visible, go to “Settings > About phone” and tap “Build number” seven times.

We can confirm if the ADB connection was established by running the following command:

$ adb devices

Note: To execute this command, make sure that android_sdk/platform-tools/ is added to our PATH variable.

Common ADB commands

Now that the ADB is connected with our Android device, let’s go through some common commands that we should be familiar with.

Installing an app

We can use the install command to install an application to the connected device.

$ adb install path_to_apk

If we have multiple devices connected to ADB, we can specify the name of the device while issuing the command.

$ adb devices
List of devices attached
emulator-5554 device
emulator-5555 device

$ adb -s emulator-5555 install path_to_apk

Stop/start the ADB server

In some situations, we might want to stop the ADB server and restart it. Often during long periods of usage, ADB might stop working properly or might not show the connected devices. In such a scenario, these commands are quite useful:

adb kill-server
adb start-server
adb devices

Copying files to/from the device

We can copy files from the Android device to our development machine using the pull command.

adb pull /sdcard/foo.txt /home/username/foo.txt

Similarly, we can copy files from our development machine to the Android device using the push command.

adb push /home/username/foo.txt /sdcard/foo.txt

Calling activity/package manager

ADB can be used to issue commands with the activity manager (am) tool to perform various actions, such as start an activity, broadcast an intent, or modify the device’s screen properties.

For example, to start an activity in our app, we can use the following command:

adb shell am start -n yourpackagename/.activityname

Similarly, we can call the package manager (pm) to perform actions and queries on installed app packages.

For example, to uninstall an app, we can issue the following command:

adb shell pm uninstall com.example.appName

Capturing screenshots using ADB

The ADB shell also allows us to capture screenshots of the device display. We can use the screencap command to capture a screenshot of the display.

adb shell screencap /sdcard/screen.png

Conclusion

This lesson provided a brief introduction to some of the most commonly used ADB commands, which you’ll find useful throughout your Android development journey. Keeping these commands handy speeds up the development immensely since you can easily perform tasks like launching an intent or clearing app data.

Get hands-on with 1200+ tech skills courses.