Debugging android with emacs

Thu Nov 6, 2008

When I last did serious java development, the eclipse ide hadn’t come on the scene. VisualAge was so buggy it crashed all the time, so I gave up on java ide’s altogether and got emacs with etags to the point that it didn’t matter. Coming from a C/C++ background, I was used to running gdb in a shell in emacs, so that is what I did with jdb at that time.

Things are different now. Eclipse seems like it might be a good ide, but I’ve never tried it. I like emacs, because I’m quicker in emacs since I know the key combinations, plus having written up lots of lisp over the years to make life easier. Note, there’s something called jde out there for emacs, which makes it into a full fledged ide – but there’s a lot of install work to get it working. I’m not about to bother with that, because I just don’t have the time.

Here’s the purpose of this article – how do you debug android applications using emacs? I did it like this on my macbook:

  1. install the android sdk ($SDK)

  2. put $SDK/tools on your $PATH

  3. Create a new android app in $WORKING like this: activitycreator –out projname com.yourdomain.appname.YourActivity

  4. Build the app with ant (you already have ant installed, right?): ant

  5. Run the android emulator: emulator &

  6. Run the debugging service: ddms &

  7. Install your app in the emulator: ant install

  8. In your .emacs file, make sure you have gud-jdb-sourcepath and gud-jdb-classpath set to your working directory src and bin/classes folders. This could be a pain if you have to change working directories a lot – but I don’t, and I’m just getting started with java development after being out of it for a long time, so if there is a better way, please let me know what it is.

    (custom-set-variables ‘(gud-jdb-use-classpath t) ‘(gud-jdb-classpath “$WORKING/src:$WORKING/bin/classes”) ‘(gud-jdb-sourcepath “$WORKING/src”) ‘(gud-pdb-command-name “~/bin/”))

  9. Now you can launch your app in the emulator. If you look at the ddms window, you’ll see a line for the process with the column on the right being the port number for the debugger to connect. In this picture it is 8607.

ddms window

  1. In emacs, C-x jdb RET and you will see: jdb. Fill in the rest of the line like this, note, no space between sourcepath and the path: jdb -sourcepath$WORKING/src -attach localhost:8607
  2. Set a breakpoint in the source file with C-x SPACE and you’ll hit it when you come around.

Note, you’ll have to fill in the $WORKING yourself, and if you don’t know jdb, I suggest you read the fine manual.

Have fun debugging.