Software Components will not be created automatically for Maven publishing from Android Gradle Plugin 8.0

42.4k Views Asked by At

With Gradle 7.2 and these plugins:

plugins {
    id 'com.android.library' // Android Gradle Plugin 7.1.2
    id 'maven-publish'
}

It still works, but gives me this deprecation warning:

WARNING: Software Components will not be created automatically for Maven publishing from Android Gradle Plugin 8.0. To opt-in to the future behavior, set the Gradle property android.disableAutomaticComponentCreation=true in the gradle.properties file or use the new publishing DSL.

Also the release notes mention it, but these refer to outdated documentation:

Starting AGP 8.0, automatic component creation will be disabled by default. Currently, AGP 7.1 automatically creates a component for each build variant, which has the same name as the build variant, and an an all component that contains all the build variants. This automatic component creation will be disabled. To transition to the new behavior, you should manually disable automatic component creation by setting android.disableAutomaticComponentCreation to true.
For more information, see Use the Maven Publish plugin.


But when enabling preview for the AGP 8.0 default behavior in file gradle.properties:

android.disableAutomaticComponentCreation=true

It cannot find property components.release:

FAILURE: Build failed with an exception.

* Where:
Script 'publish.gradle' line: 53

* What went wrong:
A problem occurred configuring project ':library'.
> Could not get unknown property 'release' for SoftwareComponentInternal set of type org.gradle.api.internal.component.DefaultSoftwareComponentContainer.

The offending line reads:

release(MavenPublication) {
    from components.release
}

The variant is is still there, but it doesn't create a component anymore:

androidComponents {
    onVariants(selector().all(), {
        println "$it.name"
    })
}

How can I upgrade to this "new publishing DSL" and create a software component to publish?

8

There are 8 best solutions below

0
Martin Zeitler On BEST ANSWER

According to PublishingOptions, one has to define an android.publishing block:

android {
    publishing {
        singleVariant('release') {
            withSourcesJar()
            withJavadocJar()
        }
        // ...
    }
}

To define multiple variants at once:

android {
    publishing {
        multipleVariants {
            withSourcesJar()
            withJavadocJar()
            allVariants()
        }
    }
}

Then eg. components.getByName('release') will be known again.

0
n8yn8 On

The Android Studio team just published some helpful user documentation at https://developer.android.com/studio/publish-library/configure-pub-variants

2
Deepak Sharma On

In your gradle.properties file inside the android directory, you can add the following line to disable this warning

android.disableAutomaticComponentCreation=true

enter image description here

1
Shiva Yadav On

To avoid this warning, In the gradle.properties file, you can add

android.disableAutomaticComponentCreation=true 
0
TheSaulMX On

I have this issue too. The solution for me was do the instruction of the warning, add android.disableAutomaticComponentCreation=true to your gradle.properties and the warning is gone.

The version of my react native is 0.71.4 (expo 48.0.9)

0
Peter On

In my case, I have solved the issue

    android {
//..
 publishing {
        singleVariant('release') {
            withSourcesJar()
            withJavadocJar()
        }

        singleVariant('debug') {
            withSourcesJar()
            withJavadocJar()
        }
    }
}

and

    afterEvaluate {
    publishing {
        publications {
            // Creates a Maven publication called "release".
            release(MavenPublication) {
                // Applies the component for the release build variant.
                components.getByName('release')

                // You can then customize attributes of the publication as shown below.
                groupId = 'your groupId'
                artifactId = 'your artifactId'
                version = 'v1.3'
            }
            // Creates a Maven publication called “debug”.
            debug(MavenPublication) {
                // Applies the component for the debug build variant.
                components.getByName('debug')

                groupId = 'your groupId'
                artifactId = 'your artifactId'
                version = 'v1.3'
            }
        }
    }
}
0
tomstan11 On

Try restarting your phone, it worked for me.

0
FarP1ace On

above gradle 8.0 maybe you should add this,then you can see components.release appear.

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