Thursday, October 6, 2011

Debug a remote Android process in Eclipse

Each component in the Android stack (Activity, Service, Receiver and Provider) is defined in the manifest file of your application. For example, an Activity is usually defined like this:

<activity android:name=".LocalActivity"
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />

The activity here will run in the main thread of the application process. If you put a breakpoint in your onCreate method, you’ll see this in the debugger:

Img. Eclipse onCreate Debug

A few things we should note here:
  • We see the debugger is connected to the DalvikVM via localhost:8627.
  • Our Activity is created in the main application thread (identified by <3> main).
If we go to the DDMS perspective, we see the following:

Img. DDMS perspective

As you can see, we see our emulator (identified by emulator-5554) hosting a number of processes including the process that is currently running our application (identified by our application package Here, you also see the debug port that allows us to attach a debugger (port 8627). The green bug next to our application process indicates that a debugger is attached.

From this screen, it is possible to start debugging other applications (represented by different processes), providing you have a source project in your workspace. In the example here, I have an android project that hosts the application identified by the com.ecs.latify package, so I can select com.ecs.latify, and hit the green bug to start debugging the selected process. As soon as you do this, 2 things will happen:

A green bug will be put alongside the process.

Img. DDMS perspective

Your Eclipse debug perspective will now have a second Android application ready to be debugged.

Img. debug second app

We’ll now add a button to our application that launches a second activity, but this time, we’ll configure the activity to run in a different process using the android:process attribute in the manifest.

<activity android:name=".RemoteActivity"

We’ll put a breakpoint in the onCreate() method of the RemoteActivity and launch the application in debugmode. You’ll notice that the debugger will not be suspended when the second activity is shown. The reason being that the second activity runs in a totaly different process. If we switch back to the DDMS perspective, we’ll see ourRemoteActivityProcess. Notice how it runs as a seperate process, using a seperate port (8631).

Img. DDMS perspective remote process

In order to hook up a debugger, one would expect that selected the process, and clicking on the green bug would be sufficient (like we did in the previous section). Unfortunately, due to reasons unclear to me, Eclipse shows the following dialog 


The only way to debug this process is by creating a remote java application in eclipse, pointing to the port (8631) as defined the DDMS process overview.


Only then will we able to suspend the debugger on the breakpoint that was put on the activity configured to run in a remote process.


Surce from: Doityourself

No comments:

Post a Comment