Debugging An Android App

Before starting that how to debug an android application, it is necessary to know that what is “Debugging”?
“Debugging is a methodical process of finding and reducing the number of bugs and defects in a computer program”
Advantage of Debugging:

When you start to make any application in android or in any programming language you will face some bugs, for solving them you need a good understanding of debugging your application.

There are following ways to debug an application in android:1.) Logcat
Android provides a general-purpose logging package that you can take advantage of to log informational or error messages from your running application. Perhaps of more importance, Android uses this facility extensively to tell you what is going on as it starts up, initiates your application, and tries to run it

2.) Eclipse Debugger
Eclipse provides a source-level debugger that the Android SDK connects with the running Dalvik bytecode, so you have all the debug capability you’d normally expect from a Java program running under Eclipse.

4.) DDMS
Android also provides a special window-oriented debugging environment custom tailored for Android and the Dalvik VM. You can find more about DDMS in our next article “What is DDMS and how it works”
5.) Traceview
An Android-specific utility that tracks all the method calls your application executed and the time spent in each method.

In this article, we will discuss the topic that how to use Logcat and Eclipse Debugger.

1.) Logcat:

First we will understand Logcat; this is most easy way to get error messages from your running application.

How to use??
To use Logcat you have to import android.util.Log in your project.
Then you can call the static class Log.
Here is a sample code snippet:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

packagecom.app.SampleProj;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

publicclassMyActivityextendsActivity{

privatestaticfinalStringTAG="MyActivity";

/** Called when the activity is first created. */

@Override

publicvoidonCreate(Bundle savedInstanceState){

Log.i(TAG,"On Create");

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

@Override

protectedvoidonDestroy(){

// TODO Auto-generated method stub

super.onDestroy();

Log.i(TAG,"On Destroy");

}

}

Here is the Logcat:

This is How Logcat Works…!!

You can also filter the Log for making it more clean:

Click in the “+” sign of your Logcat window at the right side of screen, and add new Filter with tag name:

You will see a new Tab which will contain only specific Tags…

This is all about the Logcat, now we come to our next topic Eclipse Debugger.

2.) Eclipse Debugger:

Eclipse provides a built-in “Debugger”, in which you can set “breakpoints” where the program will pause, and allow you to step through the program line by line and inspect the values of variables, amongst other features.

To open the Debugger: go toWindow->Open Perspective->-Debug

You will get a debug Perspective like this:

Now you need to set breakpoint to use debugger. You can set breakpoint by placing your cursor on the marker bar (along the left edge of the editor area) on the line with the suspected code. Double-click to set the breakpoint or right click and click Toggle Breakpoint:

Now debug the application by right click on the selected project and click on the debug as ->android application
Or
By click on the bug button in the tool bar:

The following figure shows the execution break at the first line in the onCreate() method of the activity. You can Step Into (), Step Over (), Step Return (), etc.

Step Into () or F5 key: The next expression on the currently-selected line to be executed is invoked, and execution suspends at the next executable line in the method that is invoked.

Step Over () or F6 key: The currently-selected line is executed and suspends on the next executable line.

Step Return () orF7 key: Execution resumes until the next return statement in the current method is executed, and execution suspends on the next executable line.

Next Reading

This is a sample application, having the features as the notepad like cut, copy, paste and save. You can also edit the Note by opening it again. Underlying Algorithm: Basic description of algorithm in step by step form: 1.) Create a Project MyNoteEditer. 2.) Create a class NotesDbAdapter.java, in this class we will use SQLiteDatabase […]

Previous Reading

This is a sample activity which shows How to use Android’s Text to Speech capabilities. Text to Speech was introduced in Android 1.6, so when you create your new Android project make sure your minimum required SDK is set to Android 1.6 (or API level 4). Last topic published on this forum was How […]