ADB for React Native Developers
This time that I've been developing mobile applications with React Native, many times I had to open AndroidStudio to do something related to the device (emulator or physical device).
For those who have ever developed for Android, you know that AndroidStudio takes a couple of minutes to start and consumes a lot of memory. Time and resources that are not worth being used for simple and repetitive tasks...
Learning a couple of ADB commands has helped me to do those tasks in a simpler and faster way using the command line, not to mention that I really like doing things with the terminal :P.
What is ADB?
According to the official definition, Android Debug Bridge is a command-line tool that allows you to interact with the device you are debugging an Android application on development. Basically, a command.
ADB has many options and arguments that allow you to execute different tasks on the emulator or physical device. But the basic structure is:
adb [option] command
The option argument is unnecessary if you only have one device or emulator connected and command is the action you want to execute (it is a required argument).
There is a very long list of commands that you can execute with adb to interact with the device storage, to set up ports, configure WiFi or network options and even take screenshots or record the device screen.
To see all the alternatives you can execute from a command line
adb --help
or check the official documentation. In this post, I will describe only a few that I use frequently...
1. Port Forwarding
This is a classic, setting up port forwarding. If you use libraries like Storybook or Reactotron you find that you need to communicate the device port that these libraries use to a port on your development machine, adb helps you with this!
adb reverse <remote-port> <local-port>
In the case of Storybook the remote port is tcp:7007, so the adb command is:
adb reverse tcp:7007 tcp:7007
2. Open the development menu
Yes, I know that this can also be done with the keyboard combination Ctrl + M (or Cmd + M for macOS users). But I do it with command line just because! xD
adb shell input keyevent 82
3. Copy files from and to the device storage
To copy a file to the device storage:
adb push <local-file> <remote-location>
For example:
adb push ~/Downloads/image.png /sdcard/Downloads/image.png
To copy a file from the device storage to your development machine:
adb pull <remote-file> <local-location>
For example:
adb pull /sdcard/Downloads/voice.aac ~/Downloads/voice.aac
4. Install an app
If you have a valid .apk installer that you want to install on a device from the command line:
adb install <apk-file>
5. Take a screenshot
It is also useful to take a screenshot of the device screen from the command line, for example, to share it with your team or to use it in a bug report:
adb shell screencap <remote-file>
For example:
adb shell screencap /sdcard/Downloads/screenshot.png
6. Record the device screen
Just like taking a screenshot, you can also record the device screen with the following command:
adb shell screenrecord <remote-file>
To stop the recording just press Ctrl + C in the terminal.
For example:
adb shell screenrecord /sdcard/Downloads/record.mp4# 10 seconds laterCtrl + C
Bonus!
This is not an ADB command, but it is a useful command to open an Android emulator from the command line.
Inside the directory where you have the Android SDK installed and inside the emulator directory (/path/to/Android/Sdk/emulator) there is an executable called emulator that you can use to start an emulator:
cd /path/to/Android/Sdk/emulator./emulator @<emulator-name>
And to have a list of the available emulators:
./emulator -list-avds
In my case the output of the previous command is:
# Example Response!Pixel2Pixel2oldPixel_2_API_25Pixel_2_API_26
So an example command to start the Pixel2 emulator is:
./emulator @Pixel2
To ease the process I created a symbolic link to the pointing to the executable /path/to/Android/Sdk/emulator/emulator ;)
Basically, those are the commands that I use daily to develop with React Native for Android. Cheers!