![](/img/trans.png)
[英]Apply plugin from rootproject file in kotlin dsl gradle file
[英]Apply Kotlin multiplatform plugin via gradle legacy plugin application
我正在對 Kotlin Multiplatform Mobile 進行一些深入的實踐,我不得不使用傳統的應用插件方式來應用 Gradle 插件。
我正在為 Gradle 使用 Kotlin DSL,但我沒有設法包含 kotlin-multiplatform 插件。
本質上,有兩種方法可以在您的項目中包含 gradle 插件:
我基本上創建了一個非常空白的 gradle 項目( gradle init
),與任何 IDE 或任何其他依賴項無關,這兩者對於 KMM 項目都是常見的 - 盡可能地隔離問題。
當通過./gradlew clean build
(via Plugins DSL) 運行時,下面的build.gradle.kts工作得很好
plugins {
id("org.jetbrains.kotlin.multiplatform") version "1.4.10"
}
kotlin {
jvm()
}
repositories {
jcenter()
}
然而,這不會(通過傳統的插件應用程序)工作:
buildscript {
repositories {
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10")
}
}
apply(plugin = "org.jetbrains.kotlin.multiplatform")
kotlin {
jvm()
}
repositories {
jcenter()
}
它失敗並出現此錯誤:
* What went wrong:
Script compilation errors:
Line 12: kotlin {
^ 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
Line 13: jvm()
^ Unresolved reference: jvm
2 errors
它無法解析本質上是 KMM 項目入口點的kotlin {}
塊。
有趣的是,在 Gradle 中使用 Groovy 而不是 Kotlin - 在這兩種情況下都有效。
但是我想將 Kotlin DSL 用於 Gradle 並通過傳統方式應用插件,因為這樣我可以動態應用插件,即在某些條件下。
通常,您可以在插件 DSL 中使用apply false
並有條件地調用apply
。 單獨Apply
永遠不會與 Kotlin DSL 一起工作,它只能與動態 Groovy 一起工作。 如果您還使用插件 DSL + apply false
,Kotlin 可以像 Groovy 一樣工作。 例子:
plugins {
kotlin("multiplatform") apply false // Applied conditionally later
}
有關該問題的更詳細討論,請查看此。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.