简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM