gradleandroid-gradle-pluginmaven-plugingradle-kotlin-dslbintray

SoftwareComponentInternal with name 'java' not found


App module build.gradle

apply plugin: 'com.android.library'
apply from: rootProject.file('deploy-bintray.gradle.kts')
android {...}

deploy-bintray.gradle.kts it's my bintray/maven publications script.

I'm having problems generating .jar files:

val sourcesJar by tasks.registering(Jar::class) {
    archiveClassifier.set("sources")
    from(project.the<SourceSetContainer>()["main"].allSource)
}

publications {
        create<MavenPublication>(bintrayRepo) {
            groupId = publishedGroupId
            artifactId = artifact
            version = libraryVersion

            from(components["java"])
            artifact(sourcesJar.get())
            artifact(dokkaJar.get())
            ...
            }
        }
    }

it fails with:

SoftwareComponentInternal with name 'java' not found.

or, if I comment from(components["java"]) it fails with:

SourceSet with name 'main' not found.

If I add java plugin:

The 'java' plugin has been applied, but it is not compatible with the Android plugins.

So I'm stuck here. How can I solve this?


Solution

  • I finally found a solution!
    I was doing a few things wrong, first, both dokkaJar and sourceJar tasks have to be in the main build.gradle and not inside deploy-bintray.gradle.kts. Moving them made it work and fixes:

    SourceSet with name 'main' not found.

    Secondly we cannot use from(components["java"]) because this is an Android lib so I've replaced that line with artifact("$buildDir/outputs/aar/${artifactId}-release.aar").

    Last but not least, as stated here (step 7):

    "Also, the POM file generated does not include the dependency chain so it must be explicitly added..."

    I had to add this:

    pom {
    ...
        withXml {
           val dependenciesNode = asNode().appendNode("dependencies")
           configurations.getByName("implementation") {
             dependencies.forEach {
                val dependencyNode = dependenciesNode.appendNode("dependency")
                dependencyNode.appendNode("groupId", it.group)
                dependencyNode.appendNode("artifactId", it.name)
                dependencyNode.appendNode("version", it.version)
             }
           }     
        }       
    }