Basics of Android Debugging
- Prerequisites
- Using LogCat
- Seeing a Common Mistake
- Testing Views
- Tracing Code
An Android developer wears many hats: designer, code monkey, and, inevitably, troubleshooter. Bugs in code are inevitable—so it's important to know now to quickly and effectively track them down and fix them, whether you created the bugs initially or otherwise. In this tutorial, you'll learn the basics of Android debugging.
Prerequisites
You should have the Android SDK and tools installed, including Eclipse and the Android plug-in available for Eclipse. You should also be comfortable with the basics of Java, Android, and Eclipse development. That is, you should know how to import or create Android projects, understand Java syntax, and have a basic understanding of how Android applications are structured. For example, you shouldn't be confused by a phrase like "Look at the onCreate() method of your Activity class." We also expect that you have Android devices or configured AVD upon which to debug.
Getting Started
The best way to learn how to debug an Android application is to simply jump in and debug. To make sure we're all debugging the same application, download the open source sample application we've provided for this tutorial. You can either use Subversion to synchronize the repository or download the archive file that contains the source. Either way, import the project into Eclipse and be ready to debug this buggy application.
Using Perspectives in Eclipse
The DDMS Perspective, shown in Figure 1, is integrated into Eclipse through the Android Development Tools (ADT) plug-in. This perspective is incredibly handy, as it combines many debugging panes specially designed for Android debugging. When you're debugging, you will usually switch between the DDMS perspective and the Debug perspective. Here you'll find panes such as LogCat and other useful debugging utilities integrated into Eclipse. You can use the Debug perspective to set code breakpoints, inspect variables, and step through code. You can use the DDMS perspective to attach to specific processes on an emulator or device, inspect the heap, take screenshots, and explore files on the device or emulator, among other tasks. All of these tasks are invaluable for debugging.
Figure 1 DDMS in action