Multi platform project mixed with Java code


#1

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?


#2

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


#3

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.


#4

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


#5

@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 https://github.com/fatjoem/multiplatformdemo . 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();

#6

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.