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

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

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


当前回答

您可能需要同时设置compileKotlin和compileTestKotlin。 这适用于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.
    }
}

其他回答

app / build.gradle

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

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

GL

使用Java 8语言特性

使用Kotlin Gradle DSL,这为我解决了这个问题。我把这个加到build.gradle.kts里了。 这是在乔尔的回答之外的

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

对我来说,原因是这个配置在我的构建gradle是在一些模块和一些不是

android {  
...      
kotlinOptions {
        val options = this as KotlinJvmOptions
        options.jvmTarget = "1.8"
    }
...
android {

在我的例子中,只是像这样更改目标JVM版本:File > Setting > Kotlin Compiler > Target JVM Version > 1.8并没有帮助。但是,它解决了编译时错误。但在运行时失败。

我还必须在应用程序构建中添加以下内容。Gradle文件使它工作。

 android {
     // Other code here...
     kotlinOptions {
        jvmTarget = "1.8"
     }
 }

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