我正在尝试在 Kotlin 多平台(JS/JVM)项目中使用 better-parse 库,但它在任何模块中都不可见。 示例项目链接。
这是
build.gradle.kts
:
plugins {
kotlin("multiplatform") version "1.6.20"
}
group = "llesha"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
mavenLocal()
}
kotlin {
jvm {
compilations.all {
kotlinOptions.jvmTarget = "1.8"
}
testRuns["test"].executionTask.configure {
useJUnit()
}
}
js(IR) {
browser()
}
sourceSets {
val commonMain by getting {
dependencies {
implementation("com.github.h0tk3y.betterParse:better-parse:0.4.4")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
val jvmMain by getting
val jvmTest by getting
val jsMain by getting
val jsTest by getting
}
}
我不认为该库是问题所在,因为它可以在 Kotlin/JVM 项目中使用 Kotlin 1.6.20 运行。 它还显示在外部库中,就像导入一样:
我尝试使用
api
而不是 implementation
,将依赖项移至测试模块,将依赖项移至根级别,如下所示:
dependencies {
"commonMainImplementation"("com.github.h0tk3y.betterParse:better-parse:0.4.4")
}
但这些都不起作用。
我还注意到,如果将库添加到 jvm 模块中,它就可以工作。但在 js 模块中却没有。这是修改后的
sourceSets
脚本:
...
val jvmMain by getting {
dependencies {
implementation("com.github.h0tk3y.betterParse:better-parse-jvm:0.4.4")
}
}
val jvmTest by getting
val jsMain by getting {
dependencies {
implementation("com.github.h0tk3y.betterParse:better-parse-js:0.4.4")
}
}
...
通过将 kotlin 版本更改为
kotlin("multiplatform") version "1.9.22"
我成功了!