Kotlin multiplatform JVM type mismatch in IntelliJ

2019-07-31 08:52发布

问题:

I have a project which contains the following modules:

  • common
  • common-js
  • common-jvm
  • backend
  • web

The idea is that I put my shared (kotlin) code (mainly models) in common, which can be used in both the kotlin driven backend and the javascript driven webapp using the recently introduced multiplatform support. To do this, I created a new multiplatform application in Intellij, and added the backend and web modules to it.

An example class in the common module:

data class Show(
    val id: Long,
    val type: ShowType,
    val title: String,
    var description: String? = null
)

The backend is a simple spring boot application. It has a dependency on the common-jvm project:

compile project(":common-jvm")

Now when I run gradle bootRun, the application starts and works fine. However, when I try to run the same application using IntelliJ, I get errors like:

Error:(68, 26) Kotlin: Type mismatch: inferred type is kotlin.Long but java.lang.Long was expected
Error:(68, 65) Kotlin: Type mismatch: inferred type is kotlin.String! but java.lang.String was expected

On this line, I try to create an instance of the Show model using data from a json source:

val movie = Show(json.get(id).asLong(), ShowType.MOVIE, json.get(movieTitle).asText())

I am using Kotlin 1.2.10 and spring boot 1.5.9.RELEASE.

Anybody who knows what causes this and how it can be solved? I tried putting -Xmulti-platform in the additional command line parameters in the project structures for the backend module, but this did not work either.

回答1:

You need to delegate build/run actions to Gradle, since building multiplatform projects with IDEA is not supported at this moment.