Starting a Debug Session for Android with ADT


I want to build an application targeting the Android platform. I'm a little rusty with Java but I really like developing with Eclipse (I've been using it for some other stuff like PHP development). The ADT plugin got me started with the sample applications pretty quickly but now that it's time to deviate and build something of my own I have to set a nice low goal that I can knock out with a high chance of success. Then I can iterate and go a little deeper on the next pass. To start with I think I'll play with drawing primitive graphics. There's an API example called DrawPoints with some code that just spouts out random points on the screen. I took a fair chunk of that and stuffed it in to the "Hello, Android" application I built earlier.

All in all the activity's pretty straightforward and Eclipse makes it even easier. I'd post the code for drawing but it's almost identical to DrawPoints at this stage. The next step is to get some new code in there. I decided to go for my old standby, the Hilbert curve. I modified the code to suit the drawing environment but didn't get results right away. I learned a few things about debugging with the Android emulator and ADT in the process.

Starting the debugger

To start debugging use the Debug As... command. This is the same thing you'd do with a typical Java application but with Android it also has to start up the emulator if it's not running and it has to us ADB to connect the debugger to the emulator. The first time you run or debug a given application in Eclipse you'll want to create a Run Configuration for it. The same Run Configurations are used for the Run and Debug commands so if you created one using "Run As..." then that'll also work with "Debug As...". If you haven't got one then right click your project and go down to the "Debug As" submenu and pick "Open Debug Dialog..." When the Debug Dialog shows up you can click Android Application on the left and then click the "New Launch Configuration" button at the top. This creates a new Run Configuration (or Debug Configuration or Launch Configuration depending on what reference material you're reading). This is pretty much the same as the Google directions for starting a new application.

Here are the step-by-step screenshots (as always, click for bigger images).
Right-click...
[G2:5326]
New Configuration...
[G2:5340]
Enter a name (same as the project for me)...
[G2:5330]

After that you can just debug from where you are. Next time you want to start up debugging with the same configuration you can find it under the little bug drop-down button.
[G2:5338]

Setting a breakpoint

Right-click in the left-hand margin of your source code on the line you want to stop at. This gives an option to set or clear a breakpoint. When code execution gets to the breakpoint, the debugger will stop and you can look at variable values or step through the code to watch what happens. Some lines of code might not have the effect you expect immediately, in graphics there are sometimes many buffers that an image passes through before it is visible on the screen.

Making a change, restarting

Some changes will be pushed to the emulator immediately. You can edit the code and save it, Eclipse will recompile and ADT will try to update the target. If it can't it will alert you (by default) with a message box. To ensure your code runs as it appears in the editor, hit the stop button then start a debug session again. ADT will connect to the emulator and send the new apk file (your compiled code and resources).

That's all I've got for today but I want to elaborate on some of the other debugging and logging functions I've learned about soon.

3.81818
Your rating: None Average: 3.8 (11 votes)