如何使用“api”或“implementation”指令从gradle插件获取依赖项
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用“api”或“implementation”指令从gradle插件获取依赖项相关的知识,希望对你有一定的参考价值。
背景:运行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推荐的api
或implementation
将无法使用,因为这些无法从Gradle任务中解析。
问题:如何使用api
或implementation
指令获取依赖关系列表,例如configuration.api
呈现“无法解决”的错误?
奖金问题:是否有一种新的,更好的方法来为Android Studio 3.0创建一个不涉及100行解决方案的库的javadoc?
您可以等待合并:
https://issues.apache.org/jira/browse/MJAVADOC-450
基本上,当前的Maven Javadoc插件忽略了诸如AAR之类的分类器。
以上是关于如何使用“api”或“implementation”指令从gradle插件获取依赖项的主要内容,如果未能解决你的问题,请参考以下文章
Android compile、implementation和api的区别
gradle中api、implementation和compile的区别
Android Gradle 依赖配置:implementation & api