7

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?

Dan Macak
  • 16,109
  • 3
  • 26
  • 43

2 Answers2

8

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.

Dan Macak
  • 16,109
  • 3
  • 26
  • 43
  • 1
    For me, the version changes back to 1.6 in module's Kotlin facet after the import even with this kotlinOptions settings. I had it formatted differently and even with this exact string, byte to byte, it doesn't work. Maybe it's broken because it's a submodule. – Dalibor Filus Jan 30 '19 at 20:02
  • @DaliborFilus Can you show me your build.gradle? In the chat maybe? – Dan Macak Jan 30 '19 at 20:21
  • https://gist.github.com/NoICE/7951025d72d5b2e79e812e39fa86d0f2 I think this issue is just it and they tried to fix it again and again and again... https://youtrack.jetbrains.com/issue/KT-24662 – Dalibor Filus Jan 30 '19 at 22:33
  • @DaliborFilus Are your sources and test sources in the standard folders? (src/main/kotlin etc.) – Dan Macak Feb 04 '19 at 20:13
  • 1
    this does not solve the problem for me. Intellij resets the change after any change in my build file. Are there any other magical incantations for gradle that idea looks at? I have a multi module project, which probably adds to the problem. – Jilles van Gurp Mar 26 '19 at 14:26
0

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.

AndreyKo
  • 1
  • 1