Question or issue of Kotlin Programming:
When trying to run the Example CorDapp (GitHub CorDapp) via IntelliJ, I receive the following error:
How can I modify the IntelliJ settings so that all the bytecode is built with the same JVM target?
How to solve this issue?
Solution no. 1:
app/build.gradle
android { ... compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() } }
GL
Use Java 8 language features
Solution no. 2:
You can fix this issue as follows:
- Open the IntelliJ preferences
- Go to
Build, Execution, Deployment
>Compiler
>Kotlin Compiler
BUTOther Settings
>Kotlin compiler
if Android Studio >3.4
- Change the
Target JVM version
to1.8
- Click
Apply
Solution no. 3:
you should configure something like as follows in build.gradle
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { kotlinOptions { jvmTarget = "1.8" } }
Solution no. 4:
please add this code to android section inside your app/build.gradle
compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8 }
Solution no. 5:
In my case, just changingTarget JVM Version like this: File > Setting > Kotlin Compiler > Target JVM Version > 1.8 did not help. However, it does resolved compile time error. But failed at runtime.
I also had to add following in app build.gradle file to make it work.
android { // Other code here... kotlinOptions { jvmTarget = "1.8" } }
Solution no. 6:
When the other solutions did not work for you (Changing JVM version on Compiler settings and adding jvmTarget
into your build.gradle
), because of your .iml
files trying to force their configurations you can change the target platform from Project Settings.
- Open
File > Project Structure
- Go to
Facets
underProject Settings
- If it is empty then click on the small
+
button
- If it is empty then click on the small
- Click on your Kotlin module/modules
- Change the
Target Platform
toJVM 1.8
(also it’s better to checkUse project settings
option)
Solution no. 7:
In my case this code did’n work until I move apply plugin: 'kotlin-android'
from bottom to top.
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' android { ... compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = "1.8" } }
Solution no. 8:
In Android Studio 4.3.2 adding through the below procedure is not working.
- Open the IntelliJ preferences
- Go to Build, Execution, Deployment > Compiler > Kotlin Compiler BUT Other Settings > Kotlin compiler if Android Studio > 3.4
- Change the Target JVM version to 1.8
- Click Apply
The reason is, Android studio is unable to add the below code in the module level Gradle file. Please add it manually.
kotlinOptions { jvmTarget = "1.8" }
Just for the addon, search Target JVM version in the android studio search. It will take you directly to the option.
Solution no. 9:
a picture is worth a thousand words
Solution no. 10:
Feb 2020
android 3.4+
Go to File -> Settings -> Kotlin Compiler -> Target JVM Version > set to 1.8 and then make sure to do File -> Sync project with Gradle files
Or add this into build.gradle(module:app) in android block:
kotlinOptions { jvmTarget = "1.8" }
Solution no. 11:
As it is written in the using-maven docs from the Kotlin website:
You just have to put <kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
into the properties section of your pom.xml
Solution no. 12:
If you have many sourcesets/modules it can be cumbersome to configure the jvmTarget for each of them separately.
You can configure the jvmTarget for all of them at once like so:
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { kotlinOptions { jvmTarget = "1.8" } }
This snippet can be used on top level of your gradle.build file
After modifying the gradle file Reimport All Gradle Imports
.
To check if it worked, open Project Structure
and verify that IntelliJ correctly assigned JVM 1.8
to all Kotlin-Modules. It should look like this:
I would not recommend changing the platform directly in IntelliJ, because anyone else cloning your project for the first time is likely to face the same issue. Configuring it correctly in gradle has the advantage that IntelliJ is going to behave correctly for them right from the start.
Solution no. 13:
In my case, I solved this by following these two steps
1. Go to android studio preferences -> other settings -> kotlin compiler -> set Target JVM version = 1.8 if it doesn't work then go to the second option. 2. In your module-level build.gradle file add compileOptions { sourceCompatibility = 1.8 targetCompatibility = 1.8 } tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { kotlinOptions { jvmTarget = "1.8" } }
Solution no. 14:
In my case, jvmTarget was already set in build.gradle
file as below.
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { kotlinOptions { jvmTarget = "1.8" } }
But my issue was still there.
Finally, it gets resolved after Changing Target JVM version from 1.6 to 1.8 in Preferences > Other Settings > Kotlin Compiler > Target JVM version. see attached picture,
Solution no. 15:
For me the reason was this configuration in my build gradle was in some modules and in some it wasnt
android { ... kotlinOptions { val options = this as KotlinJvmOptions options.jvmTarget = "1.8" } ... android {
Solution no. 16:
This helped my project to build, add this to module build.gradle file:
compileOptions { sourceCompatibility 1.8 targetCompatibility 1.8 } tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { kotlinOptions { jvmTarget = "1.8" } }
Solution no. 17:
For Gradle with Kotlin language (*.gradle.kts
files), add this:
android { [...] kotlinOptions { this as KotlinJvmOptions jvmTarget = "1.8" } }
Solution no. 18:
Setting sourceCompatibility = JavaVersion.VERSION_1_8
enables desugaring, but it is currently unable to desugar all the Java 8 features that the Kotlin compiler uses.
Fix – Setting kotlinOptions.jvmTarget to JavaVersion.VERSION_1_8
in the app module Gradle would fix the issue.
Use Java 8 language features:
https://developer.android.com/studio/write/java8-support
android { ... // Configure only for each module that uses Java 8 // language features (either in its source code or // through dependencies). compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } // For Kotlin projects kotlinOptions { jvmTarget = "1.8" } }
Solution no. 19:
in most cases this is enough:
compileKotlin { kotlinOptions.jvmTarget = "1.8" } compileTestKotlin { kotlinOptions.jvmTarget = "1.8" }
if you have declared custom Gradle tasks like integrationTest
for example, add a configuration for compile<YourTaskName>Kotlin
as well:
compileIntegrationTestKotlin { kotlinOptions.jvmTarget = "1.8" }
Solution no. 20:
if you are in android project
in your app’s build.gradle
under android{}
android{ //other configs... buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = "1.8" } }