Sir Codesalot
Sir Codesalot

Reputation: 7283

Can I configure a Kotlin Multiplatform module in a Gradle plugin?

I'm building a project with several Kotlin Multiplatform modules. As the Gradle documentation suggests, to share configuration between those modules I've created a custom plugin. This plugin is supposed to apply the kotlin-multiplatform plugin and the shared configuration but unfortunately it's unable to resolve the kotlin extension for applying the multiplatform configuration.

My plugin (buildSrc\src\main\kotlin\my.pugin.gradle.kts):

plugins {
    kotlin("multiplatform")
}

kotlin {
    val hostOs = System.getProperty("os.name")
    val isMingwX64 = hostOs.startsWith("Windows")
    val nativeTarget = when {
        hostOs == "Mac OS X" -> macosX64("native")
        hostOs == "Linux" -> linuxX64("native")
        isMingwX64 -> mingwX64("native")
        else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
    }

    sourceSets {
        val commonMain by getting
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
    }

    jvm {
        val main by compilations.getting {
            kotlinOptions {
                jvmTarget = "1.8"
            }
        }
    }
}

The error:

Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: public fun DependencyHandler.kotlin(module: String, version: String? = ...): Any defined in org.gradle.kotlin.dsl public fun PluginDependenciesSpec.kotlin(module: String): PluginDependencySpec defined in org.gradle.kotlin.dsl

Upvotes: 2

Views: 1982

Answers (1)

Sir Codesalot
Sir Codesalot

Reputation: 7283

Apparently, what you need is to add a dependency of the buildSrc on the kotlin-gradle-plugin in buildSrc\build.gradle.kts:

dependencies {
    //...
    implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
    //...
}

EDIT: $kotlin_version is unavailable at this scope, you need to specify it explicitly.

Upvotes: 4

Related Questions