You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin Multiplatform库在IntelliJ IDEA中无法识别的问题求助

问题描述

我在Kotlin Multiplatform(JS/JVM)项目中使用better-parse库时,遇到IntelliJ IDEA无法在所有模块中识别该库的问题,已准备示例项目用于复现问题。

项目的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 1.6.20的纯Kotlin/JVM项目中,该库可以正常工作,并且能显示在外部库列表中(配图:已导入的better-parse)。

已尝试的解决方法

  • 用api替换implementation声明依赖
  • 将依赖移至测试模块
  • 将依赖移至根目录的dependencies块中,写法如下:
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")
    }
}
...

内容的提问来源于stack exchange,提问作者waterstopper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 14:07:39