22

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?

Rahul
  • 3,293
  • 2
  • 31
  • 43
Swapnil
  • 1,870
  • 2
  • 23
  • 48

9 Answers9

7

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)
    }
}
Balwinder SIngh
  • 1,831
  • 2
  • 24
  • 28
  • 1
    This seems to be an important point, as several dependencies I was working with made the assumption of availability of these options. Cheers! – TahoeWolverine Apr 25 '22 at 22:46
6

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
            }
        }
    }
}
Community
  • 1
  • 1
Daniel Wilson
  • 18,838
  • 12
  • 85
  • 135
5

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  .....
            }
        }
     }
   }
}
Fayçal
  • 1,150
  • 1
  • 12
  • 19
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)

netikras
  • 422
  • 4
  • 12
1

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

Kakata Kyun
  • 573
  • 1
  • 5
  • 12
1

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

                //...
            }
        }
    }
}
Homayoon Ahmadi
  • 1,181
  • 1
  • 12
  • 24
-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://stackoverflow.com/a/60936807/971355

ka3ak
  • 2,435
  • 2
  • 30
  • 57
-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 {
        ...
}

Liuting
  • 1,098
  • 17
  • 35
-2

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  .....
                }
            }
        }
    }
}
Geert
  • 3,527
  • 1
  • 20
  • 16
xuan yu
  • 1
  • 1
  • 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](/help/how-to-answer). – Community Oct 21 '21 at 09:19
  • 1
    You copy pasted my answer – Fayçal Oct 24 '21 at 18:28
  • @Fayçal Typo and everything – Ruslan May 18 '23 at 19:13