Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 and 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?

like image 869
Martin Zeitler Avatar asked Aug 31 '25 15:08

Martin Zeitler


2 Answers

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.

like image 104
Martin Zeitler Avatar answered Sep 07 '25 17:09

Martin Zeitler


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'
            }
        }
    }
}
like image 22
Peter Avatar answered Sep 07 '25 17:09

Peter