Search code examples
androidgradleandroid-gradle-pluginmaven-publish

Android Library publishing fails after recommended AGP update to 7.1.1/Gradle 7.2


My build is failing after performing the upgrade to AGP 7.1.1 and performing the automated upgrade steps from Android Studio in a library project.

I am following the following guide: https://developer.android.com/studio/build/maven-publish-plugin

We've been using this for quite awhile without issue.

After performing the automated steps from the IDE, I now get the following error:

A problem occurred configuring project ':some-lib'.
> Could not find method publications() for arguments [build_546b384slk3v64hu8jsdnbywh$_run_closure1$_closure5$_closure9$_closure10@333a4a39] on object of type com.android.build.gradle.internal.dsl.LibraryPublishingImpl$AgpDecorated.

My build.gradle file looks like so:

android { 
 ...

     afterEvaluate {
        publishing {

            publications {
                mavenAar(MavenPublication) {
                    from components.release
                }
            }

            repositories {
                maven {
                    url artifact_repo_url
                    credentials {
                        ...
                    }
                }
            }
        }
    }
}

My gradle-wrapper.properties looks like so:

distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

Any suggestions for how to fix this?


Solution

  • The answer to my question is shown here: https://developer.android.com/reference/tools/gradle-api/7.1/com/android/build/api/dsl/LibraryPublishing

    The afterEvaluate{ publishing {} } section should be outside of the android {} block and in its own afterEvaluate {}

    We also need to add a publishing {} section in the android block that specifies the variant/s to publish.

    For me the result looked something like this:

    android {
        publishing {
            singleVariant("release")
        }
    }
    
    
    afterEvaluate {
        publishing {
            publications {
                release(MavenPublication) {
                    from components.release
                    // ......
                }
            }
        }
    }