Gradle 如何修复IntelliJ IDEA中冲突的Kotlin依赖关系?

Gradle 如何修复IntelliJ IDEA中冲突的Kotlin依赖关系?,gradle,kotlin,dependencies,Gradle,Kotlin,Dependencies,我们正在Kotlin创建一个多平台项目,某个模块的一部分使用了实验功能 我们正在使用Gradle一起构建项目/库。公共模块的gradle构建脚本如下所示: apply plugin: 'kotlin-platform-common' dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version" implementation "org.jetbrains.kotl

我们正在Kotlin创建一个多平台项目,某个模块的一部分使用了实验功能

我们正在使用Gradle一起构建项目/库。公共模块的gradle构建脚本如下所示:

apply plugin: 'kotlin-platform-common'

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.5"
    testCompile "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
    testCompile "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
}
kotlin {
    experimental {
        coroutines "enable"
    }
}
这里真的没什么不寻常的。然而,协同程序所依赖的Kotlin标准库版本与我们希望在项目中使用的Kotlin标准库版本之间似乎存在冲突

除其他信息外,
gradle模块:依赖项
还输出一个包含以下信息的树:

compileClasspath - Compile classpath for source set 'main'.
+--- org.jetbrains.kotlin:kotlin-stdlib-common:1.2.41
\--- org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.5
     \--- org.jetbrains.kotlin:kotlin-stdlib:1.2.21
          \--- org.jetbrains:annotations:13.0

compileOnly - Compile only dependencies for source set 'main'.
No dependencies

default - Configuration for default artifacts.
+--- org.jetbrains.kotlin:kotlin-stdlib-common:1.2.41
\--- org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.5
     \--- org.jetbrains.kotlin:kotlin-stdlib:1.2.21
          \--- org.jetbrains:annotations:13.0

implementation - Implementation only dependencies for source set 'main'. (n)
+--- org.jetbrains.kotlin:kotlin-stdlib-common:1.2.41 (n)
\--- org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.5 (n)
如您所见,该项目依赖于Kotlin的版本1.2.41,但协同程序库内部依赖于1.2.21

这导致的问题是,当我使用Kotlin语言的某些构造时,IntelliJ无法识别它,并显示未解决的引用错误:

这变得非常烦人,因为几乎所有文件都被IntelliJ标记为包含致命错误,即使您可以毫无问题地构建它们

在检查模块依赖关系后,我发现IntelliJ确实认为模块依赖于两个Kotlin标准库:

我发现,如果我从此列表中删除
kotlin-stdlib-1.2.21
依赖项,IntelliJ将停止显示未解决的引用错误。不幸的是,在使用Gradle重新同步项目时,依赖关系又回来了

有没有办法避免此问题?

如中所述,您可以使用以下代码排除特定的依赖项:

compile('com.example.m:m:1.0') {
   exclude group: 'org.unwanted', module: 'x'
}
compile 'com.example.l:1.0'
这会将模块
x
m
的依赖项中排除,但如果您依赖
l
,则仍然会带来模块
x

在你的情况下,简单地说

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.5") {
  exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-common'
}

将阻止gradle加载
kotlin stdlib common
依赖于
kotlinx协同程序
作为依赖项,但仍然添加您指定的
kotlin stdlib common

绝对精彩!非常感谢你。这很有帮助。很遗憾,我不能对你的答案投赞成票,对此我深表歉意。这并不重要,我很乐意帮助你:)