[android] Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6

When trying to run the Example CorDapp (GitHub CorDapp) via IntelliJ, I receive the following error:

Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6

How can I modify the IntelliJ settings so that all the bytecode is built with the same JVM target?

This question is related to android intellij-idea kotlin jvm corda

The answer is


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"
        }
    }

you should configure something like as follows in build.gradle

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

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"
         }

If you use Eclipse assuming you downloaded the Kotlin plugin:

Right click project -> Properties -> Kotlin Compiler -> Enable project specific settings -> JVM target version "1.8"


In my case File > Setting > Kotlin Compiler > Target JVM Version > 1.8


For recent versions of Android Studio, if changing just the Kotlin Target VM version didn't work.

File ? Project Structure ? Modules (app): set both "Source Compatibility" and "Target Compatibility" to "1.8 (Java 8)". Press "OK" and sync project with Gradle.


For Gradle with Kotlin language (*.gradle.kts files), add this:

android {
    [...]
    kotlinOptions {
        this as KotlinJvmOptions
        jvmTarget = "1.8"
    }
}

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,

enter image description here


If you'r facing this message in a Spring Boot/Kotlin project, just set the property "kotlin.compiler.jvmTarget" to "1.8" in your pom.xml.

    <properties>
        <kotlin.version>1.3.70</kotlin.version>
        <kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
    </properties>
    ...

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"
        }
    }

Nothing worked for me until I updated my kotlin plugin dependency.
Try this:
1. Invalidate cahce and restart.
2. Sync project (at least try to)
3. Go File -> Project Structure -> Suggestions
4. If there is an update regarding Kotlin, update it.
Hope it will help someone.


In Android Studio 4.3.2 adding through the below procedure is not working.

  1. Open the IntelliJ preferences
  2. Go to Build, Execution, Deployment > Compiler > Kotlin Compiler BUT Other Settings > Kotlin compiler if Android Studio > 3.4
  3. Change the Target JVM version to 1.8
  4. 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. enter image description here


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


If non of the above answers don't work you can do this in kotlin dsl

android {
...

    tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
        kotlinOptions {
            jvmTarget = "1.8"
        }
    }
}

If using Visual Studio Code** with Kotlin extension, go to the plugin management Crtl + Shift + x, type kotlin and click on manage (the little gear) >> Configure Extension Settings

on Kotlin >> Compiler >> Jvm:Target - type the java version. In my situation just typed 1.8

And then restart :-)

** vscode or just 'code' for linux


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 {

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"
     }
 }

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
    }

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.

enter image description here

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"
  }
}

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"
    }
}

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"
}

Another hint for Eclipse users. After double checking the jvm target settings pointed out by other users, I still had the same problem, and that was caused by missing Kotlin Runtime Library. For eg, when creating a project with spring initializr, it is not added automatically. For adding it: right click on your project -> Build path -> Add libraries... -> User Library, and simply add org.jetbrains.kotlin.core.KOTLIN_CONTAINER

Make sure you refresh your gradle project afterwards (right click -> Gradle -> Refresh gradle project)


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"
    }
}

The next solution helped me. Add to build.gradle

 compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

All answers here are using gradle but if someone like me ends up here and needs answer for maven:

    <build>
        <sourceDirectory>src/main/kotlin</sourceDirectory>
        <testSourceDirectory>src/test/kotlin</testSourceDirectory>

        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <jvmTarget>11</jvmTarget>
                </configuration>
            </plugin>
        </plugins>
    </build>

The change from jetbrains archetype for kotlin-jvm is the <configuration></configuration> specifying the jvmTarget. In my case 11


You may need to set both compileKotlin and compileTestKotlin. This works on gradle 6.5.1.

compileKotlin {
    kotlinOptions {
        languageVersion = "1.2"
        apiVersion = "1.2"
        jvmTarget = "1.8"
        javaParameters = true   // Useful for reflection.
    }
}

compileTestKotlin {
    kotlinOptions {
        languageVersion = "1.2"
        apiVersion = "1.2"
        jvmTarget = "1.8"
        javaParameters = true   // Useful for reflection.
    }
}

Using the Kotlin Gradle DSL, this solved the issue for me. I added this to the build.gradle.kts. This is in addition to the answer by Joel

val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString()

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 under Project Settings
    • If it is empty then click on the small + button
  • Click on your Kotlin module/modules
  • Change the Target Platform to JVM 1.8 (also it's better to check Use project settings option)

a picture is worth a thousand words

a picture is worth a thousand words


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


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:

project structure

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.


I'm using Kotlin and Gradle for normal JVM development, (not android) and this worked for me in build.gradle:

allprojects {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
        kotlinOptions.jvmTarget = JavaVersion.VERSION_11.toString()
    }
}

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to intellij-idea

IntelliJ: Error:java: error: release version 5 not supported Has been compiled by a more recent version of the Java Runtime (class file version 57.0) Error: Java: invalid target release: 11 - IntelliJ IDEA IntelliJ can't recognize JavaFX 11 with OpenJDK 11 Error: JavaFX runtime components are missing, and are required to run this application with JDK 11 ERROR Source option 1.5 is no longer supported. Use 1.6 or later Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 How to configure "Shorten command line" method for whole project in IntelliJ intellij idea - Error: java: invalid source release 1.9 Failed to resolve: com.google.android.gms:play-services in IntelliJ Idea with gradle

Examples related to kotlin

No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator How to allow all Network connection types HTTP and HTTPS in Android (9) Pie? Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0 Default interface methods are only supported starting with Android N Error : Program type already present: android.support.design.widget.CoordinatorLayout$Behavior Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 startForeground fail after upgrade to Android 8.1 How to get current local date and time in Kotlin How to add an item to an ArrayList in Kotlin? HTTP Request in Kotlin

Examples related to jvm

Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 How can I get a random number in Kotlin? Kotlin unresolved reference in IntelliJ Is JVM ARGS '-Xms1024m -Xmx2048m' still useful in Java 8? Android Gradle Could not reserve enough space for object heap Android java.exe finished with non-zero exit value 1 Android Studio Gradle project "Unable to start the daemon process /initialization of VM" Android Studio - No JVM Installation found Android Studio error: "Environment variable does not point to a valid JVM installation" Installing Android Studio, does not point to a valid JVM installation error

Examples related to corda

Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6