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

8

32

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?

Chlorous answered 5/3, 2022 at 19:30 Comment(0)
C
42

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.

Chlorous answered 5/3, 2022 at 21:34 Comment(0)
L
6

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

Lip answered 10/3, 2022 at 20:55 Comment(0)
B
6

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'
            }
        }
    }
}
Biskra answered 24/5, 2023 at 14:51 Comment(0)
C
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)

Colostomy answered 31/3, 2023 at 23:15 Comment(0)
B
1

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

android {
    publishing {
        singleVariant('release') {
            withSourcesJar()
            withJavadocJar()
        }
    }
}
Bedabble answered 22/8, 2023 at 5:9 Comment(0)
C
0

According to the docs one needs to declare publishing {} within android {}. That will create a required component.

There are 2 options - publish as Android App Bundle (AAB) or as a ZIP file of APKs.

AAB:

android {
    ...
    publishing {
        // Publish your app as an AAB
        singleVariant("fullRelease")
    }
}

afterEvaluate {
    publishing {
        publications {
            fullRelease(MavenPublication) {
                from components.fullRelease
                // ......
            }
        }
    }
}

ZIP file of APKs:

android {
    ...
    publishing {
        // Publish your app as a ZIP file of APKs
        singleVariant("fullRelease") {
            publishApk()
        }
    }
}

afterEvaluate {
    publishing {
        publications {
            fullRelease(MavenPublication) {
                from components.fullRelease
                // ......
            }
        }
    }
}
Caespitose answered 22/4 at 23:30 Comment(0)
P
-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

Porphyria answered 12/9, 2022 at 13:30 Comment(2)
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.Episodic
See the question, this only enables the AGP 8.0 default behavior.Chlorous
P
-4

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

android.disableAutomaticComponentCreation=true 
Partin answered 4/1, 2023 at 11:50 Comment(1)
See the question, this only enables the AGP 8.0 default behavior.Chlorous

© 2022 - 2024 — McMap. All rights reserved.