Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix conflicting Kotlin dependency in IntelliJ IDEA?

We're creating a multi-platform project in Kotlin and part of a certain module uses the experimental coroutines feature.

We're using Gradle to build the project/library together. The common module's gradle build script looks like this:

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"
    }
}

really nothing out of the ordinary here. There however seems to be a problem with conflicting versions of Kotlin's standard library upon which coroutines depend and Kotlin's standard library we want to use in the project.

Besides other information, the gradle module:dependencies outputs a tree with this information:

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)

As you can see, the project depends on the Kotlin's version 1.2.41, but the coroutines library internally depends on 1.2.21.

The problem this causes is that when I use certain constructs from the Kotlin language, IntelliJ does not recognise it and shows an Unresolved reference error:

Unresolved reference error, IntelliJ

This grows to be quite annoying, because pretty much all files are marked by IntelliJ as if they contained a fatal error, even though you can build them without a problem.

Upon inspecting the modules dependencies, I have found out that the IntelliJ really thinks the module depends on two Kotlin standard libraries:

Module dependencies in IntelliJ

I have found out that if I remove the kotlin-stdlib-1.2.21 dependency from this list, IntelliJ will then stop showing the Unresolved reference error. Unfortunately, upon re-syncing the project using Gradle, the dependency is back.

Is there a way how to circumvent this issue?

like image 747
Andrew Avatar asked May 11 '18 14:05

Andrew


People also ask

How do I change dependencies in IntelliJ?

From the main menu, select File | Project Structure Ctrl+Alt+Shift+S , and go to Modules | Dependencies. In the next dialog, click Edit, and then click Configure next to the Include transitive dependencies option. Select the dependencies you want to include in the library and click OK.


1 Answers

As mentioned in https://discuss.gradle.org/t/how-do-i-exclude-specific-transitive-dependencies-of-something-i-depend-on/17991, you can exclude a specific dependency by using this code:

compile('com.example.m:m:1.0') {
   exclude group: 'org.unwanted', module: 'x'
}
compile 'com.example.l:1.0'

This would exclude the module x from the dependencies of m, but will still bring it if you depend on l.

In your case, simply

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

Will prevent gradle from loading kotlin-stdlib-common depended by kotlinx-coroutines as a dependency, but still add the kotlin-stdlib-common you specified.

like image 196
ice1000 Avatar answered Oct 01 '22 13:10

ice1000