79

Having the source code attached to external libraries is awesome. Where do I find the source code for the v4 support package? Preferably, it would be a zip file which could be easily attached to the android-support-v4.jar in Eclipse.

durron597
  • 30,764
  • 16
  • 92
  • 150
Håvard Geithus
  • 5,167
  • 5
  • 30
  • 49

9 Answers9

123

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.

Edward Brey
  • 35,877
  • 14
  • 173
  • 224
andr
  • 15,322
  • 10
  • 40
  • 54
  • 9
    This is the 'official' and recommended way to do it. – BoD Jan 21 '13 at 11:57
  • 10
    Note that if you have a big project with multiple referenced libraries which in their turn reference the android support lib, you must make sure that all copies of the android-support-v4.jar are same binaries. Otherwise you'll get a strange error "checksum mismatch bla bla". Also only one of these support lib copies will be imported, maybe one contained in lib\ folder of the library projects. To see which one is used, open Package Explorer -> Android Dependencies and see where the support lib is imported from. Then do the same procedure not for the main project, but for the library project. – WindRider Jan 31 '13 at 19:16
  • @WindRider great, will keep that in mind.. thanks for contributing :) – andr Feb 01 '13 at 00:56
  • Yes, that stupid thing bothered me for months until i finally decided to find the reason. There are some additions in the ADT addressing conflicting dependency libs. It is something related to the DEX tool and it was described in a Google I/O video. Now there is another problem: The source appears but is not the correct version. I think the revisions are mismatching. The sources in SDK are maybe be for a newer revisions. That's because SDK Manager is updating the support lib with newer revisions. I cannot believe how one could lose all day with such non-important problems! – WindRider Feb 01 '13 at 13:04
  • 2
    After trying some of the rest I can say that this is the best answer - should be accepted as the correct one. – Amir Uval Apr 28 '13 at 22:53
  • To emphasize, the `.properties` file must be created in the `libs` directory, like uval says. – DavidS Jul 04 '13 at 20:50
  • This is THE CORRECT ANSWER. – Jazib Sep 27 '13 at 20:33
  • 1
    I've noticed that sometimes this seems to stop working for some reason... Usually to the point of having to delete and recreate the .properties file to force it to work again, anyone else experiencing this? – syklon Oct 03 '13 at 22:07
  • What to do if I want to use this for android/support/v7/appcompat? In this case the .jar file is not listed in the libs folder. – Simulant Oct 24 '13 at 12:07
  • @zyklonSport nope - I'm actively using this method for several projects and a total of about 60 library source archives and found no issues so far. maybe try posting a new SO question and/or ADT newsgroup if you're still experiencing this - that way it'll certainly get more attention from smart people – andr Oct 24 '13 at 23:38
  • 2
    @Simulant - I'm not an expert on ActionBar compat lib, but from a quick glance I presume that to use it, you have to import it as a separate Android Lib project and reference it as a dependency in your main project, right? If so, you just need to follow the steps described here for the "libs" dir *of the lib project*. That dir should have two `.jar` files: `android-support-v4.jar` and `android-support-v7-appcompat.jar` for which you should be able to created mentioned `.properties` files. – andr Oct 24 '13 at 23:45
  • @andr - yes that works fine. I tried it myself in the wrong project. So I had to add the properties in the Library-Projects libs folder. Thanks a lot! – Simulant Oct 25 '13 at 08:31
  • 1
    Is there a variable or macro available so that the ADT tooling path doesn't need to be hard coded? The .properties file is a good candidate for checking to source control and sharing between developers. – Edward Brey Nov 05 '13 at 14:50
  • 1
    @EdwardBrey unfortunately no - AFAIK variable expansion *is not* performed for any kind of vars (Eclipse or OS). What works for me is placing all the sources in a separate directory (say `libsrc`) in the root project directory. I then keep `libsrc` in VCS (so that all developers share the same version of sources) and reference it like `src=../libsrc/acra-4.5.0-sources.jar`. – andr Nov 05 '13 at 21:55
  • THANKS A LOT!, finally an elegant way for doing this. @Håvard Please mark it as the right answer for future searchers. Thanks. – Shirane85 Jan 01 '14 at 08:16
  • If you're wondering which project Eclipse will look for the source it will be the first project you open. So if you open your referenced library that contains the support library project first, the source will be referenced there. If you open the referencing project first, it will use that. So just open the project that contains the source library first then the others after that. Or just use the referenced library solution that other users have suggested. :) – Brad Feb 27 '14 at 08:58
  • If you need the javadoc, you can first goto `/extras/android/supportv/`, then create the javadoc by `javadoc -d docs -sourcepath src/java -subpackages android.support.v4`. The final step is to add a line `doc=c:/apps/adt-bundle-windows-64bit/sdk/extras/android/support/v4/docs` in `android-support-v4.jar.property`. Close and reopen your project, and you can see the javadoc for support library now. – lins05 Mar 08 '14 at 05:28
  • 5
    If you're using Windows, make sure you use forward slashes in the `src` line. – Scutterman May 27 '14 at 22:53
