Using Android Debug Bridge
As we are doing Android development with a variety of tools, Android Debug Bridge, or ADB, is a command-line utility among the most important ones. Like others, ADB is also integrated into Google's Android Studio IDE. If you have not started using it, please check out the introductory tutorial, "Using Android Studio."
We will cover the most commonly used options in this tutorial and touch on some advanced examples. Topics include what ADB is, where to find it, how to use it to interact with connected devices and emulators, and point out the Android Studio integration.
What and Where is Android Debug Bridge (ADB)?
Android Debug Bridge (ADB) is a very powerful command-line utility you can use to communicate with Android-enabled devices or emulators. It is implemented in the server-client fashion and allows you to directly operate from the system level through shell commands on handheld devices. The executable of ADB, adb.exe, can be found under Android SDK's child folder, platform-tools/. For example, in my case, it is under C:\Users\Chunyen\AppData\Local\Android\sdk\platform-tools. I recommend you add your folder path to the evironment variable.
Figure 1 shows the Android Studio integration, which you can see under Tools -> Android -> Enable ADB Integration. This should be enabled as a default. Once you have a connected device, you can see the device name in the drop-down menu and device's system messages under logcat. Figure 2 shows the related settings on a mobile device. To do debugging through USB connection, this is a required step.
Figure 1: ADB in Android Studio
Figure 2: Setup on a Mobile Device
Starting and Stopping ADB
To start the ADB server, simply use the following from the command line.
If ADB is already running, you will not get any message back except for the shell prompt. If it is started for the first time, you should see something similar to the following:
* daemon not running * * starting it now on port 5037 * * daemon started successfully *
To stop the ADB server, issue the following command. Sometimes, I do this more than one time to make sure the preceding message will be displayed when I start it again.
Connecting with Devices or Emulators
If you have Android Studio with the default settings and drivers, connect a handheld device through a USB cable, issue the adb devices command, and you should see a returned message listed with your device id. The example in Figure 3 is to connect another smart watch device through a designated TCP port number 4444. Whatever data is sent to that port, it will be forwarded to the smart watch. Once you list all the devices through ADB, localhost:4444 is used as the id for the smart watch. Once you have more than one connected handhelds, you can use adb -s device_id ... to direct commands to a specific device. In the example, I have two devices: a Google Nexus 5 mobile phone and a Motorola Moto 360 smart watch, as in Figure 3.
Figure 3: ADB and Devices
Logs and Reports
One of ADB's most powerful features is allowing you to monitor the system events in real time through adb logcat. Before we use that option, we can start by cleaning up whatever is already in the logs with adb logcat -c. To direct the logs onto a file, you can do logcat -d > mylogs.txt. In Figure 4, the example captures the real time logs on standard output and pipes them to Microsoft Windows findstr with the filter string "google"; in other words, only displaying lines containing this substring.
To save the bug report onto a user file titled myreports.txt, you do the following.
adb bugreport > c:\temp\myreports.txt
Figure 4: ADB logcat
Installing Apps and Copying Files
Installing and uninstalling apps is straightforward, but there is a little difference between the two. Assume we have an app with the filename MyFirstGame.apk and its package name is com.chunyenliu.mygames. If you do not exactly remember the package name for your app after a while, you can use a system shell by doing adb shell and then pm list packages | grep <any_substring_in_the_app_name>. You should be able to visually check if your app's package name is on the list. Finally, the syntax would be like the following:
adb install MyFirstGame.apk adb uninstall com.chunyenliu.mygames
Copying files back and forth between the device and your computer works in a very similar way. Copying a file or folder from the device to your computer uses adb pull and you need to specify the source's full path name on the device. It's the same thing for copying a file or folder from your computer to the device with adb push; the destination's full path name on the device needs to be specified. If you also supply a new filename, it will be renamed. Assume you have a photo named myhouse.jpg. Here is how you do it.
adb pull /sdcard/DCIM/Camera/myhouse.jpg adb push myhouse.jpg /sdcard/DCIM/Camera/
Simulating System Activities
Many system activities can be simulated directly through adb shell commands. Often, it turns out very helpful in terms of automating the system testing process. For example, we can capture a screen shot and attach it with the bug report as follows.
adb shell screencap /sdcard/DCIM/Camera/mydevice.png
Furthermore, we can utilize the activity manager to instantiate activities to behave as if users are manually performing the tasks, such as making a phone call.
adb shell am start -a android.intent.action.CALL -d tel:+<phone_number>
Similarly, to simulate the delivery of a text message and key strokes, here are the ADB commands issued in a sequence on my Google Nexus 5. Key event 22 is Android's KeyEvent.KEYCODE_DPAD_RIGHT and 66 is KeyEvent.KEYCODE_ENTER. On my device, event 66 needs to be issued twice for two consecutive device screens.
adb shell am start -a android.intent.action.SENDTO -d sms:CC:+<phone_number> --es sms_body ":+<message_body>" --ez exit_on_sent true adb shell input keyevent 22 adb shell input keyevent 66 adb shell input keyevent 66
Android Debug Bridge (ADB) is something we definitely need to have working knowledge of when doing Android development. Occasionally, we have to reset the ADB server to make sure it is started in its initial state or for the devices to be re-connected. For advanced users, we can tap into the mobile device system shell directly and perform operations for various reasons; for example, reorganizing files or apps. I hope this tutorial helps you in some way.
- Android Developers at: http://www.android.com/developers/
- Official ADB: http://developer.android.com/tools/help/adb.html
- Androidlet at http://www.androidlet.com
About the Author
Chunyen Liu has been a software professional in Taiwan and the United States. He is a published author of 40+ articles and 100+ tiny apps, software patentee, technical reviewer, and programming contest winner by ACM/IBM/SUN. He holds advanced degrees in Computer Science, trained in 20+ graduate-level courses. On the non-technical side, he is a certified coach, certified umpire, rated player of USA Table Tennis, and categorized medalist at State Championships and US Open.