Friday, March 25, 2011

Debugging in Android using Eclipse


Android is a great platform which makes creating applications for mobile devices much easier than before. But in the progress of creating your applications you are bound to face bugs. For this purpose you need a good understanding of debugging your software. In this article I will explain how to debug your applications using the Eclipse IDE with the Android plugins.
We’ll use the Android Debug Bridge (ADB) which basically is a tool that runs on both your android platform (emulator or device) and your development computer. The ADB can be used via the command line interface, or just simply via Eclipse. We will be using Eclipse to debug since it’s much easier. If you are debugging on a device, you first have to enable USB debugging. Go to the settings of the machine > software > development > USB-debugging (or something like that, my phone is Dutch!)

First I will show you Logcat, this is a nice and quick way to exchange information from the application on your device and the development computer.
To use Logcat first import android.util.Log in your project. Now you can call the static class Log from your project to start logging. As you can see below, Logcat has different levels of logging. When debugging we’ll just use Debug (D) to log the progress. Of course when you want to log an actual error you will use Error (E).
  • V — Verbose (lowest priority)
  • D — Debug
  • I — Info
  • W — Warning
  • E — Error
  • F — Fatal
  • S — Silent (highest priority, on which nothing is ever printed)
So  let’s move on to debugging. Take a look at the example below, I created an Integer with a value and while the application is running I like to see it’s value in my log screen. I called the d method since I want to debug, if I wanted to show an error I would use e, easy right? As you can see it takes two arguments, the first is to identify your application, its good practice to save the identifier in a public value. The second argument takes the line that needs to be logged.
debugging-1
Now before you run this application, you have to open the Logcat window, if you don’t already have it on your screen go to Window > Show view > Other, Android > Logcat. You should be able to see Logcat at the bottom of your screen just like this:
debugging-2
Now you are ready to run your application!
debugging-3
Yay that works.
This logging is fun and can help you in many ways, but it isn’t perfect yet… Fortunately Android allows us to debug breakpoints in our application on runtime. 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.
To add such a point, double-click on the area before the line you want the application to halt. Like so:
debugging-4
Now instead of running the application, debug it (F11, or the little green bug). After a while when the application is installed on your device, it detects the breakpoint and asks you if you want to open the debug view. Most of the view is pretty straightforward, the most important things you need to know is how to navigate through the application. This is done via the 3 arrows in the top.
debugging-5
  • 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.
In my earlier example I added a breakpoint to my Integer, in order to see the value I first have to let that line be processed, do this by simply stepping over (F6) the line to the next. Now you can look in the box on the top-right and you will see mInt with it’s value.
debugging-6
The debug window is something you will use pretty often. To switch between your debug view and normal java view you can use the bottons on the right-top of your window.
debugging-7
Well, this was my little article about debugging. For experienced android programmers this is very easy, but for beginners this will save you allot of work.

No comments:

Post a Comment