2012年10月18日木曜日

Debug a remote Android process in Eclipse

Most people that are developing Android applications using Eclipse will probably use theEclipse ADT plugin. The plugin offers a lot of functionality, including

  • Debugging features, via it's integration with the Dalvik Debug Monitor Server (DDMS)
  • Emulator control from the Eclipse workspace
  • XML Validation for Android specific files (strings, views,  menus , ….)
  • Project Wizards, to facilitate the creation and packaging of your Android app

In this short article, we'll be discussing the debugging of remote android processes (the processes defined in your manifest using the android:remote attribute).

Android Eclipse ADT Plugin


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 :

1 <activity android:name=".LocalActivity"
2           android:label="@string/app_name">
3     <intent-filter>
4         <action android:name="android.intent.action.MAIN" />
5         <category android:name="android.intent.category.LAUNCHER" />
6     </intent-filter>
7 </activity>

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 :

Eclipse onCreate Debug

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 :

DDMS perspective

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 com.ecs.android.sample.remoteprocess). 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.

DDMS perspective

DDMS perspective

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

debug second app

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.

1 <activity android:name=".RemoteActivity"
2           android:label="@string/app_name"
3           android:process=":RemoteActivityProcess">
4 </activity>

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 our RemoteActivityProcess. Notice how it runs as a seperate process, using a seperate port (8631).

DDMS perspective remote process

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.

0 件のコメント:

コメントを投稿