【发布时间】:2022-07-24 15:07:05
【问题描述】:
我有一个看起来像这样的 android gradle 项目结构
- module1-aar
- module2-aar
- testapp-apk
关键事实
- module2-aar 依赖于 module1-aar
- testapp-apk 依赖于 module2-aar
- JDK11
- Gradle 7.4.2
- Android gradle 插件 7.1.3
没有 javadocs、gpg、签名或发布,一切都很好。应用程序运行,一切都很好。
当我开始添加任务以生成 javadocs 时,一切都变得混乱了。 module1-aar 将毫无问题地构建和生成 javadocs。然而,module2-aar 在 javadoc 任务期间总是失败。
任务如下。大部分都是从这里借来的How to generate javadoc for android library when it has dependencies which are also aar libraries?
project.task("javadoc", type: Javadoc) {
afterEvaluate {
configurations.all
.each {item ->
item.setCanBeResolved(true)
}
classpath += configurations.api
classpath += configurations.implementation
// 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
//fails here when an AAR depends on an AAR
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)
}
}
}
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
classpath += project.files(android.getBootClasspath())
classpath += configurations.implementation
classpath += fileTree(dir: project.buildDir.absolutePath + "/tmp/aarsToJars/")
classpath += files(project.buildDir.absolutePath + "/intermediates/compile_r_class_jar/release/R.jar")
classpath += files(project.buildDir.absolutePath + "/generated/source/buildConfig/release/release")
classpath += files(project.buildDir.absolutePath + "/generated/source/r/buildConfig/release/release")
destinationDir = file( project.buildDir.absolutePath + "/outputs/javadoc/")
failOnError true
options.charSet 'UTF-8'
options.docEncoding 'UTF-8'
options.encoding 'UTF-8'
options.addBooleanOption 'Xdoclint:none', true
exclude '**/BuildConfig.java'
exclude '**/R.java'
exclude '**/doc-files/*'
}
// 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()
}
//wires in the javadoc task to the normal build
tasks.named("build") { finalizedBy("generateJavadocJar") }
我收到的错误消息如下
* What went wrong:
A problem occurred configuring project ':module2-aar'.
> Could not resolve all files for configuration ':module2-aar:implementation'.
> Could not resolve project :module1-aar.
Required by:
project :module2-aar
> Cannot choose between the following variants of project :module1-aar:
- debugRuntimeElements
- releaseRuntimeElements
All of them match the consumer attributes:
- Variant 'debugRuntimeElements' capability com.github.test:module1-aar:6.1.11-SNAPSHOT:
- Unmatched attributes:
- Provides com.android.build.api.attributes.AgpVersionAttr '7.1.3' but the consumer didn't ask for it
- Provides com.android.build.api.attributes.BuildTypeAttr 'debug' but the consumer didn't ask for it
- Provides com.android.build.gradle.internal.attributes.VariantAttr 'debug' but the consumer didn't ask for it
- Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
- Variant 'releaseRuntimeElements' capability com.github.test:module1-aar:6.1.11-SNAPSHOT:
- Unmatched attributes:
- Provides com.android.build.api.attributes.AgpVersionAttr '7.1.3' but the consumer didn't ask for it
- Provides com.android.build.api.attributes.BuildTypeAttr 'release' but the consumer didn't ask for it
- Provides com.android.build.gradle.internal.attributes.VariantAttr 'release' but the consumer didn't ask for it
- Provides org.gradle.usage 'java-runtime' but the consumer didn't ask for it
我一直在玩 gradle 任务,似乎每当我尝试遍历 module2-aar 的类路径时都会生成错误消息。
我尝试了许多其他建议,例如更改 module2-aar 的依赖声明
api project(':module2-aar')
到
api project(path:':module2-aar')
但是这没有任何作用
我也试过这个:
api project(path: ':module1-aar', configuration: 'default')
虽然上述解决了报告的问题,但它会导致编译问题,即 module2-aar 在编译期间似乎在类路径中没有 module1-aar...而且它似乎在 module1-aar 之前编译。
不幸的是,configuration 在引用 android 项目时的含义的文档有点薄,或者我可能找错地方了。我不确定还有哪些其他有效值可用。
不管怎样,我不知道这里出了什么问题,除了我在这上面花了太多时间。
【问题讨论】:
-
您是否尝试将 aar 解压缩到一个模块中并为该模块运行 javadoc 命令?
-
手动?不。但这就是上述任务的作用
-
我认为您走在正确的道路上,但在我看来,您仍在将 aar 文件添加到 javadoc 类路径中,但只是将其重命名为 jar 文件。我认为您需要从 aar 文件中提取 classes.jar 文件并将提取的 classes.jar 文件添加到 javadoc 类路径中。
-
顺便说一句,不要再使用“配置”参数了。它只是出于遗留原因而存在,并且它的使用可能会产生重复类的问题。这是我从安卓开发团队得到的信息。
标签: android gradle javadoc aar