当试图通过IntelliJ运行示例CorDapp (GitHub CorDapp)时,我收到以下错误:

不能将使用JVM目标1.8构建的字节码内联为当前的字节码 使用JVM目标1.6构建

如何修改IntelliJ设置,使所有字节码都使用相同的JVM目标构建?


当前回答

设置sourceCompatibility = javaverse。VERSION_1_8启用了糖化,但它目前无法糖化Kotlin编译器使用的所有Java 8特性。

修复-设置kotlinOptions。jvmTarget到javaverse。应用模块Gradle中的VERSION_1_8会修复这个问题。

使用Java 8语言特性: 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"
  }
}

其他回答

如果在Spring Boot/Kotlin项目中遇到这个消息,只需在pom.xml中将属性“Kotlin .compiler. jvmtarget”设置为“1.8”。

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

在我的例子中,jvmTarget已经在构建中设置了。Gradle文件如下。

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

但我的问题依然存在。 最后,在Preferences > Other Settings > Kotlin Compiler >目标JVM版本后,将目标JVM版本从1.6更改为1.8。见附图,

如果您有很多源集/模块,那么为每个源集/模块分别配置jvmTarget会很麻烦。

你可以为所有这些对象一次性配置jvmTarget,如下所示:

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

这个片段可以用在你的gradle的顶层。构建文件

修改gradle文件后,重新导入所有gradle导入。 要检查它是否工作,请打开Project Structure并验证IntelliJ是否正确地将JVM 1.8分配给所有Kotlin-Modules。它应该是这样的:

我不建议直接在IntelliJ中更改平台,因为任何第一次克隆您的项目的人都可能面临同样的问题。在gradle中正确配置它的好处是,IntelliJ从一开始就能正确地为它们工作。

在我的例子中,我通过以下两个步骤解决了这个问题

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

您可以通过以下方法修复此问题:

打开IntelliJ首选项 去构建,执行,部署>编译器> Kotlin编译器但其他设置> Kotlin编译器如果Android Studio > 3.4 将目标JVM版本更改为1.8 单击Apply