Cannot inline bytecode built with JVM target 1.8 i

2020-01-30 03:04发布

问题:

When trying to run the Example CorDapp (https://github.com/corda/cordapp-example) 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?

回答1:

app/build.gradle

android {
    ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }
}

GL

Source



回答2:

You can fix this issue as follows:

  • 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


回答3:

you should configure something like as follows in build.gradle

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


回答4:

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


回答5:

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)


回答6:

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.



回答7:

a picture is worth a thousand words



回答8:

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



回答9:

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


回答10:

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 {


回答11:

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

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


回答12:

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



回答13:

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"



回答14:

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



回答15:

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.



回答16:

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


回答17:

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



回答18:

If you are done changing versions and all and still not able to run your code.

Try this.

Create a class like HeaderInterceptor .

 class HeaderInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response = chain.run {
        proceed(
            request()
                .newBuilder()
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "application/json")
                .addHeader(
                    "Authorization",
                    "Bearer" + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwcm9maWxlX2lkIjo0NDcsImxvY2F0aW9uX2lkIjoxMTgyLCJpc3N1ZWRfZm9yIjoiY3VzdG9tZXIiLCJzdWIiOjY3MSwiaXNzIjoiaHR0cHM6Ly9hcGkubmVvbi1tb2JpbGUuY29tL2FwaS9mMS9maXJlYmFzZS1sb2dpbiIsImlhdCI6MTU2ODE3OTgyMSwiZXhwIjoxNTcwNzcxODIxLCJuYmYiOjE1NjgxNzk4MjEsImp0aSI6ImI1WGNEVDh6TlhTSWprb04ifQ.vNU--vvlz6Qy6qzwooCQxYL2gtB79v0t378qVWXWxcs"
                )
                .build()
        )
    }
}

And now just add it as

.addInterceptor(HeaderInterceptor())

This might help you to save your day.