Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 619
Skocdopole Avatar asked Oct 20 '18 19:10

Skocdopole


People also ask

Whats new Kotlin 1. 7 0?

It provides a consistent developer experience in multiplatform projects. For example, you'll have a much easier time creating new cross-platform mobile applications that work on both Android and iOS. The new Kotlin/Native memory manager lifts restrictions on object-sharing between threads.

What is the current Kotlin version?

Kotlin 1.6. 0 is now officially released with Stable exhaustive whens, Kover, and new memory manager for Kotlin/Native!

Does kotlin support Java 11?

 Yes. Kotlin is supported as a first-class language on Android.

How do I change the source level in Intellij?

From the main menu, select File | Project Structure Ctrl+Alt+Shift+S . Under Project Settings, select Modules | Sources. From the Language level list, select the necessary option. To use the project language level, select Project default.


Video Answer


1 Answers

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.

like image 73
Skocdopole Avatar answered Nov 14 '22 20:11

Skocdopole