83

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.

Jeremy Stein
  • 17,438
  • 15
  • 64
  • 80
Hiep
  • 2,191
  • 1
  • 22
  • 29
  • 2
    "Java Build Path" and not "Configuration Build Path" for me. – user123321 Dec 11 '12 at 23:58
  • 4
    This had all of the needed steps and worked for me! The last step (ordering) was left out of others and very important. – cottonBallPaws Jan 16 '13 at 17:48
  • 3
    I had android-support-v4.jar listed in Android Private Libraries (APL, which was listed above Android Dependencies) so I had to pull the newly added .jar above APL – Kuitsi Jun 06 '13 at 22:45
  • 1
    For the record, this works with other libraries as well, not just the support library. /Captain Obvious – Emil Lundberg Jun 07 '13 at 23:24
  • 2
    You shouldn't have to do this. It goes against the initial setup of a standard android application. I would recommend you use the "official" solution by @andr. – JRomero Aug 09 '13 at 18:14
  • An eclipse restart was needed as well. Don't know why :( – AlikElzin-kilaka Nov 11 '13 at 13:09
  • In the "Order and Export" tab I had to pull up external jar above *Android Private Libraries* instead of *Dependencies* for me to work – Adam Toth Jan 07 '14 at 15:27
  • @J.Romero, How is the other solution official in any way? Seems to me like an undocumented feature found by someone browsing through Eclipse's source code. – Pacerier Nov 24 '14 at 10:33
66

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.

Jacob Dam
  • 1,668
  • 14
  • 16
  • 12
    In my case it was stored in `C:\Program Files\Android\android-sdk\extras\android\compatibility\v4\src\java`. I then took its content (the android folder) an packaged it up into a *.jar with this command: `jar cf android-support-v4-sources.jar android/`. I attached this jar to android-support-v4.jar (java build path / library settings in eclipse). Works nice :) – Håvard Geithus Jan 03 '12 at 21:53
  • 5
    This does not seem to work now. How can I make it work? help.I am using adt 21.1. The the android-support-v4.jar's doc source or java source is non-modifiable. Right click project, Properties > Java Build Path > Libraries Tab, Android Private Libraries> android-support-v4.jar - Source attachment:(None)- non modifiable. :( – Thupten May 20 '13 at 15:49
  • 3
    Ok.got it to work. The android-support-v4.jar need to be in `Referenced Libraries` by right click, `Build Path..`, `Add to Path`. Then in `Referenced Libraries`, right click properties on android-support-v4 and set the sources. – Thupten Jun 03 '13 at 08:20
  • This answer does fully answer the OP's question. To attach it to your jar file in eclipse see answer by @andr – JRomero Aug 09 '13 at 18:11
  • @Thupten, Aren't you referencing **twice** then when one will suffice? – Pacerier Nov 24 '14 at 10:21
16

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.

