Android Studio: How to attach javadoc

Intellij IdeaGradleAndroid Studio

Intellij Idea Problem Overview


It might be very trivial question, But I couldn't find any option to attach javadoc/source with local jar dependencies (in libs folder) in android project. I can't believe I have spent a hour on such simple stuff :(

Google search result just tells about adding android documentation or adding javadoc in eclipse. That's not What I am looking for!

Intellij Idea Solutions


Solution 1 - Intellij Idea

I found a solution just now since I can't find any other solution for a long time.

suppose:

  1. your lib name is: libxxx.jar
  2. javadoc name is docs.zip

under folder .idea/libraries, you can find libxxx.xml. replace JAVADOC with

<JAVADOC>
  <root url="jar://C:/yourpath/doc.zip!/" />
</JAVADOC>

then ctrl+alt+y to sync the project. (Don't do "Sync Project with Gradle files", it will delete the changes)

Solution 2 - Intellij Idea

I've found & tested a way in which we can add the javadocs without loosing them after a gradle sync.

  1. Right click in Project panel > External libraries > lib-name
  2. Click Library properties Lib props
  3. Click specify documentation URL
  4. And specify an URL. For my machine it was file:///opt/android-sdk/extras/google/google_play_services/docs/reference Add doc url
  5. The result was: added jdoc
  6. And after a project sync I could browse the javadoc of the google play services google play services maps javadoc
Note:

This placed the same JAVADOC entry in the xml identified by Jason but doesn't disappear after doing a sync with gradle

Solution 3 - Intellij Idea

It is possible:

  1. Open Project structure
  2. Navigate to dependency that you want
  3. Press button from the screenshot and provide folder or file

screenshot 1

Another way to attach sources:

  1. Step into class from library (Hover mouse over class name and do CMD + Left click or CMD + B)
  2. You will see decompiled version of class there menu on the top right side of editor "Attach sources.."

screenshot 2

Solution 4 - Intellij Idea

I had problem with answer from Matyas that I wasn't seeing my local .jar library in the External Libraries list, because it only show maven attached libs.

Solution is mentioned here: https://code.google.com/p/android/issues/detail?id=73087#c26

  1. Right click on the "Structure" tab and check "Split Mode" (so you can open both "Project" and "Structure" tabs at same time).
  2. Open both "Project" and "Structure" tabs at the same time.
  3. In "Project" tab select "Android" perspective and then select your module folder (e.g. "app")
  4. In "Structure" window you should now see list of all libraries, including your local *.jar

Continue as in answer from Matyas:

  1. Right click on wanted library and select "Library Properties..."
  2. If you have *.jar with javadocs locally, you can press "add" button (green "+") and search for the file on your disk (you don't have to type "file://" path manually).

Solution 5 - Intellij Idea

Still a current issue as of my posting it seems:

https://code.google.com/p/android/issues/detail?id=73087

Solution 6 - Intellij Idea

After some investigation, I stumbled upon this:

https://github.com/xujiaao/AARLinkSources

Works like magic!

Solution 7 - Intellij Idea

I wasted so much time on this too...

Here's a gradle task which finds source and javadoc by location/naming convention, and registers them in the .idea files on sync. It belongs in the root gradle file's allProjects section. As-is, it expects to find [projectname]/libs/lib.jar next to lib-sources.jar and/or lib-javadoc.jar. Also, as noted in comments, if your javadocs not pathed at "/" inside the jar, you may need to change the script to add "docs/html" (for example) at the end of "jar://$doc!/".

allprojects {

    task addJavaDoc {
        afterEvaluate {
            // Specify paths, this will be run per non-root project
            def projectDir = project.getProjectDir().getCanonicalPath()
            def rootDir = project.getRootDir().getCanonicalPath()
            def lib = projectDir + '/libs'

            // println lib // Uncomment this to troubleshoot
            // Get any jar dependencies register in the lib folder
            fileTree(include: ['*.jar'], exclude: ['*-source.jar', '*-javadoc.jar'], dir: lib ).each { File jar ->
                def jarName = jar.getName()
                def moduleName = jarName.substring(0, jarName.lastIndexOf("."))
                // IntelliJ does this to file names when making the xml files
                def escapedName = moduleName.replace("-", "_").replace(".", "_")
                def xmlFile = "$rootDir/.idea/libraries/${escapedName}.xml"
                // println xmlFile // Uncomment this to troubleshoot
                if (new File(xmlFile).exists()) {
                    ['javadoc', 'sources'].each {String docType ->
                        // Get sources or java doc by naming convention, (expects name-sources or name-javadoc
                        def doc = "$lib/$moduleName-${docType}.jar"
                        // println doc // Uncomment this to troubleshoot
                        if(new File(doc).exists()) {
                            def xml = new XmlParser().parse(xmlFile);
                            def xmlTag = docType.toUpperCase()
                            // Perform xml replacement by convention
                            xml.library[xmlTag].replaceNode {
                                "$xmlTag" {
                                    root(url: "jar://$doc!/")
                                }
                            }
                            // Write out changes
                            new XmlNodePrinter(new PrintWriter(new FileWriter(xmlFile))).print(xml)
                            // Notify that changes worked
                            println "Fixed up reference to $doc"
                        }
                    }
                }
            }
        }
    }
}

Also, if you are using jcenter or mavencentral, javadocs and sources should work for downloaded jars without using that task, but you may have to add this in each non-root gradle file:

apply plugin: 'idea'

idea{
    module {
        downloadJavadoc = true
        downloadSources = true
    } 
}

Solution 8 - Intellij Idea

There is a solution, This procedure take place through terminal ,

I have tested solution in MAC OS.

  1. Move to your project folder

  2. ls -al (to show hidden files)

  3. Move to .idea folder , Command : cd .idea

  4. Move to libraries folder , Command : cd libraries/

  5. Now you can see list of all xml files for your libs or jars. Edit it like , vi open androidasync_2_1_7.xml

  6. In the editor screen , For inserting

    Press i

Now you see <SOURCES /> tag we have to provide a path here like,

<SOURCES>
  <root url="file://$PROJECT_DIR$/androidasync/src/main/java" />
</SOURCES>

For exiting

Press Esc

    :wq //for exiting and saving
    :q! //for exiting without saving

7) Restart Android studio (Sometime it needed also sync gradle).

Solution 9 - Intellij Idea

Personally tested successfully!

1.Project Structure(ctrl+alt+shift+s)

2.SDK Location

3.JDK Location

4.UnCheck "Use embedded JDK(recommended)"

5.Select your own jdk path(My Path:C:\Program Files\Java\jdk1.8.0_111)

6.Synchronized(ctrl+alt+y)

success

Solution 10 - Intellij Idea

in android studio if you use compileSdkVersion 23 in buidl.gradle and you have downloaded the SDK 23 document file and source file in SDK manager , the android API and java API doc and source will all show auto, you don't need to manual set.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAshish BindalView Question on Stackoverflow
Solution 1 - Intellij IdeaJason QiView Answer on Stackoverflow
Solution 2 - Intellij IdeaMatyasView Answer on Stackoverflow
Solution 3 - Intellij IdeaEugen MartynovView Answer on Stackoverflow
Solution 4 - Intellij IdeaRobyerView Answer on Stackoverflow
Solution 5 - Intellij IdeawaynesfordView Answer on Stackoverflow
Solution 6 - Intellij Ideazsolt.kocsiView Answer on Stackoverflow
Solution 7 - Intellij IdeanpjohnsView Answer on Stackoverflow
Solution 8 - Intellij IdeaRonak MehtaView Answer on Stackoverflow
Solution 9 - Intellij IdeaSavinoView Answer on Stackoverflow
Solution 10 - Intellij IdeataotaoView Answer on Stackoverflow