Android Studio: How to attach javadoc

2019-01-12 23:11发布

问题:

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!

回答1:

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)



回答2:

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
  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
  5. The result was:
  6. And after a project sync I could browse the javadoc of the google play services

Note:

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



回答3:

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

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.."



回答4:

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).


回答5:

Still a current issue as of my posting it seems:

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



回答6:

After some investigation, I stumbled upon this:

https://github.com/xujiaao/AARLinkSources

Works like magic!



回答7:

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
    } 
}


回答8:

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).



回答9:

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



回答10:

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.