Post Top Ad

Post Top Ad

Saturday 11 February 2017

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.
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.

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");
    }
}
 

No comments:

Post a Comment