In this increasingly fragmented and complex world of Android application development, thoroughly testing your Android app is becoming a real challenge. One of the biggest headaches is how to test your Android app in all the conceivable real-world scenarios. To make matters worse, most Android app development scenarios are mobile-specific, and will be unfamiliar to developers who have previously dealt only with desktop and Web environments. Will your Android app freeze when a text message or phone call comes through? How will it perform under fluctuating network speeds, or when the signal drops?
You may have an Android device with your targeted operating system on hand, but you'll still struggle to recreate every possible situation, which is why the emulator has become the Android developer's best friend. But even Android emulators have their limitations, and the testing phase is crucial to the success of your Android app.
The good news for Android developers is that once you have the Android SDK and ADT plugin installed, you'll have access to one of the most powerful debugging tools out there: the Dalvik Debug Monitor Server (DDMS.) The DDMS is an invaluable tool for checking the memory usage of your Android app, drilling down into errors, simulating a range of real-world conditions, and much more. The DDMS can analyze an Android project running in the emulator, or it can be connected to a real device (if both are connected, the DDMS will default to the emulator.)
Firing Up the DDMS
To access the DDMS, open the Window menu and select Open Perspective. Select Other, which will present you with a list of additional options. Select DDMS.
This will bring up the DDMS perspective with the default views (note that you can always access additional views by opening the Window menu and selecting Show View). In this article, I'll take you through these different DDMS views and explain why they're so important for Android development.
Android DDMS View # 1: LogCat
This is where Android collects and displays a stream of system messages and diagnostic data. When you run your app, the Log class automatically prints messages to LogCat alongside other output data, such as stack traces.
It's possible to filter these log messages, which is useful if you're after a specific item. Select the green Plus sign to bring up the LogCat Message Filter Settings dialog, and enter your search parameters.
Android DDMS View #2: Devices
The Devices view lists the devices and emulators that are online, alongside the processes that are running in each instance. This view is useful for getting an overview of your different projects.
The Devices tab also provides easy access to some common debugging operations. Select the item you wish to explore in the Devices view; this will open a menu where you can access the following shortcuts:
- Update heap: Enable heap information for a process, and spare yourself having to manually handle memory deallocation.
- Dump HPROF file: Generate a heap dump.
- Cause GC: Invoke garbage collection.
- Update threads: Enable information about running threads in the Threads view (more about this later.)
- Start method profiling: Track a method's metrics, e.g. the time it takes to execute a method.
- Stop process
- Screen capture: Capture what is currently displayed on your handset/emulator.
- Reset adb: This can be useful for solving adb-related errors.
Android DDMS View #3: Allocation Tracker
Android's Dalvik runtime is garbage collected, but memory management should still be on your list of priorities. Neglecting memory management leads to hanging objects and eventually to memory leaks. The Allocation Tracker provides an insight into allocations that are occurring in real time. To start tracking the memory allocation of objects, you:
- select the process you wish to track in the Devices tab.
- open the Allocation Tracker.
- select Start Tracking to begin the tracking process.
When you are ready to analyze the collected data, click Get Allocations to bring up a list of all the objects that have been allocated since you enabled tracking.