Android Notes: Debugging

By now you’ve probably written a bit of code, and no doubt it’s not working quite right. It’s time to embrace the fantastic world of debugging. Debugging is a bit of a chore, and it can be made even more traumatic by having to use a bad debugging environment.

Fortunately the Android/Eclipse development environment looks like it has been used by real Android developers. It’s so easy to use. Just set breakpoints inside Eclipse and press the ‘Debug’ button instead of ‘Run’. The emulator will pop up and Eclipse will turn into its debugging view. This is why everyone recommends Eclipse and the ADT, it “just works” (I also get the feeling everyone in Google uses a Mac too, it seems to “just work” even better on a Mac)

What if you own a device and want to debug on that? Easy. On your phone go into Settings -> Applications -> Development and turn on ‘USB Debugging’. Now edit the manifest.xml file and add the following to the ” tag

android:debuggable="true"

for some reason, toggling the ‘Debug’ option in the GUI version of the manifest doesn’t do anything. If debugging isn’t working, check the XML file’s source is correct.

There’s more though. Sometimes you don’t want a full debugger, you just need the code to print out some debugging messages. This is handled through the ‘Log’ object like so:

1
2
3
4
5
import android.util.Log;
 
...
 
Log.d("Sensors", "Sensors x=" + x);

To see this debug logging, either connect the debugger and it appears in a small window, or use the ‘adb‘ console application:

macbook:~ james$ adb devices
List of devices attached
HT91RKV01693	device

macbook:~ james$ adb logcat
D/Sensors (22600): Sensors x=0.16344418
D/Sensors (22600): Sensors x=0.16344418

It’s probably best not to put logging inside tight loops, there’s a lot of debugging output in the phone, and excessive logging will hurt performance, it’ll definitely trigger the garbage collector.

The Android Debug Bridge is documented on its relevant page.

As far as debugging and running code is concerned, there is no difference between using a real device and the emulator. The debugging commands are identical, and there is no speed difference between starting a debug session on a phone or through the emulator.

About James

I'm just a person who likes writing software in their spare time. I'm not an "indie games developer" and am not trying to escape my day job and live in the happy world of games dev. I'm more like one of those people that used to write games in their spare time in the 80s. My stuff would be PD or Shareware if this were the 80s or 90s. It's good to comment on the posts in here :)