我们正在 Kotlin 中创建一个多平台项目,并且某个模块的一部分使用了实验性 coroutines特征。
我们正在使用 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 module:dependencies
输出具有以下信息的树: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 无法识别它并显示 Unresolved 引用错误:
Unresolved reference error, IntelliJ
这变得非常烦人,因为几乎所有文件都被 IntelliJ 标记为好像它们包含 fatal error ,即使您可以毫无问题地构建它们。
在检查模块依赖项后,我发现 IntelliJ 确实认为该模块依赖于两个 Kotlin 标准库:
Module dependencies in IntelliJ
我发现如果我删除
kotlin-stdlib-1.2.21
从此列表中的依赖项,IntelliJ 将停止显示 Unresolved 引用错误。不幸的是,在使用 Gradle 重新同步项目后,依赖关系又回来了。有没有办法绕过这个问题?
最佳答案
如 https://discuss.gradle.org/t/how-do-i-exclude-specific-transitive-dependencies-of-something-i-depend-on/17991 中所述,您可以使用以下代码排除特定依赖项:
compile('com.example.m:m:1.0') {
exclude group: 'org.unwanted', module: 'x'
}
compile 'com.example.l:1.0'
这将排除模块
x
来自 m
的依赖项, 但如果你依赖 l
仍然会带来它.在您的情况下,只需
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.5") {
exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-common'
}
将阻止 gradle 加载
kotlin-stdlib-common
取决于 kotlinx-coroutines
作为依赖项,但仍添加 kotlin-stdlib-common
你指定的。
关于gradle - 如何修复 IntelliJ IDEA 中冲突的 Kotlin 依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50294062/