gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:59:48 24 4
gpt4 key购买 nike

背景:运行 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 插件曾经用“exploded-aar”做的事情。

但是,代码依赖于使用 compile依赖关系。使用 apiimplementation Gradle 4.1 推荐的那些将不起作用,因为这些不能从 Gradle 任务中解决。

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

额外问题:是否有一种新的、更好的方法来为使用 Android Studio 3.0 的库创建 javadoc,而不涉及 100 行解决方法?

最佳答案

您可以等待合并:

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

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

关于gradle - 如何使用 "api"或 "implementation"指令从 gradle 插件获取依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46810769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com