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

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?



Solution 1:[1]

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
            }
        }
    }

Solution 2:[2]

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)

Solution 3:[3]

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  .....
            }
        }
     }
   }
}

Solution 4:[4]

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)
    }
}

Solution 5:[5]

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

Solution 6:[6]

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  .....
                }
            }
        }
    }
}

Solution 7:[7]

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

Solution 8:[8]

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 {
        ...
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Wilson
Solution 2 netikras
Solution 3 Fayçal
Solution 4 Balwinder SIngh
Solution 5 Kakata Kyun
Solution 6 Geert
Solution 7 ka3ak
Solution 8 Liuting