Installing Android sources into Eclipse for debugging
A couple of weeks ago I installed Android sources into my Eclipse environment. My primary motivation was to get JavaDoc comments in code assist, but those sources were also picked up by the debugger.
The problem with mismatched Android version sources
It’s pretty common when developing for Android to build the application with one version of Android SDK and debug with another (typically earlier) version. My environment is no exception: I build with Android 2.2 SDK, and debug on a Motorola Milestone that runs 2.1update1 (which will never be updated to 2.2, but that’s another story). This means that the sources installed for JavaDoc and code assistance are, strictly speaking, not the right ones for debugging, as you can see in the screenshot below:
The debugger says the application is stopped inside performLaunchActivity, but the source window shows startActivityNow. Let’s fix that.
Getting the right sources (again)
My development phone is a Motorola Milestone running 2.1update1, so I needed to find the right (or close enough) version of the base Android framework in the repository here:
I found what looked like 2.1update1 for my Milestone at the bottom of that page; it was a branch called “eclair-sholes-release2” (“sholes” being the codename for Motorola Droid / Milestone architecture, and assuming that “eclair-sholes-release” was the first 2.1 release).
In addition to the list of branches, the page also has a list of tags, which is another place to look for a particular version of Android.
Clicking on “eclair-sholes-release2” took me to this page:
which doesn’t have a download link, so I replaced “a=shortlong” with “a=snapshot” in the URL:
Source code from major release-specific branches can be downloaded like this:
You can get other versions by changing the branch name (froyo) in the link above (to donut, gingerbread, etc.)
Extracting the sources
This is how I extracted the sources on my computer, nothing special:
Pointing the debugger at the sources
The debugger has its own setting for source code lookup, which is separate from the “
source attachment” to
android.jar used before. To set this, switch to the debug perspective, then right-click on your running application, and select “Edit Source Lookup”, then proceed as shown below.
The left screenshot below also shows a deliberate error I made in one of my layouts, by having a ScrollView with more than one child, thus making my application crash.
The right screenshot shows how to select the right folder where Android 2.1u1 sources were extracted.
This completes the setup.
The right source code version shown after a crash
This is what I get now instead of the misaligned source code shown near the top of this page. Looks like a pretty significant improvement to me: