Could not get unknown property 'release' for SoftwareComponentInternal - Maven publish plugin project gradle

25.1k Views Asked by At

I have an Android project with multiple modules and I want to publish them to self-hosted maven repo. I earlier had the publishing code present in individual modules and things worked just fine. I am now trying to move the publishing code into the project build.gradle so that I can reuse the code. The code inside my individual modules was:

afterEvaluate {
        // To avoid publishing the applications inside the project...
        if (!plugins.hasPlugin("android")) {
            publishing {
                publications {
                    mavenAar(MavenPublication) {
                        artifactId "$project.name"
                        from components.release
                    }
                }
                repositories {
                    .... My repo details and credentials .......
                }
            }
        }
    }

and things worked just fine. When I moved the code to the project build.gradle, like this:

subprojects {
    apply plugin: 'maven-publish'

    afterEvaluate {
        // To avoid publishing of the applications inside the project ..
        if (!plugins.hasPlugin("android")) {
            publishing {
                publications {
                    mavenAar(MavenPublication) {
                        artifactId "$project.name"
                        from components.release
                    }
                }
                repositories {
                    .... My reop details and creddentials  .....
                }
            }
        }
    }
}

I started getting the following error when running the publish task:

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

Can someone point out the problem here?

10

There are 10 best solutions below

0
On

Make sure that you are using com.android.tools.build:gradle:4.1.2 or above version 3.6 in both root/build.gradle and root/<library_name>/build.gradle. Then make sure both plugins were declared in root/<library_name>/build.gradle like this:

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'

then you can use components.release

You can read this docs https://developer.android.com/studio/build/maven-publish-plugin#groovy for more information

2
On

I came across this after updating AGP for Jitpack. Both the MavenPublication block and the components.release line appear to use your build variant name. So if you have a standard debug and release variant it would look like this:

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release
                groupId = 'com.my.group'
                artifactId = 'id'
                version = Config.libraryVersion
            }
        }
    }
}

But if you have a build variant with a name other than release, you need to use that:

afterEvaluate {
    publishing {
        publications {
            productionRelease(MavenPublication) {
                from components.productionRelease
                groupId = 'com.my.group'
                artifactId = 'id'
                version = Config.libraryVersion
            }
        }
    }
}
0
On

Ran into a similar error and resolved by applying the android plugin before afterEvaluate, so something looks like this:


apply plugin: 'com.android.library'

afterEvaluate {
    publishing {
        publications {
        ...
}

0
On

I ran against the same error. Solved it with the code below:

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                groupId = 'mygroup'
                artifactId = 'common'
                version = '1.0.0'
                artifact(bundleReleaseAar)
            }
        }
    }
}

It seems that you have to specify the exact path to the AAR file. You can find more details in this answer: https://stackoverflow.com/a/60936807/971355

5
On

This worked for me, I added another afterEvaluate.

subprojects {
  apply plugin: 'maven-publish'

  afterEvaluate {
    // To avoid publishing of the applications inside the project ..
    if (!plugins.hasPlugin("android")) {
        publishing {
            publications {
                mavenAar(MavenPublication) {
                    afterEvaluate {
                       artifactId "$project.name"
                       from components.release
                    }
                }
            }
            repositories {
                .... My reop details and creddentials  .....
            }
        }
     }
   }
}
1
On

check your project level build.gradle and make sure gradle classpath is

classpath "com.android.tools.build:gradle:4.1.2" or above.

This worked for me.

As mentioned by others, you can also check component names by :

task comps {
    afterEvaluate {
        println("Components: " + components*.name)
    }
}
1
On

I solved my problem using this line:

from components.findByName('release')

here you should put it:

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.findByName('release')

                //...
            }
        }
    }
}
0
On

When using Gradle Plugin 7.x there was a warning showing up:

Software Components are 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.

When migrating an Android library project to Gradle Plugin 8+ follow these steps: https://stackoverflow.com/a/71366104/2115352

Add:

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

Do this for all modules which are to be published.

3
On

I got same error, Solved it with the code below.

The actual change is to wrap the from components.release line in an additional afterEvaluate block.

subprojects {
    apply plugin: 'maven-publish'

    afterEvaluate {
        // To avoid publishing of the applications inside the project ..
        if (!plugins.hasPlugin("android")) {
            publishing {
                publications {
                    mavenAar(MavenPublication) {
                        artifactId "$project.name"
                        //add afterEvaluate block
                        afterEvaluate {
                            from components.release
                        }
                    }
                }
                repositories {
                    .... My reop details and creddentials  .....
                }
            }
        }
    }
}
0
On

Neither of the answers worked for me. And I wouldn't be myself if I didn't try to reverse-engineer it rather than read the docs :)

Turns out, this components.release works fine when we're building a com.android.library, but not so well when building with the com.android.application plugin. If gradle says it doesn't have a release component, then let's see what components DOES it have:

task comps {
    afterEvaluate {
        println("Components: " + components*.name)
    }
}

doing a ./gradlew :app:comps prints the following:

Components: [debug_aab, debug_apk, release_aab, release_apk]

If in the android.buildTypes {} closure I defined another type, say, freebie, ./gradlew :app:comps will print the following:

Components: [debug_aab, debug_apk, release_aab, release_apk, freebie_aab, freebie_apk]

It appears that the gradle plugin takes your build variant's name and creates two variants out of it: ${variant}_apk and ${variant}_aab.

Now to solve the initial problem, replace

publications {
    mavenAar(MavenPublication) {
        artifactId "$project.name"
        from components.release
    }
}

with

publications {
    mavenAar(MavenPublication) {
        artifactId "$project.name"
        from components.release_apk
    }
}

or use the _aab one. If you're using another variant than release, replace the from components.release_apk part accordingly.

Gradle: 7.3

AndroidStudio: 2021.2.1 Canary 4 (Chipmunk)