Starting Debugging
As you can see from the figure below, several Android applications are in this Package Explorer of Eclipse. These applications might have been created from scratch or created around the source of existing applications (samples, etc.).
Figure 1. Several Android Applications open in the Package Explorer

You can debug any of these applications in one of several ways:
- Right-Click on the Project and choose Debug As – Android Application
- Click on the bug button on the toolbar and choose the appropriate application
- Choose Run-Debug from the menu
- Right-Click on the Project and choose Debug As – Open Debug Dialog and debug the app from the dialog box
Probably the easiest is using the bug button on the toolbar (once you have debugged that application at least once)
Figure 2. Debug Button on the Eclipse Toolbar

Any of these actions will start the application in the Android Emulator. But it will not let you break into the code yet, as no breakpoints have been set.
Placing the breakpoints at the right points
Setting the breakpoint at the start of the ‘main screen’
In this scenario, I would like to place a breakpoint just when the main screen for the application is about to get executed. You can find out the main Activity of the Android application by looking in the AndroidManifest.xml.
For example, for the NotesList application, you can see that the Activity named NotesList is the MAIN / LAUNCHER screen (from the Code Listing below)
Code Listing 1. MAIN/LAUNCHER screen from NotesList application
<activity android:name="NotesList" android:label="@string/title_notes_list">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
…
</activity>
Now find the NotesList class, which extends the Activity class (or one of its derived classes like ListActivity) from the Android SDK. This class will override the onCreate method from the parent Activity class. This is the first method that gets called. So, you want to put a breakpoint in this method. As shown in the Code Listing below, the very first line would be super.onCreate(icicle).
Code Listing 2. First line that gets executed in an Activity
public class NotesList extends ListActivity {
…
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setDefaultKeyMode(SHORTCUT_DEFAULT_KEYS);
…
}
}
Setting the breakpoint at the beginning
However, the main screen is not the place where Android application actually begins. If you look at the AndroidManifest.xml, you might find a provider behind the application. In the listing below, you will see that the NotesList application has “NotePadProvider” specified as the provider.
Code Listing 2. Finding the Provider from the Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.notepad">
<application android:icon="@drawable/app_notes"
android:label="@string/app_name">
<provider android:name="NotePadProvider"
android:authorities="com.google.provider.NotePad" />
As you can see, this content provider class has a bunch of private variables, a static class, and a static block. You can place a breakpoint in an appropriate place here or in other global code to break before getting to the main screen of the application.
Code Listing 3. A content provider class
public class NotePadProvider extends ContentProvider {
private static final String TAG = "NotePadProvider";
private static final String DATABASE_NAME = "note_pad.db";
private static final int DATABASE_VERSION = 2;
…
private static class DatabaseHelper extends SQLiteOpenHelper {
…
static {
URL_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
URL_MATCHER.addURI("com.google.provider.NotePad", "notes", NOTES);
…
}
}
The Debug Perspective
When you are in the debug mode, Eclipse displays the Debug Perspective. If not, you can display this perspective by using the menu Window – Open Perspective – Debug. This is a collection of several debugging related views, including Breakpoints view, source editor for stepping through the code, threads and call stack, etc.
The figure below shows the execution break at the first line in the onCreate() method of the NotesList activity. From here you can Step Into, Step Over, Resume, etc.
Figure 3. The Debug Perspective for an Android Application

Choosing different emulators
And finally, for testing/debugging the Android application in emulators of different sizes, use the Debug Dialog box. In the Target tab, four different emulators are available – HVGA-L, HVGA-P, QVGA-L, QVGA-P. You can also use –wipe-data command line option to make a fresh start on the emulator. However, this might take a bit of time to get to the application launch.
Figure 4. Debug Dialog box for Android
