如何为android库生成javadoc,当它有依赖,也是aar库?

时间:2016-03-07 20:59:34

标签: android gradle android-gradle javadoc android-library

我有android库项目,这依赖于其他android库项目。我需要为库生成javadoc,但它失败了,因为gradle将javadoc类路径路径放到.aar位置,但是javadoc需要.jar文件。

简化的gradle文件:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    configurations {
        javadocDeps
    }

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 23
        versionCode 1
        versionName "0.1.0"
    }
}

dependencies {
    compile 'com.android.support:support-v4:23.2.0'
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'com.annimon:stream:1.0.7'
    javadocDeps 'com.android.support:support-annotations:23.2.0'
    javadocDeps 'com.nineoldandroids:library:2.4.0'
    javadocDeps 'com.android.support:support-v4:23.2.0'
}

task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier = 'sources'
}    

task javadoc(type: Javadoc, dependsOn: explodeAars) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    classpath += configurations.javadocDeps
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

artifacts {
    archives javadocJar
    archives sourcesJar
}

可能有3个解决方案:

1)以某种方式从每个aar库添加到类路径路径classes.jar中它依赖于build / intermidiates / explosion-aar / library / version / jars / classes.jar 我不知道如何在javadoc任务中包含这些路径。

2)从aar文件手动解包classes.jar并将它们添加到javadoc任务的classpath

3)非常脏的黑客 - 硬编码到库的路径 - 但我认为这是错误的。

如何使用gradle dsl实现1或2?

5 个答案:

答案 0 :(得分:6)

我设法通过提取每个AAR文件中包含的classes.jar并将其添加到javadoc任务的类路径来自动化 Guillaume Perrot 的解决方案。

它似乎适用于Android Studio 2.3和Gradle 3.3上的AAR依赖项和AAR模块

import java.nio.file.Files
import java.nio.file.Paths
import java.io.FileOutputStream
import java.util.zip.ZipFile

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += configurations.compile
    classpath += configurations.provided

    afterEvaluate {
        // Wait after evaluation to add the android classpath
        // to avoid "buildToolsVersion is not specified" error
        classpath += files(android.getBootClasspath())

        // Process AAR dependencies
        def aarDependencies = classpath.filter { it.name.endsWith('.aar') }
        classpath -= aarDependencies
        aarDependencies.each { aar ->
            // Extract classes.jar from the AAR dependency, and add it to the javadoc classpath
            def outputPath = "$buildDir/tmp/aarJar/${aar.name.replace('.aar', '.jar')}"
            classpath += files(outputPath)

            // Use a task so the actual extraction only happens before the javadoc task is run
            dependsOn task(name: "extract ${aar.name}").doLast {
                extractEntry(aar, 'classes.jar', outputPath)
            }
        }
    }
}

// Utility method to extract only one entry in a zip file
private def extractEntry(archive, entryPath, outputPath) {
    if (!archive.exists()) {
        throw new GradleException("archive $archive not found")
    }

    def zip = new ZipFile(archive)
    zip.entries().each {
        if (it.name == entryPath) {
            def path = Paths.get(outputPath)
            if (!Files.exists(path)) {
                Files.createDirectories(path.getParent())
                Files.copy(zip.getInputStream(it), path)
            }
        }
    }
    zip.close()
}

答案 1 :(得分:4)

此功能仅适用于早于2.3的Android Studio和/或早于3.3的Gradle

要从AAR添加JAR,您可以将以下doFirst添加到javadoc任务中:

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
}
.doFirst {
    classpath += fileTree(dir: "$buildDir/intermediates/exploded-aar/", include:"**/classes.jar")
}

它会将所有AAR中的所有.jar个文件添加到javadoc类路径中。 (您提出的解决方案中的选项1)

答案 2 :(得分:4)

@rve的解决方案现在在Android Studio 2.3 / Gradle 3.3上被解除,因为exploded-aar不再存在(在构建目录中没有其他选择)。

如果您所依赖的aar不是项目中的模块,则首先需要先提取classes.jar,然后再在类路径中引用它(基本上手动重新创建intermediates/exploded-aar)。

如果您依赖的aar只是项目中的另一个模块,您还可以使您的javadoc任务依赖于该模块的编译任务并引用该模块的intermediates/classes/release(如果您使javadoc依赖于assembleRelease)例如)。解决方法的一个示例:https://github.com/Microsoft/mobile-center-sdk-android/pull/345/files

我真的希望有人能提出更好的解决方案。

答案 3 :(得分:1)

我正在运行新的Android Studio 3.0-beta7,并试图使用@ nicopico的答案,但它失败了许多不同的错误,所以这里有一个改编它没有&#39 ; t依赖于不存在的java.nio实用程序。

task javadoc(type: Javadoc) {
    failOnError false
    source = android.sourceSets.main.java.srcDirs
    // Also add the generated R class to avoid errors...
    // TODO: debug is hard-coded
    source += "$buildDir/generated/source/r/debug/"
    // ... but exclude the R classes from the docs
    excludes += "**/R.java"

    // TODO: "compile" is deprecated in Gradle 4.1, 
    // but "implementation" and "api" are not resolvable :(
    classpath += configurations.compile

    afterEvaluate {
        // Wait after evaluation to add the android classpath
        // to avoid "buildToolsVersion is not specified" error
        classpath += files(android.getBootClasspath())

        // Process AAR dependencies
        def aarDependencies = classpath.filter { it.name.endsWith('.aar') }
        classpath -= aarDependencies
        aarDependencies.each { aar ->
            System.out.println("Adding classpath for aar: " + aar.name)
            // Extract classes.jar from the AAR dependency, and add it to the javadoc classpath
            def outputPath = "$buildDir/tmp/exploded-aar/${aar.name.replace('.aar', '.jar')}"
            classpath += files(outputPath)

            // Use a task so the actual extraction only happens before the javadoc task is run
            dependsOn task(name: "extract ${aar.name}").doLast {
                extractEntry(aar, 'classes.jar', outputPath)
            }
        }
    }
}

// Utility method to extract only one entry in a zip file
private def extractEntry(archive, entryPath, outputPath) {
    if (!archive.exists()) {
        throw new GradleException("archive $archive not found")
    }

    def zip = new java.util.zip.ZipFile(archive)

    zip.entries().each {
        if (it.name == entryPath) {
            def path = new File(outputPath)

            if (!path.exists()) {
                path.getParentFile().mkdirs()

                // Surely there's a simpler is->os utility except
                // the one in java.nio.Files? Ah well...
                def buf = new byte[1024]
                def is = zip.getInputStream(it)
                def os = new FileOutputStream(path)
                def len

                while ((len = is.read(buf)) != -1) {
                    os.write(buf, 0, len)
                }
                os.close()
            }
        }
    }
    zip.close()
}

让我感到困扰的是,我们需要所有这些代码来为库创建一个令人讨厌的javadoc,但至少我得到了它的工作。但是,我确实需要找到configuration.api和configuration.implementation无法解析的解决方法。

答案 4 :(得分:0)

这就是我使用Public Sub FillArray_1() array1 = VBA.Array( _ "member_1", _ "member_2", _ "member_3") End Sub 解决此问题的方式。配置:Gradle 4.10,Gradle插件:3.3.2,Android Studio:3.4。

zipTree