27

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?

Martin Zeitler
  • 1
  • 19
  • 155
  • 216

8 Answers8

33

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.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
6

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

n8yn8
  • 4,467
  • 4
  • 19
  • 20
4

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'
            }
        }
    }
}
Peter
  • 587
  • 6
  • 16
1

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)

saw303
  • 8,051
  • 7
  • 50
  • 90
TheSaulMX
  • 11
  • 1
0

Try restarting your phone, it worked for me.

tomstan11
  • 1,047
  • 7
  • 9
0

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

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

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
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 15 '22 at 17:54
  • See the question, this only enables the AGP 8.0 default behavior. – Martin Zeitler Jan 05 '23 at 05:23
-4

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

android.disableAutomaticComponentCreation=true 
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Shiva Yadav
  • 72
  • 1
  • 9