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.
Android DDMS View #4: Emulator Control
Your app may run perfectly in your IDE, but it isn’t destined for a life on the desktop. The Emulator Control view is a powerful tool for testing your app under real-world conditions.
The different options available as part of the Emulator Control tab are:
- Telephony Status: Test your app under different network conditions, including network data I/O speed and latency.
- Telephony Actions: Simulate incoming calls and SMS messages.
- Location Controls: Does your app have location-specific functionality? This option can be used to simulate changes of location, either manually or by using the GPX (GPS eXchange file) or KML (Keyhole Markup Language Files) options.
Android DDMS View #5: File Explorer
The File Explorer allows you to view files, and to copy them between the emulator and the device, in a similar way to the abd
Android DDMS View #6: Heap View
Are you forever running out of memory? Then it’s time to find out exactly what objects are being created. The heap view can be used to analyze memory usage and to manually invoke garbage collection. To invoke garbage collection and update the heap with the latest memory analysis, you:
- select the process you wish to analyze from the Devices tab.
- select Update Heap to start gathering heap information.
- select the Heap tab. Click Cause GC to invoke garbage collection.
Once the operation has been completed, you will see a group of object types, alongside the memory that has been allocated for each object.
Android DDMS View #7: Threads
The thread view allows you to access the thread information for a selected process. To check thread information, open the Devices tab and select the process you wish to view thread information for. Then, it’s simply a matter of clicking Updates Thread and opening the Threads tab.
At this point you may receive a “Thread updates not enabled for selected client (use toolbar button to enable)” message. You can easily rectify this by opening the Windows menu, selecting Preferences and Android, followed by DDMS. Check the Thread Updates Enabled by Default box. Your thread information should now display correctly.
Android DDMS View #8: Network Statistics
The Network Statistics tab is useful for tracking when your app is making network requests. Monitoring data transfers (in particular how and when they occur) allows you to modify your app to optimize network usage and battery efficiency.
This article has covered the core functionality DDMS offers Android developers. When testing your app, bear in mind that although selecting the DDMS perspective will bring up the default views, you can easily add additional views by opening the Window menu and then Show View, as needed. Or, if you just want to reorganize the DDMS layout, this is simply a matter of dragging and dropping the different panes around your dashboard.