debugging android in idea

Fri, May 1, 2009

You’ve downloaded the Android SDK and want to get started but you also want to make sure you can debug in your favourite IDE, IDEA. You’ve installed the SDK into a directory we’ll call ANDROID_HOME and added ANDROID_HOME/tools to your PATH. So let’s debug.

First, create a hello world application:

cd ~/dev/android

android create project
–target 3
–path ./HelloWorld
–activity HelloWorldActivity
–package com.codebrane

This creates a new directory, HelloWorld in the current directory. To build is simple:
cd HelloWorld
ant debug
Next we need an AVD (Android Virtual Device) on which to install the application. The following command will create an AVD, ~/.android/avd/HelloWorld.avd:
android create avd –name HelloWorldAvd –target 3
To start the emulator we need to be in the tools directory:
cd ANDROID_HOME/tools
emulator -avd HelloWorldAvd
Once the emulator is up and running, install the application:
cd ANDROID_HOME/tools
adb install ~/dev/android/HelloWorld/bin/HelloWorldActivity-debug.apk

191 KB/s (4391 bytes in 0.022s) pkg: /data/local/tmp/HelloWorldActivity-debug.apk Success

Now we’re ready to debug, so start DDMS:
cd ANDROID_HOME/tools
ddms
Back in the emulator, to get to the app, click on the arrow above the MENU button and you’ll see it in the list of installed apps. Click on it and you’ll see the text displayed:

Hello World running in the Android emulator

but what happened to the debugging? This is the tricky bit.

First you’ll need to open the HelloWorld project in IDEA. Just create a new project from existing source and point it at ~/dev/android/HelloWorld. It’ll create two modules, Hello World and Tests. Add ANDROID_HOME/platforms/android-1.5/android.jar as a dependency in the HelloWorld module and add the HelloWorld module as a dependency in the Tests module. Compile in IDEA to sync it with the Ant compiled source.

Next, add android.os.Debug.waitForDebugger() to the code in HelloWorldActivity.java:

public class HelloWorldActivity extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    android.os.Debug.waitForDebugger();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }
}
android.os.Debug.waitForDebugger() tells DDMS to halt the application until a debugger attaches to the port DDMS is listening on. The default is 8700. So in IDEA setup a new Remote Run/Debug Configuration, set it to localhost on 8700:

IDEA debug settings for Android

what will happen is when you open the application in the emulator, DDMS will stop it loading until IDEA connects on 8700. Before we can do that we need to rebuild the application using “ant debug” and compile in IDEA too to keep them in sync. Then we need to replace the non halting version on the emulator with the new one with that line added. So with the emulator running, remove the current version by shelling directly into the device:

cd ANDROID_HOME/tools
adb shell
…

cd data

cd app

ls

com.codebrane.apk

rm com.codebrane.apk

ls

exit

and install the new one:

adb install ~/dev/android/HelloWorld/bin/HelloWorldActivity-debug.apk

191 KB/s (4391 bytes in 0.022s) pkg: /data/local/tmp/HelloWorldActivity-debug.apk Success

Start the debugger:
cd ANDROID_HOME/tools
ddms
open the application in the emulator and nothing will happen. So have a look at the debugger and you’ll see it’s stopped the app from loading:

The Android Debugger

notice how the application has a red bug next to it. Cute or what! DDMS is now waiting for us to connect to it from IDEA. There are two ports available, 8608 (the actual number depends on how many apps are installed on the emulator) and 8700 (the default which always gets us to the selected app in the debugger) and we’ve chosen to use 8700 in IDEA. So hit the Debug button in IDEA and off we go:

Debugging Android in IDEA

I’m off to port i-aws to Android now!

comments powered by Disqus