https://kotlinlang.org logo
#gradle
Title
# gradle
m

Matheus Finatti

01/20/2023, 10:57 AM
I’m having some weird issue when trying to add a gradle plugin to a convention plugin I have a
build-logic
module with a bunch of convention plugins In
build.gradle.kts
I tried adding a gradle plugin like such:
Copy code
plugins {
    `kotlin-dsl`
    `kotlin-dsl-precompiled-script-plugins`
}

group = "buildlogic"

repositories {
    mavenCentral()
    google()
    maven ("<https://s01.oss.sonatype.org/content/repositories/releases/>")
}

java {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
}

dependencies {
    implementation(libs.gradlePlugin.android)
    implementation(libs.gradlePlugin.kotlin)
    implementation(libs.detekt)
    implementation(libs.kmm.spm) // This one is throwing an exception
}
My version catalog looks like this:
Copy code
[versions]
detekt = "1.22.0"
kmm-spm = "2.1.1"

[libraries]
detekt = { module = "io.gitlab.arturbosch.detekt:detekt-gradle-plugin", version.ref = "detekt" }
kmm-spm = { group = "io.github.luca992.multiplatform-swiftpackage", name = "io.github.luca992.multiplatform-swiftpackage.gradle.plugin", version.ref = "kmm-spm" }
Detekt works fine, but
kmm-spm
does not. I get an exception when syncing:
Copy code
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':build-logic:generateExternalPluginSpecBuilders'.
	[...]
Caused by: java.lang.NullPointerException: implementationClass must not be null
	at org.gradle.kotlin.dsl.codegen.PluginIdExtensionsKt$pluginEntriesFrom$1$2.invoke(PluginIdExtensions.kt:105)
	at org.gradle.kotlin.dsl.codegen.PluginIdExtensionsKt$pluginEntriesFrom$1$2.invoke(PluginIdExtensions.kt)
Could this be an issue with Gradle, or with the plugin I’m trying to import? plugin in question: https://github.com/luca992/multiplatform-swiftpackage Thanks in advance!
a

Adam S

01/20/2023, 11:04 AM
Copy code
kmm-spm = { group = "io.github.luca992.multiplatform-swiftpackage", name = "io.github.luca992.multiplatform-swiftpackage.gradle.plugin", version.ref = "kmm-spm"
this looks like a mistake, I guess this Maven coordinates of the the Gradle plugin marker, not the actual plugin implementation
m

Matheus Finatti

01/20/2023, 11:05 AM
This is the only way I could find that would make gradle actually download the plugin. I’ve checked my
.gradle/caches
and all the contents are there
a

Adam S

01/20/2023, 11:06 AM
hmm yes, you’re right. I can’t see the plugin marker in Maven, and the error doesn’t match what happens when the wrong coordinates are used https://stackoverflow.com/q/74817692/4161471
do you know if the plugin is published to the Gradle Plugin Portal?
m

Matheus Finatti

01/20/2023, 11:07 AM
No, it’s published here:
Copy code
maven ("<https://s01.oss.sonatype.org/content/repositories/releases/>")
a

Adam S

01/20/2023, 11:09 AM
I think that’s the Maven Central URL, so while it’s completely unrelated to your problem, you could remove the URL from the repos
Copy code
repositories {
    mavenCentral()
    google()
//    maven("<https://s01.oss.sonatype.org/content/repositories/releases/>")
}
hmmm, very weird. Looking at the plugin code it’s using
afterEvaluate {}
which could cause problems, and it requires that the Kotlin Multiplatform plugin is applied rather than reacting to it. What are the contents of your convention plugin where you’re applying the plugin? What’s the full stacktrace? Are there any
com.chromaticnoise
lines in there?
m

Matheus Finatti

01/20/2023, 11:16 AM
Copy code
mport buildlogic.Namespace
import buildlogic.Versions
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework

plugins {
    id("kotlin-multiplatform")
    id("com.android.library")
}

group = "mp"

kotlin {
    android()
    val frameworkName = name
    val xcf = XCFramework(frameworkName)
    listOf(
        iosArm64(),
        iosSimulatorArm64(),
    ).forEach { target ->
        target.binaries.framework {
            baseName = frameworkName
            xcf.add(this)
        }
    }

    sourceSets {
        val commonMain by getting
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        val androidMain by getting
        val androidTest by getting
        val iosArm64Main by getting
        val iosSimulatorArm64Main by getting
        val iosMain by creating {
            dependsOn(commonMain)
            iosArm64Main.dependsOn(this)
            iosSimulatorArm64Main.dependsOn(this)
        }
        val iosArm64Test by getting
        val iosSimulatorArm64Test by getting
        val iosTest by creating {
            dependsOn(commonTest)
            iosArm64Test.dependsOn(this)
            iosSimulatorArm64Test.dependsOn(this)
        }
    }
}

android {
    namespace = "${Namespace.shared}.core"
    compileSdk = Versions.compileSdk
    defaultConfig {
        minSdk = Versions.minSdk
    }
}
This is the convention plugin, I haven’t added
multiplatform-swiftpackage
yet, because it fails before
v

Vampire

01/20/2023, 11:16 AM
It's simply that the
io.github.luca992.multiplatform-swiftpackage
plugin is broken.
If you download the jar and look into
META-INF\gradle-plugins\com.chromaticnoise.multiplatform-swiftpackage.properties
you find a property
implementationClass
but not a property
implementation-class
which should be there.
m

Matheus Finatti

01/20/2023, 11:19 AM
Nice one, so just replacing it to
implementation-class
would fix the issue?
Will it still work when adding to a
plugins{}
block in a regular
build.gradle.kts
?
v

Vampire

01/20/2023, 11:21 AM
I don't know when this code path is hit, so no idea. But yeah, fixing the properties file should probably fix the problem.
Btw.
kotlin-dsl
already applies
kotlin-dsl-precompiled-script-plugins
internally, there is no need to apply it explicitly.
a

Adam S

01/20/2023, 11:21 AM
Gradle Kotlin DSL will do some code gen to generate accessors for any plugins that it finds in buildSrc, or equivalent, that’s why the exception is in
org.gradle.kotlin.dsl.codegen.PluginIdExtensionsKt
. So given that the plugin is released at all, yeah, I think it should work in normal
plugins {}
blocks.
m

Matheus Finatti

01/20/2023, 11:22 AM
Awesome, thanks a lot
a

Adam S

01/20/2023, 11:23 AM
I’d say this is an edgecase that Gradle could handle better. It would be more helpful if Gradle didn’t error out, and logged a message instead.
to actually fix the issue, the plugin author could 1. remove
src/main/resources/META-INF/gradle-plugins/com.chromaticnoise.multiplatform-swiftpackage.properties
2. change the publishing coordinates of the plugin to be different to the plugin marker ID then the
gradlePlugin {}
config will make the plugin ID work automatically https://github.com/ge-org/multiplatform-swiftpackage/blob/8d7c25ecc19841d4af515db885ae3426f7f32274/build.gradle.kts#L52-L59
m

Matheus Finatti

01/20/2023, 11:27 AM
I will pass it forward. Thanks for the support!
56 Views