Android Projects

Android Development Books

Friday

How to debug android application

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.
3.) Android Debug Bridge (adb)
This provides a command-line debugging interface to a running Android phone or emulator.
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.
To open the Logcat window: go to
Window > Show view > Other, Android > Logcat.

You should be able to see Logcat at the bottom of your screen just like this:

As you can see above, Logcat has different levels of logging.
V stands for Verbose (lowest priority)
D stands for Debug
I stand for Info
W stands for Warning
E stands for Error
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:
package com.app.SampleProj; import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MyActivity extends Activity {
    private static final String TAG = "MyActivity";
        /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(TAG,"On Create");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    @Override
    protected void onDestroy() {
    // 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 to
Window->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.

 
If you want more information about this article click here.
Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 comments

Thanks for your comment

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

Related Posts Plugin for WordPress, Blogger...
© Google Android Lovers
Designed by BlogThietKe Cooperated with Duy Pham
Released under Creative Commons 3.0 CC BY-NC 3.0