adb: a must-know CLI tool for Android development - Phelipe Teles

adb: a must-know CLI tool for Android development

3 min.
View source code

adb is a CLI that lets you control your Android device from your computer. In this post I want to share its features that made me enjoy more mobile development.

Initial Setup

First, you’ll need to connect your device to your computer, either through USB or purely via Wi-Fi (if you’re on Android 11+), but it’s also possible to use adb over Wi-Fi on Android 10 or lower if you follow some initial steps while connected over USB.

If everything works correctly, by running adb devices you should see something like this:

Bash
$ adb devices List of devices attachedemulator-5554	device0035714150	device

Record screen with adb shell screenrecord

To record your screen, it’s as simple as running adb shell screenrecord /sdcard/video.mp4. After you’re done, press Ctrl + C. Run adb pull /sdcard/video.mp4 ~/Videos/video.mp4 to get the video into your computer.

It’s also useful to know some of the command line options, my favorite ones being to limit the video size with --size and recording time with --time-limit.

For example, I typically run adb shell screenrecord --size 320x568 --time-limit=120 /sdcard/video.mp4.

Capture screen with adb shell screencap

This is straightforward, it just captures the screen with adb shell screencap /sdcard/img.png, then getit locally with adb pull /sdcard/img.png ~/Images/img.png.

Debugging with adb logcat

This has proved useful to me so many times. In the context of React Native development, this is usually less useful in debug builds. But in release builds it’s sometimes the only way to debug when something wrong happens.

For example, when an app crashes or some SDK call is not working, you’ll probably be able to see why with adb logcat.

The downside is that the output can be overwhelming, since it’s a huge wall of text that is growing constantly. So it pays off to know how to filter it, e.g. if I wanted to see only logs tagged with Sentry, ReactNative and ReactNativeJS at any priority level:

Bash
$ adb logcat Sentry:* ReactNative:* ReactNativeJS:* *:S

The official Google documentation explains nicely how this works.

Networking with adb reverse

Imagine you want to see your Storybook files in a mobile web browser. If you go to localhost:6006 in your computer’s browser, it works, but nothing shows up in your mobile device’s browser, since no process is bound to port 6060 there.

You can solve this problem by running adb reverse tcp:6006 tcp:6006. Now your mobile device will have access to the server running on your computer.

An example from React Native is the Metro bundler, that usually serves the bundled JS of your app at port 8081, so we need to run adb reverse tcp:8081 tcp:8081 to make the server available in your mobile device. This is usually done under the hood when we run npx react-native run-android, but if the device can’t find the JS bundle or is stuck loading it we usually need to run it again.

There’s also adb forward, in case you need to make a web server running on your phone also available in your computer.

Start/kill apps

You can start and kill an app with the adb shell am, in which am stands for Activity Manager.

Given the app’s package name, you can start it with:

Bash
$ adb shell am start -n com.company.app/.MainActivity

And kill it with:

Bash
$ adb shell am force-stop com.company.app

You can also open a URL, this is specially useful to test deep links:

Bash
$ adb shell am start -a android.intent.action.VIEW -d company://Screen

Install/uninstall apps

You can install an .apk file with adb install app.apk.

And uninstall it with adb uninstall com.company.app.

This is usually how I do it, but there’s also the pm command, which stands for Package Manager, that is a more powerful interface to manage apps. Apart from enabling you to install and uninstall apps, you can also clear data, grant/revoke permissions etc.