Thursday, October 24, 2013

Debugging Android applications

Debugging Android applications
This tutorial describes 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 yourAndroidManifest.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 DDMSperspective as depicted in the following screenshot.
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. Access Android source code

Ensure that you can access the source code of you Android system in your Eclipse IDE.

3.2. Create example project

Create an Android application with the top level package calledcom.vogella.android.debugging.listview.
Implement the following adapter for your ListView.
package com.vogella.android.debugging.listview;

import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.os.Debug;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MyArrayAdapter extends ArrayAdapter<String> {

private List<String> values;
private Context context;

public MyArrayAdapter(Context context, List<String> values) {
super(context, android.R.layout.simple_list_item_1);
this.context = context;
this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Ensure sorted values
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setText(values.get(position));
return view;
}
}
Implement a ListView in the activity which shows 1000 random generated strings.
package com.vogella.android.debugging.listview;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;

public class MainActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<String> list = createValues();
MyArrayAdapter adapter = new MyArrayAdapter(this, list);
setListAdapter(adapter);
}

private static List<String> createValues() {
SecureRandom random = new SecureRandom();
List<String> list = new ArrayList<String>();
for (int i = 0; i < 1000; i++) {
String string = new BigInteger(130, random).toString(32);
list.add(string);
}
return list;
}

}

3.3. Debug your application

Run your application. The list is not displayed. Debug the super() method call in your list adapter to find the reason.

No comments:

Post a Comment