Developers should understand how the platform works to write applications that deliver the best possible experience on the targeted platform. In this article, we will learn about how debugging works on the Android platform.
If you are an Android developer and use Android Studio, you already know that it comes loaded with debugging tools. The Android debugging environment is comprised of the following components.
Android Debug Bridge (ADB)
The adb is an application that allows you to communicate with a connected emulator or physical Android device. It is comprised of a client app that runs on the development machine, a server that runs on a background process on the development machine, and a daemon that runs on the emulator or device.
The adb resides in the <sdk>/platform-tools folder.
Dalvik Debug Monitor Server (DDMS)
The DDMS is the debugging tool that provides for the following:
- Port-forwarding services
- Screen capture on the device
- Thread and heap information
- Process and radio state information
- Incoming call and SMS spoofing
DDMS can be accessed from Android Studio by launching the Android Device Monitor and clicking the DDMS button.
Note that, if both an emulator and a physical device are connected to DDMS, DDMS defaults to the emulator.
DDMS supports the JDWP protocol that allows other application development platforms to support debugging Android applications.
How Android Debugging Works
Each Android application runs in its own process. DDMS connects to adb when it starts. A VM monitoring service acts as a bridge between adb and DDMS, and notifies DDMS when a VM on the device (each Android process has a VM) starts or terminates.
Once the VM is running, DDMS opens a connection to the VM’s debugger, through the abd daemon. DDMS then assigns a debugging port for each VM on the device. Once the debugger is connected to the port, all traffic is forwarded to the debugger from the associated VM.
DDMS also has another port (DDMS base port) that is a port forwarder, which accepts VM traffic from any debugging port and forwards it to the debugger on the base port.
DDMS for Developers
Developers need to access various aspects of information during debugging to understand issues with the applications. Here is how DDMS can be used for such cases. To start DDMS, go to Tools -> Android -> Android debug Monitor.
Figure 1: The Android debug Monitor
Process Heap Information
In DDMS, go to the Devices tab ->. Select the process and click “Update Heap” to enable heap information. If you want to invoke Garbage collection, click “Cause GC”.
Figure 2: Heap information
In DDMS, go to the Devices tab ->. Select the process. In the Allocation Tracker tab, click Start Tracking to begin allocation tracking. You can see the list of objects that have been allocation since tracking has started. In the list, you can select a specific row to see the method and line number of the code that allocated the object.
Figure 3: Allocated memory
Working with Files
To view files on the system, DDMS provides a File Explorer where one can “Pull File” or “Put File”.
Figure 4: The File Explorer
To view threads, in DDMS, go to the Devices tab ->. Select the process you want to examine, and click the “Update Thread” button to view process threads and its information.
Figure 5: Viewing the process threads and its information
To profile a method, in DDMS, go to the Devices tab ->. Select the process you want to profile and click the “Start Method profiling” button. You can click “Stop Method profiling” when you believe you have enough data.
In this article, we learned how Android debugging works and the features offered by the Dalvik Debug Monitor Server. I hope you have found this information useful.
About the Author
Vipul Patel is a technology geek based in Seattle. He can be reached at firstname.lastname@example.org. You can visit his LinkedIn profile at https://www.linkedin.com/pub/vipul-patel/6/675/508.