IntelliJ support for multi platform projects

I managed to set up a minimal kotlin multi platform project. It works fine except for one use case. I am not sure whether this is a limitation of kotlin multi platform projects, or whether I am doing something wrong.

What works fine is:

  • Kotlin code within my JVM module can access and execute Kotlin code within my common module.
  • Java code within my JVM module can access and execute Kotlin code within my JVM module.

What does not work is:

  • Java code within my JVM module cannot directly access Kotlin code within my common module.

In the last case I will get error messages like “java.lang.String is not compatible with java.lang.String”.

Is this a known limitation or is it supposed to work?

Update: I now know that this is an IntelliJ issue. Compiling with Gradle will work without problems. It is just the IDE that shows compiling errors.

I haven’t tried exactly what you describe, but I did run into similar type errors in a multiplatform project. It ended up being that I was either applying kotlin jvm plugins to the common module or using kotlin jvm dependencies in the common module. Id suggest double checking your build.gradles that the common module only uses common plugins/dependencies and the jvm module only uses jvm

I had issues with this when I had Kotlin and Java code separated into separate folders (/src/main/java and /src/main/kotlin). When I put all the code in the /java folder, I could do kotlin->java and java->kotlin and kotlin->java->kotlin all fine.

@fatjoe79 Could you share build.gradle file of your JVM module or better the entire project?

@ilya.gorbunov meanwhile I realized that my setup compiles and runs successfully with gradle. The problem I described is just a compilation error shown by Intellij.

Anyway, I created a minimal git repository under GitHub - fatjoem/multiplatformdemo: A minimal app demonstrating kotlin multi platform code sharing capabilities. . Note: it is a flat-structured multi-project. The main build.gradle file is located under multiplatformdemo/multiplatformdemo/build.gradle.

Intellij will show an error for the following line of code:

// intellij shows an error for the following line:
// (though it compiles and runs successfully with gradle)
final String string = fancyText.toString();

Thanks for the repro.
I did reproduce the problem with 1.2.31 Kotlin plugin, but it goes away with the 1.2.40 plugin, so it seems to be fixed in that newer version.

@ilya.gorbunov I can confirm that this particular error seems to be fixed with Kotlin plugin 1.2.40. However there is still a problem with IntelliJ behaving incorrectly in another case.

I have updated my repo under GitHub - fatjoem/multiplatformdemo: A minimal app demonstrating kotlin multi platform code sharing capabilities. to show the new problem (note: it is a flat-structured multi-project; the main build.gradle file is located under multiplatformdemo/multiplatformdemo/build.gradle).

The problem will only occur when activating ‘Build project automatically’ in IntelliJ settings. When activated, IntelliJ’s ‘Problems’ tool window will complain for the file FancyText.kt :

/*
 * When enabling IntelliJ File | Settings | Build, Execution, Deployment | Compiler | Build project automatically,
 * then IntelliJ will complain with 'Error:(9, 35) Kotlin: Unresolved reference: String' in its 'Problems' tool window.
 *
 * However this file compiles successfully with gradle.
 */
class FancyText(private val text: String) {
  override fun toString() = "Fancy: $text"
}