How do I attach the Android Support Library source in Eclipse?

Solution 1:

I just want to add yet another method of attaching sources for the support library. It requires ADT in version 20 or later. Supposedly this method works for all JARs for which setting source/javadoc location is disabled by the container. Here's what you need to do:

  1. The android-support-v4.jar library lies in the libs directory of your project. In that same directory create a regular Java properties file named exactly like the JAR in question, but with appended .properties extension. So, for our support library it'll be:
    android-support-v4.jar.properties.

  2. Open created properties file and set value of property named src to the location where sources for that library can be found. Your file should have one line like:

    src=c:/apps/adt-bundle-windows-64bit/sdk/extras/android/support/v4/src
    
  3. Save the file.

  4. Close and re-open your android project.

  5. Try browsing to one of the support classes. The source attachment should work now.

Worked perfectly in my case.

One thing to note: if src is not an absolute path, it will be resolved starting in the parent directory of the JAR file. Taking support library as an example - if src=support/src, ADT will assume that the class sources are located in libs/support/src.

Short description of this feature written by its author can be found here.

If anyone is interested in how exactly this .properties file is processed, I recommend reading patch set #4, esp. changes in eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/ internal/project/LibraryClasspathContainerInitializer.java :)

Edit

Please also see a fine comment by WindRider about adding sources properly in a project with multiple referenced libraries.

Solution 2:

Here the solution to attache the source of the support library in Eclipse Juno

I suppose that your project already has android-support-v4.jar in your "Build Path", under "Android Dependencies", but you cannot attach the sources directory to it. (the "Source attachment" said "Non modifiable"). Solution:

  • Goto "Configure Build Path"
  • Add External JARs > YourProject/libs/android-support-v4.jar (I know your project had already referenced to it but don't worry, just add it again).
  • Expand jar and attach Source to the External Jar: android-sdk/extras/android/support/v4/src
  • Switch to the "Order and Export" tab, pull up the external jar above the "Android Dependencies"

Enjoy navigating the support library with source!

if you have an "Android Test Project" attached to YourProject, so YourProjectTest might not compiled anymore. In this case, you have to return to "Order and Export" and pull down the external jar below the "Android Dependencies" to get things back to normal.

Solution 3:

After downloading the support package from the Android SDK Manager, you can find the source code of support package in folder <android-sdks>/extras/android/support/v4/src.

Solution 4:

Referencing the accepted answer, it is also possible to attach the source straight from the directory without building a .jar file. From the Java build path / libraries tab, expand android-support-v4.jar, highlight "Source attachment", click "Edit...", "External Folder..." then point to (android-sdk)\extras\android\support\v4.

This was tested using eclipse indigo.

From the comments:

The problem of being unable to add source to the support library seems to occur if your support library is located in the "Android Dependencies" folder of your project. The workaround is from the same "Java build path / libraries" tab click "Add External JARs..." and find the .jar file in your (android-sdk)\extras\android\support\v4 path. It will then appear in your project setup under a new "Referenced Libraries" folder.