Android Studio: How to attach javadoc

前端 未结 10 1894
一向
一向 2020-12-04 09:47

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

10条回答
  •  不思量自难忘°
    2020-12-04 10:32

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

提交回复
热议问题