如何使用“api”或“implementation”指令从gradle插件获取依赖项

问题描述 投票:4回答:1

背景:运行Android Studio 3.0-beta7并试图让一个javadoc任务适用于Android库(事实上,这首先不是一个现成的任务,这真的很奇怪),我设法调整了一个根据我的需要回答一个不同的问题,最后得到这个代码(https://stackoverflow.com/a/46810617/1226020):

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

此代码尝试查找所有依赖项AAR:s,循环遍历它们并从中提取classes.jar,并将它们放入在javadoc生成期间添加到类路径的临时文件夹中。基本上尝试重现真正的旧的Android gradle插件用于“爆炸 - aar”。

但是,代码依赖于使用compile依赖项。使用Gradle 4.1推荐的apiimplementation将无法使用,因为这些无法从Gradle任务中解析。

问题:如何使用apiimplementation指令获取依赖关系列表,例如configuration.api呈现“无法解决”的错误?

奖金问题:是否有一种新的,更好的方法来为Android Studio 3.0创建一个不涉及100行解决方案的库的javadoc?

gradle android-gradle android-studio-3.0
1个回答
0
投票

您可以等待合并:

https://issues.apache.org/jira/browse/MJAVADOC-450

基本上,当前的Maven Javadoc插件忽略了诸如AAR之类的分类器。

© www.soinside.com 2019 - 2024. All rights reserved.