The Eclipse support for Android development is pretty good, but one slight annoyance is that the source for the Android SDK classes isn’t available by default.
Fortunately, there’s a fairly straightforward process to fix this.
First, get hold of the source JAR for the SDK. What’s that? We don’t appear to make a source JAR available? That’s annoying.
Well, hopefully that will be fixed at some point. In the meantime, you can either download one that someone’s prepared (that’s apparently for something approximating 1.5r2 — but note that I haven’t checked how correct it is!), or you can produce your own from the git repository, from a tag of your choice.
Once you have a source JAR, you’d normally expect to just attach it directly to the library in Eclipse, but that doesn’t work with the Android SDK: Eclipse says “The JAR of this class file belongs to container ‘Android 1.5’ which does not allow modifications to source attachments on its entries.”
Which is a roundabout way of saying that the source path is fixed. If you open up the Eclipse project properties dialog, change to the Java Build Path page and Libraries tab, then expand the ‘Android 1.5’ library container and then the android.jar file (phew!), you’ll see the ‘Source attachment’ option, which shows where the source is expected to be.
For the Android 1.5 SDK, this is
Location/platforms/android-1.5/sources (and presumably similarly for
the Android 1.1 target), where
SDK Location is the path set in the
workspace preferences’ Android page. Note that the 1.0 SDK (which only
supported the 1.0 target, of course) just appends the string
sources to the
SDK Location, on the assumption that the SDK location ended with a trailing
slash. (This should only be something to look out for if you need to target
1.0; I’m pretty sure it was fixed in the plugin provided with the 1.1 SDK.)
There are two ways to put the source JAR into the right place. The
conventional way is to create a directory called
platforms/android-1.5/ (or 1.1), and then unzip the JAR (which is just a ZIP
file) into that directory. However, although
sources doesn’t have an
extension, Eclipse doesn’t actually require it to be a directory, so a simpler
way is just to rename your source JAR so that it’s called
sources, and move
it into the relevant directory.
Once you’ve moved the file or files into the right places, you just need to get Eclipse to take note of it. I found that just restarting Eclipse was enough, but if that doesn’t work you could always try refreshing the project, or closing and re-opening the project via the context menus.