{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
Debugging an Android Application
Summary
This article gives some quick pointers on how to start debugging an Android application, along with where to put the breakpoints and how to change the emulator sizes from within Eclipse.
 
Table of Contents

Starting Debugging

Figure 1. Several Android Applications open in the Package Explorer

Figure 2. Debug Button on the Eclipse Toolbar

Placing the breakpoints at the right points

Setting the breakpoint at the start of the ‘main screen’

Code Listing 1. MAIN/LAUNCHER screen from NotesList application

Code Listing 2. First line that gets executed in an Activity

Setting the breakpoint at the beginning

Code Listing 2. Finding the Provider from the Manifest

Code Listing 3. A content provider class

The Debug Perspective

Figure 3. The Debug Perspective for an Android Application

Choosing different emulators

Figure 4. Debug Dialog box for Android

 

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

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

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

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

Figure 4. Debug Dialog box for Android
Bookmark and Share This

More Articles With Similar Tags
icon-android-safari-browser-user-agent-string.jpg
This article talks about the basics of the browser (including the user agent string) used in the Android platform.
This article talks about various aspects of creating an SQLite database. It also uses Android SQLite shell as an example.
This article explains how to get pretty / nice output from the SQLite command line shell program.
icon-android-emulator-hvga-p-lunar-lander.jpg
See the images of various skins and screens of Android emulator with games (Lunar Lander and Snake) in them. Also shows how to change the skins of Android emulator while debugging your program.
icon-android-m5-main-screen.jpg
This is the cool new user interface of the Android OS/phone from version m5 (m5_rc14). Now this phone is beginning look like iPhone. I am beginning to believe that this will be a serious contender to any of the phones out there. Screenshots of various apps included.
About  Contact  Privacy Policy  Site Map