rekire
  • 45,039
  • 29
  • 149
  • 249
happydude
  • 3,818
  • 1
  • 21
  • 41
  • 5
    If you don't have the bug which doesn't let you attach sources to the jar, of course. – Ixx Aug 01 '12 at 18:44
  • 1
    This would be awesome but my "edit" button is greyed out :/. Is that the bug you mention lxx? Found a solution? – span Aug 02 '12 at 06:36
  • 3
    It is all greyed out, it says "non modifiable" – Mister Smith Oct 01 '12 at 15:59
  • 2
    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. You can then attach the source to this .jar file as indicated in the original answer. – happydude Oct 03 '12 at 05:52
2

For those who like the standard to have the jar file of the source code, which makes it more convenient for source control and sharing the project.

For example:

../android-support-v4.jar
../android-support-v4-src.jar

It is simple to create the source jar file and attach it:

  1. cd to path_to_android_sdk/extras/android/compatibility/v4/
  2. Use your favorite zip program such as 7-zip to create a zip file and name it android-support-v4-src.jar. The first folder in the jar should be /scr.
  3. Copy the file to your project, in this example it is in the same folder as the code jar.
  4. In Eclipse attach the source jar in project properties.
fishjd
  • 1,415
  • 1
  • 16
  • 28
1

I just remove the auto generated one , then manual add it as a Referencde Libraries.

First open a class, the IDE will ask you to Change Attached Source.

Hisenberg
  • 11
  • 1
0

The process of attaching the src and doc from build path works for some and it doesn't for some (like me). some key things to keep in mind

  1. Make sure you are in Package Explorer, not Project Navigator.

  2. If you have put your android-support-v4.jar in libs folder under your project. great.

  3. Right click the jar, Build path.. Add to Path. (if Add to Path does not show up then its already added. you will see configure path..)

  4. As the result of step 3, a new folder called Referenced Libraries will appear in package explorer tree. Open that tree, find the android-support-v4.jar there. Right click on this jar in the Referenced Libraries, properties. Then set the Java Source Attachment and Javadoc Location there.

  5. You are done.

The path for my Java Source Attachment.(its external location)

C:/Users/thupten/adt-bundle-windows-x86_64-20130514/sdk/extras/android/support/v4/src

I used the android website doc for java doc location

http://developer.android.com/reference/android/support/v4/app/package-summary.html

Thupten
  • 2,038
  • 1
  • 22
  • 30
0

I found this for me:

For main lib: android.jar:

src: sdk/sources/android-19 doc: sdk/docs/reference/

For support lib: android-support-v4.jar: (before this we should add android-support-v4.jar like external jar (Project Properties -> Java Build Path -> Libraries, then in Tab ‘Order and Export’ pull up this library before Android Private Libraries)):

src: sdk/extras/android/support/v4/src/java doc: http://developer.android.com/reference/android/support/v4/app/package-summary.html (not sure)

ultraon
  • 1,630
  • 2
  • 20
  • 29
0

After you have updated the SDK and downloaded Documentations in it:

  • Import support library project to package explorer.
  • Create a folder in the library and name it something like ext-jars.
  • Move android-support-v4.jar to ext-jars folder.

    enter image description here

  • Right click on the project and click on Properties.
  • Click on Java Build Path in the left menu then select Libraries tab.
  • Click on Add JARs... then expand the project and select the jar file you already moved to folder ext-jars.

enter image description here

  • Expand the added jar and select Source attachment then click on Edit.
  • Select External location then click on External Folder...
  • Choose the source folder for the Support v4 which is located in [sdk-path]/extras/android/support/v4/src

enter image description here

  • Select Javadoc location click on Edit then select Javadoc URL then click on Browse... and choose the javadoc location for support v4 which is located in [sdk-path]/docs/reference/

enter image description here - Select Order and Export tab and check the android-support-v4.jar you just added.

I suggest you also clean the project and reopen the Eclipse.

M D P
  • 4,209
  • 2
  • 18
  • 34