Tuesday, January 29, 2013

How to debug Android application using eclipse


At the first time I tried Android programming, I don't know how to debug and you know, it's awful when my app get a bug. And then a long time I tried, I found it so I decide to write this topic (it's maybe my first note) to show you how to debug an android program :) . 
As we know, debugging is one of the most important skills in programming. Within debugging, we can see the value of a variable, a string, the running thread, and how the programing runs step by step.
There are two ways to debug: one is using logcat, another is breakpoints.

1. Using logcat: This is a nice and quick way to exchange information from the application on your device and the development computer.
First of all, we you import android.ulti.Log in your project (It’s not important because we can quick import by using Ctrl + Space). And then, you can call the static class Log from your project to log. The list below shows you some different levels of Logcat. When debugging we’ll just use Debug (D) to log the process. When logging some information, we’ll use Info (I),…
  • V — Verbose (lowest priority)
  • D — Debug
  • I — Info
  • W — Warning
  • E — Error
  • F — Fatal
  • S — Silent (highest priority, on which nothing is ever printed)
Now let get start…! Take a simple demo for android development: Hello world.
This is my code.

We use Log.i(DEBUG_TAG, "String value is: " + hello); to put our information into Logcat. After we run this program, we can see this line as the figure below:

If you don’t see this tab, you can go to Window > Show view > Logcat.
This helps you to find which line is a bug in your program. When your program get stuck, you can guess a put a Log.i(DEBUG_TAG, "This is mark");code into your project, and then you run program again a the see the Logcat display if a line Log.i(DEBUG_TAG, "This is mark"); you have put is in the Logcat list or not. If it is, the code you wrote from the beginning of  your project to this line is fine and you only check all codes after this line. If it is not, the bug is in some code before this line and you check this codes. Or you can use Logcat to see value of a variable in your project to make sure it is what you guess.

2. Using breakpoint: This is a powerful way that Eclipse support to you to debug your application.
To use it, you need some breakpoints in your project first. This means we can add a point to our code where we tell android to halt executing and tell us all he knows about the current state of the application. You can create a breakpoint by clicking to a bar in the left side of your codes. Here I have some:

And then, you click icon  . Then the eclipse will ask you if you want to open debug view. Now I will show you how to navigate through the application within 2 button and 3 arrows in the top.
  •  Resume : It means you don’t want to debug and you want your application keep running. You use it when you debug and know a bug is or it has no bug and you want to see the result.

  • Disconnect : It means you stop running your application anymore because you have found the bug is.
  • The first arrow means is step in (F5), this is good when you used a breakpoint on a function. With this arrow you will step into the function.
  • The second arrow means step over (F6), with this arrow you will go to the next line of code in your application.
  • The last  code is step return (F7), with this arrow you can return to previous states.


You can switch between debug view and java view by using the buttons on the right-top of your windows.

Ok, this was my little article about debugging. For experienced android programmers this is very easy, but for beginners this will save you a lot of work.