Last week, Personal Capital released its Android app to the Google Play store. You can read more about it here. Developing quality apps that perform well requires more than just writing code. The Android SDK comes with a number of excellent tools for debugging and testing apps. While simple to use, they can expose some of the harder-to-find bugs and reveal opportunities for improving your code. Here are some highlights of the things we get lots of mileage from:
While not specifically an Android tool, Eclipse is a valuable one nonetheless. Android apps can be run with the standard Java debugger just like any Java project, and Eclipse’s debugger is no slouch with conditional breakpoints, exception breakpoints, drop to frame, and other neat features. Most of the SDK tools integrate into Eclipse as well, so all of your tools are in one place.
Logs are especially useful when trying to reproduce a bug, both to give you a starting point for your investigation and to confirm that you’ve accurately reproduced an issue. Even when you’re not looking for anything specific there, watching the device log can be surprisingly instructive.
Reading the Logcat can be done with the Android Device Monitor tool in the Android SDK. You can start it from a command line by running
<android_sdk_path>/tools/monitor. Alternatively, you can open the Eclipse perspective called DDMS to access the same features.
Select any connected device or running emulator from the Devices tab, then switch over to the Logcat tab to see the logs. There will probably be lots of messages flying by, so put part of your application’s package name in the filter bar to increase your signal-to-noise ratio. You can also filter out messages below a certain priority level by selecting from the dropdown menu. The save button in the upper right of the pane will export selected lines to a file.
Suppose you get a crash while running your app and you aren’t connected to a computer, or you don’t have Device Monitor running. Is there any way to retrieve those logs? Indeed there is: in your
<android_sdk_path>/platform-tools/ directory is a tool called ADB. Connect your device and run the following command to dump all the log messages to a file:
$ adb logcat -d > log.txt
Of course those log messages won’t last forever, so grab them before they get pushed out of the log buffer.
In case your device doesn’t have screenshot capabilities, or to spare yourself having to transfer screenshots form your device to your computer, Android Device Monitor has a screen capture utility built in.
In Device Monitor (or DDMS in Eclipse), select a device or emulator from the Devices tab and click the icon near the top. A window opens up showing a preview of the device screen, which you can save as a PNG. The preview does not update when you interact with the device; you have to click the Refresh button to reload the preview.
Speaking of UI, occasionally you get some layout params wrong and a View (or, you know, half the View tree) mysteriously vanishes into nothingness. The last time this happened to me, it was in a part of the view hierarchy I certainly didn’t expect, and I never would have found it without this tool.
Hierarchy Viewer is included in Device Monitor, or you can open it as a perspective in Eclipse. In the Windows tab, select the window corresponding to the application you are debugging (the active window will be bolded). Once the View tree loads, clicking an individual View will show you a preview of its appearance on screen. Where that View is positioned on screen can be seen in the Layout View tab, and you can inspect various attributes in the View Properties tab.
If a View that should be on screen is not visible and you do find it in the View tree, check the layout and measurement properties and see if its width or height is inadvertently being set to zero. This can happen for a number of reasons, some potentially obscure, like using the wrong anchor in a
RelativeLayout or not setting the
TraceView is a profiling tool to help you determine areas of code that are performance bottlenecks. It can also alert you to operations on the UI thread that should be moved elsewhere. Due to technical limitations and to the latency it adds, method tracing is not something you would leave running for the duration of testing. Instead, you should start tracing just before performing some action of interest, and then stop tracing just after.
The easiest way to use TraceView is through Android Device Monitor (or DDMS in Eclipse). While your app is running, select the process in the Devices tab. Click the icon at the top of the pane to start tracing, interact with the app for a bit, and click the icon again to stop tracing. TraceView will open the trace data for you to inspect.
The top pane will show a timeline of thread activity, while the bottom shows profile information for all method calls that took place. In the timeline, you can zoom in and out with your mouse scroll wheel, or click and drag a horizontal region to zoom in. Pay particular attention to the Main (or UI) thread; any unusually long gaps in the timeline could indicate something making your app unresponsive.
The bottom pane shows all the method calls and reports data regarding execution time (real time or percent of total), both exclusive and inclusive. Exclusive time is only the time the method was executing instructions; inclusive time also counts calls to other methods. You can drill down through method calls and sort them by any column. Sorting by exclusive real time, for instance, could help find particular methods that are taking the most time to process.
Sometimes the simplest thing—screenshots, exported logs, etc.—vastly improves our ability to turn around a bug posted to our issue tracker. Other times, you need comprehensive measuring tools to pinpoint a critical flaw. Whatever platform you are building for, tools like those packaged in the Android SDK are invaluable. Even for non-developers, these tools can be leveraged to help understand the behavior of the app and provide better feedback for developers.
If you are already using these tools in your development process, great! Consider introducing all of your engineers to them as well. Empower others to explore, get their hands dirty, and perhaps engage in the feedback part of your development loop; they’ll probably appreciate the knowledge you’ve shared. And don’t count out your client base either—believe it or not, even some of our end users send us logcat output on occasion.