android-gradle-pluginandroid-librarymaven-publish

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


I have an Android library project which uses versions 7 of "Gradle" and the "Android Gradle Plugin" (versions 7.6.1 and 7.4.2 respectively to be precise).

Here are the parts of my library's build.gradle file which relate to publishing:

plugins {
    id("com.android.library")
    id("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"
            }
        }
    }
}

The various publish... Gradle tasks that are available to my project work fine until I bump up the versions of "Gradle" and the "Android Gradle Plugin" in the project to version 8. Once I upgrade to version 8, the various publish... Gradle tasks fail and return the following error:

* 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.

I've been unable to action the possible solutions listed in the error output given that I can't work out how to create a dependency between the generateSourcesJar task which I own and the generateMetadataFileForReleasePublication task which I don't own.

How can I get around this error and publish my library using versions 8 of "Gradle" and the "Android Gradle Plugin"?


Solution

  • I see that the instructions in the Android Gradle Plugin > Publish your library documentation have changed significantly since I upgraded my library to versions 7 of "Gradle" and the "Android Gradle Plugin".

    According to the latest instructions (for versions 8 of "Gradle" and the "Android Gradle Plugin"), the parts of my library's build.gradle file which relate to publishing change to the following:

    plugins {
        id("com.android.library")
        id("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"
                }
            }
        }
    }
    

    The most important change is that there's no longer a need for me to define a custom Gradle task which generates the Jar for my library's sources.

    I've put together a minimal project here which fully demonstrates how to publish an Android library with versions 8 of "Gradle" and the "Android Gradle Plugin".