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

12

31

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?

Pointenoire answered 25/4, 2021 at 12:57 Comment(3)
are you able to resolve this issue ?Hargrave
Please any update?Sty
I posted a solutionSty
U
17

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

                //...
            }
        }
    }
}
Uncovenanted answered 27/8, 2023 at 7:45 Comment(1)
This worked for me, thanks. Using Gradle 8.0 in Android Studio Flamingo (2022.2.1 Patch 2)Unpile
G
8

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)
    }
}
Gurule answered 31/12, 2021 at 13:45 Comment(1)
This seems to be an important point, as several dependencies I was working with made the assumption of availability of these options. Cheers!Brave
P
7

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
            }
        }
    }
}
Porty answered 7/10, 2021 at 15:41 Comment(2)
Which Gradle file does this go in? Where in the file should it be added?Beshore
it goes in your module level build.gradle filePorty
C
6

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  .....
            }
        }
     }
   }
}
Chaotic answered 21/7, 2021 at 14:0 Comment(5)
This didn't work. It just changes the line at which it complains about the failure down one. to the new line for "from components.release"Flake
@Flake can you show me your code?Sty
gist.github.com/william-ferguson-au/…Flake
@Flake gist.github.com/BackPackerDz/918e6d47387c86fe6b00bd2fec7f6220 did you try it like that?Sty
Yes - same error just the line numbers are shunted down one.Flake
M
5

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)

Marrin answered 19/11, 2021 at 21:28 Comment(0)
B
3

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://mcmap.net/q/449691/-software-components-will-not-be-created-automatically-for-maven-publishing-from-android-gradle-plugin-8-0

Add:

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

Do this for all modules which are to be published.

Brink answered 20/9, 2023 at 10:26 Comment(0)
F
2

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

Flown answered 7/3, 2022 at 4:20 Comment(0)
A
1

I resolved this by updating Android Studio to Jellyfish version.

In this case I am using Expo SDK 51

Arsine answered 2/6 at 14:5 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewWaterhouse
P
0

I looked up many answers, but none of them worked. The

println("Components: " + components*.name)

above gave me the inspiration.and I finally solved the problem. Both of the following methods are effective:

android {
    ……
    publishing {
        singleVariant("release") //then,print->Components: [release]
    }
}

Another:

apply plugin: 'kotlin-android' //this really works.print [debug, release], beacause my 'buildType' has debug and release
Poleax answered 17/6 at 9:27 Comment(0)
A
-2

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://mcmap.net/q/401063/-manually-adding-aar-with-dependency-pom-iml-file

Aegisthus answered 13/6, 2021 at 12:19 Comment(0)
L
-2

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

Laughing answered 31/8, 2021 at 15:25 Comment(0)
K
-3

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  .....
                }
            }
        }
    }
}
Kazim answered 21/10, 2021 at 8:33 Comment(3)
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.Shirleenshirlene
You copy pasted my answerSty
@Chaotic Typo and everything 😂Pinole

© 2022 - 2024 — McMap. All rights reserved.