Daniel Macak
Daniel Macak

Reputation: 17103

Idea keeps switching compile target from 1.8 to 1.6 for Kotlin

I am working on a new Kotlin project based on Maven and JDK 8 using Intellij Idea. Everything is great except after each Maven reimport the compile target switches from 1.8 to 1.6 for some reason.

I literally tried everything from the 2 best answers to the same problem (though from Java world) but nothing worked for me. I also migrated to Gradle with hope of overcoming the problem but that didn't help either. Apart from the mentioned SO question I went over a ton of forums and the advice was always the same as in the mentioned SO answers.

So the question is, how can I prevent Idea from switching the target Java version after reimport?

Upvotes: 10

Views: 2879

Answers (2)

AndreyKo
AndreyKo

Reputation: 1

Solved with:

  compileKotlin {
    kotlinOptions.jvmTarget = 1.8
  }

If this does not work for you, run the 'compileKotlin' task and check the output. Probably there is some other error in build script which prevents applying of this option.

Upvotes: 0

Daniel Macak
Daniel Macak

Reputation: 17103

So after a lot of try & error attempts I found out that Idea picks version 1.6 because this is Kotlin's default JVM target version and I haven't set that 1.8 version anywhere in pom.xml or build.gradle. If this version isn't defined there, Idea apparently tends to ignore project settings and stick to defaults.

This means the solution is to set the version in the kotlin plugin manually, but first make sure you have done everything listed in the accepted answer to the question dealing with the same problem but in Java.

Now, assuming you are using Gradle (I stayed with it after the migration) you should just follow the instructions to include kotlin plugin in your build.gradle as written in kotlin's docu on how to use it with Gradle. This isn't enough though, so you have to scroll down in that document and find kotlin compile options, jvmTarget in particular. There you will find out that the version 1.6 is indeed default and you can proceed with configuring your compile task in build.gradle:

compileKotlin { kotlinOptions.jvmTarget = 1.8 }

Voila, now each reimport sticks to 1.8. If you use Maven, there is also a plugin you can use (just google it) and I am sure there will be the same jvmTarget setting at your disposal, though I haven't tried that.

Upvotes: 8

Related Questions