当前位置: 首页 > 知识库问答 >
问题:

Android AAR依赖于AAR,javadoc生成失败

郏兴贤
2023-03-14

我有一个android gradle项目结构,看起来像这样

    < li >模块1-aar < li >模块2-aar < li>testapp-apk

关键事实

  • 模块2 aar取决于模块1 aar。
  • testapp apk取决于模块2 aar。
  • JDK11
  • Gradle 7.4.2
  • Androidgradle插件7.1.3

没有javadocs、gpg、签名或发布,一切都构建得很好。App运行,一切都很棒。

当我开始添加任务来生成javadocs时,一切都变得混乱。module1 aar将毫无问题地构建和生成javadocs。然而,模块2 aar在javadoc任务期间总是失败。

任务在下面。大部分都是从这里借用的。当它有依赖项时,如何为android库生成javadoc,这些依赖项也是aar库?

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之前编译。

不幸的是,关于< code>configuration在引用android项目时的含义的文档有点单薄,或者我可能找错了地方。我不确定还有哪些有效值可用。

不管怎样,我不知道这里出了什么问题,除了我在这上面花了太多时间。

共有2个答案

吕嘉赐
2023-03-14

我知道你有一个这样的项目结构:应用程序 -

其中<代码>-

基本上可以归结为在每一个模块的每一个层次上实现这一点:https://stackoverflow.com/a/73096187/9902249

我希望它对你有用。

严兴言
2023-03-14

我将发布我在javadoc中使用“aar”文件的解决方案。在试图解决这个问题的过程中,我也遇到了与spy所指的相同的错误。实际上,这个错误意味着它可以区分它应该使用发布库还是调试库。在我看来,试图纠正这个问题是徒劳无益的,因此,我采取了不同的方法来解决我认为本质上相同的问题。

就我而言,我有一个包含多个子项目的项目,当我生成我的javadoc留档时,我想生成一个合并的javadoc文档,它只包含一些子项目(不是所有的子项目)。据我所知,这不是Android Studio内置的功能。当前版本的Android Studio(2021.2.1)似乎在为android库模块生成javadoc留档时存在问题。有两个问题:

1.)javadoc类路径没有添加android引导类。引用任何android SDK方法(如“上下文”、“字体”等)都会出现错误。

2.)javadoc类路径没有添加任何AndroidX库。许多AndroidX库都是“aar”文件。Android Studio(2021.2.1)在使用javadoc时无法正确处理aar文件。

我的环境类似于间谍,除了我使用的是android gradle插件7.2.0。我在“应用”模块的构建.gradle.kts 脚本中创建了一个定义的 javadoc 任务。我的“应用程序”模块是一个Android应用程序模块。代码需要放置在包含插件“com.android.应用程序”或“com.android.库”的任何模块中。我为合并的 javadoc 生成的一些模块是 Java 库,这没关系。

 // create a custom configuration  
 val javadocDeps = configurations.create("javadocDeps")

 // add javadoc dependencies that you need.
 dependencies {
     javadocDeps(project(":OsgiFramework"))
     // note: I'm using a libs version catalog for the dependencies
     // you can add hardwired dependencies if you prefer
     javadocDeps (libs.androidx.appcompat)
     javadocDeps (libs.androidx.fragment)
     javadocDeps (libs.androidx.navigation.fragment)
     javadocDeps (libs.androidx.navigation.ui)
     javadocDeps (libs.androidx.constraint.layout)
 }

 // register the createCoreJavadoc task
 // in my case, "gradlew app:createCoreJavadoc" creates the merged javadoc 
 tasks {
    register<Javadoc>("createCoreJavadoc") {
        setFailOnError(true)
        val docDir: File = File(project.projectDir.parentFile.parentFile, "Doc/Core")
        println("javadoc destination dir: " + docDir.absolutePath)
        // set the location where the documentation is produced in
        setDestinationDir(docDir)

        // select the projects to produce merged javadoc for
        var sourcepaths: FileCollection = project(":CoreInterfaces").files("src/main/java")
        sourcepaths =
            sourcepaths.plus(project(":CoreInternalInterfaces").files("src/main/java"))
        sourcepaths = sourcepaths.plus(project(":CoreAndroidInterfaces").files("src/main/java"))
        sourcepaths =
            sourcepaths.plus(project(":CoreAndroidInternalInterfaces").files("src/main/java"))
        sourcepaths = sourcepaths.plus(project(":OsgiInterface").files("src/main/java"))
        sourcepaths =
            sourcepaths.plus(project(":InstallPlanInterfaces_1_0_0").files("src/main/java"))
        setSource(sourcepaths.asFileTree)


        // fix the problem with the missing android bootclasses
        android.bootClasspath.forEach{
            classpath += fileTree(it)
        }

        // create a temporary directory for storing the "classes.jar" file contained in the *.aar files
        val tmpDir:File = File(project.buildDir, "\\tmpAar\\")
        if (tmpDir.exists()) tmpDir.delete()
        tmpDir.mkdirs()
        // add the javadoc dependencies
        javadocDeps.forEach {
            // I've got a custom class that allows me to treat jar or zip files and a file system
            // you could replace this using spy's zip file extraction method
            val zipFileSystem: com.phinneyridge.ZipFileSystem = ZipFileSystem(it.absolutePath,null)
            if (it.name.endsWith(".aar")) {
                // extract the classes.jar file from the aar file to the tmpDir
                // renaming it to name of the aar file, but change the extension to jar
                val tmpFile:File = File(tmpDir, it.name.replace(".aar", ".jar"))
                zipFileSystem.extractEntry("classes.jar", tmpFile)
            } else {
                // for jar files, we just add it to the path
                classpath += fileTree(it)
            }
        }
        // now add the tmpDir files to the javadoc classpath
        classpath += fileTree(tmpDir)
        // for diagnosis purposes, we'll print the classpath.
        // Notice that you have a lot more path entries then you have javadocDeps
        println("classpath: " + classpath.asPath)

    }
}
 类似资料:
  • 我有Android库项目,这依赖于其他Android库项目。我需要为库生成javadoc,但它失败了,因为gradle将javadoc类路径设置为。aar位置,而javadoc需要。jar文件。 简化分级文件: 如何用gradle DSL实现1或2?

  • 我正在编写一个java jar库。我只想为Gradle的项目添加游戏服务依赖项。我的gradle构建脚本: Gradle sync完成得很好,android studio告诉我一切都好,但当我试图构建: 我的gradle版本2.13。另外,我使用了最新的gradle版本候选版本。没什么。

  • 我已经用java编写了一个RESTful服务,并希望用get方法生成json。由于jar版本或依赖关系的问题,我一直在与许多没有找到类定义的错误作斗争。 例如 ,杰克逊错误:java.lang.NoClassDefFoundError: com/fasterxml/Jackson/core/Versioned 引起者:java.lang.NoClassDefFoundError: com/aste

  • 我的Build.Gradle如下: 我得到的错误如下所示。

  • 我正在创建构建一个jar,其中也将包括它的依赖关系。我正在使用maven阴影插件。但是获取错误。我试图排除传递依赖项,如下所示,仍然得到错误。

  • 我试图用以下依赖项构建gradle。但它不是编译。我尝试了很多,通过查看论坛来更改版本。我在“项目结构”对话框中搜索依赖项,发现了以下两个。 实际上,我用的是两台电脑。这是在另一台电脑上制作的,有以下功能 仍然没有运气和gradle建造每次需要20-25分钟 主要build.gradle 应用build.gradle