How to debug Android applications?

1. Debugging Android applications

1.1. Android debuggable attribute in the AndroidManifest.xml

If you develop Android applications you can debug them via Eclipse. To enable debugging an a real Android device you need to add the android:debuggable=”true” flag to the application segment in your AndroidManifest.xml file. The ADT tooling sets this flag automatically for debug builds based on the constant DEBUG of the BuildConfig class. This class is automatically updated by the ADT.

1.2. Turn debugging on at runtime in Eclipse

The JVM requires that you start a Java program in Debug mode to debug it. Android allows you to turn debugging for a running application on at runtime via the Debug button in the Device view of the DDMS perspective as depicted in the following screenshot.

how to debug Android application 1

After turning debugging on in this view, the next time a Breakpoint is encountered Eclipse allows you to debug the process.

2. Eclipse debugging

Android applications can be debugged similar to Java applications. See Eclipse debugging tutorial for details.

3. Exercise: Debugging

3.1. Create example project

Download and import Android project from www.vogella.com/download/androidtest/countries.zip into your workspace.

3.2. Debug the creation of thelist items

After launching the application you suppose to see a list of countries registered in the system. There is a bug in the implementation which causes the list to show empty strings.

  1. Launch the application normally.
  2. Realizing that their is a problem, you switch Debug mode on in the DDMS perspective.
  3. Put a breakpoint in MainActivity.onCreate(Bundle savedInstanceState) and re-launch the activity.
  4. Execute CountryUtils.getCountries() statement and check the value of “countries” variable. If you believe the value is incorrect, use “Step into” debug function to find the issue.
  5. Fix the issue.
  6. Launch the application and make sure the list of countries is shown correctly.

3.3. Debug list item click behavior

If you click an item in the list, application will show a toast message with the name of the country clicked. There is a bug in the applications which makes this function not working properly.

  1. Launch the application normally.
  2. (Try to) debug MainActivity.onListItemClick() method and fix the issue.
  3. Launch the application and make sure correct item is show in the toast.