将“Gradle”和“Android Gradle 插件”从版本 7 更新到版本 8 后无法发布 Android 库

Cannot publish Android library after updating "Gradle" and the "Android Gradle Plugin" from version 7 to version 8

提问人:Adil Hussain 提问时间:7/14/2023 最后编辑:Adil Hussain 更新时间:7/14/2023 访问量:276

问:

我有一个 Android 库项目,它使用版本 7 的“Gradle”和“Android Gradle 插件”(准确地说分别是 7.6.1 和 7.4.2 版本)。

以下是我的图书馆文件中与发布相关的部分:build.gradle

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'

task generateSourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    archiveClassifier.set('sources')
}

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release
                artifact generateSourcesJar

                groupId 'com.tazkiyatech'
                artifactId 'android-utils'
                version '1.0.0'
            }
        }

        repositories {
            maven {
                name = 'BuildFolder'
                url = "${project.buildDir}/repository"
            }
        }
    }
}

我的项目可用的各种 Gradle 任务工作正常,直到我将项目中的“Gradle”和“Android Gradle 插件”版本提升到版本 8。升级到版本 8 后,各种 Gradle 任务将失败并返回以下错误:publish...publish...

* What went wrong:
A problem was found with the configuration of task ':library:generateSourcesJar' (type 'Jar').
  - Gradle detected a problem with the following location: '/Users/adil/Work/TazkiyaTech/android-utils/library/build/libs/library-sources.jar'.
    
    Reason: Task ':library:generateMetadataFileForReleasePublication' uses this output of task ':library:generateSourcesJar' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed.
    
    Possible solutions:
      1. Declare task ':library:generateSourcesJar' as an input of ':library:generateMetadataFileForReleasePublication'.
      2. Declare an explicit dependency on ':library:generateSourcesJar' from ':library:generateMetadataFileForReleasePublication' using Task#dependsOn.
      3. Declare an explicit dependency on ':library:generateSourcesJar' from ':library:generateMetadataFileForReleasePublication' using Task#mustRunAfter.
    
    For more information, please refer to https://docs.gradle.org/8.2.1/userguide/validation_problems.html#implicit_dependency in the Gradle documentation.

鉴于我无法弄清楚如何在我拥有的任务和我不拥有的任务之间创建依赖关系,我无法操作错误输出中列出的可能解决方案。generateSourcesJargenerateMetadataFileForReleasePublication

如何解决此错误并使用版本 8 的“Gradle”和“Android Gradle 插件”发布我的库?

android-gradle-plugin android-library maven-publish

评论


答:

0赞 Adil Hussain 7/14/2023 #1

自从我将库升级到版本 7 的“Gradle”和“Android Gradle 插件”以来,我看到 Android Gradle 插件>发布库文档中的说明发生了重大变化。

根据最新说明(适用于“Gradle”和“Android Gradle 插件”版本 8),我的库文件中与发布相关的部分将更改为以下内容:build.gradle

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'

android {
    publishing {
        singleVariant('release') {
            withSourcesJar()
        }
    }
}

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release

                groupId 'com.tazkiyatech'
                artifactId 'android-utils'
                version '1.0.0'
            }
        }

        repositories {
            maven {
                name = 'BuildFolder'
                url = "${project.buildDir}/repository"
            }
        }
    }
}

最重要的变化是,我不再需要定义自定义 Gradle 任务来为我的库源生成 Jar。

我在这里整理了一个最小的项目,它完全演示了如何使用版本 8 的“Gradle”和“Android Gradle 插件”发布 Android 库。