为什么 kotlin gradle 插件不能用1.8目标构建?

我使用 intellij 为 kotlin 1.2.10配置了最简单的 gradle 项目:

buildscript {
ext.kotlin_version = '1.2.10'


repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}


group 'com.ali'
version '1.0-SNAPSHOT'


apply plugin: 'java'
apply plugin: 'kotlin'


sourceCompatibility = 1.8


repositories {
mavenCentral()
}


dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
testCompile group: 'junit', name: 'junit', version: '4.12'
}


compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

我有一个简单的 Java 界面:

public interface MyMath {
static int myAbs(int input) {
return Math.abs(input);
}
}

当我导入这个接口并尝试调用 myAbs方法时,它失败了,出现了这个错误:

Error:(6, 12) Kotlin: Calls to static methods in Java interfaces are prohibited in JVM target 1.6. Recompile with '-jvm-target 1.8'

我已经创建了一个 intellij Kotlin 应用程序,它工作正常。这是一个新的 Kotlin 梯度插件的错误吗?

77937 次浏览

Not quite sure why this works, but you could try changing the setting in the Idea itself. Since Gradle from the command line works, but not when building from IntelliJ this is probably the root.

Go to File -> Project Structure. Go to the Project tab and make sure Project SDK is 8 (or newer) and set the Project language level to Java 8

enter image description here

The config in there seems to override Gradle for some reason, so changing it should work

It turned out that it was my kotlin compiler configuration in intellij settings. In Settings > Build, Execution, Deployment > Compiler > Kotlin Compiler a setting called Target JVM version should have been set to 1.8.

I think this could be helpful for those using Android Studio 3.2 on Mac.

To change the Kotlin Compiler Target JVM version you should go to Android Studio -> Preferences -> Kotlin Compiler and then change the Target JVM version choosing from the dropdown.

Anyway, I'm still getting the following error

Calls to static methods in Java interfaces are prohibited in JVM target 1.6. Recompile with '-jvm-target 1.8'

SOLVED

Adding the following to my build.gradle solved the problem:

android {
kotlinOptions {
jvmTarget = '1.8'
}
}

About this and other Gradle configuration options: https://kotlinlang.org/docs/reference/using-gradle.html


With Kotlin Gradle DSL:

import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions


(kotlinOptions as KotlinJvmOptions).apply {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}

In my case, I tried all @alisabzevari options including given in comment, but It didn't work,

my mistake I added java file also in src/<sourcset_name>kotlin/ folder, later I converted java file to kotlin file. and Voila! It works.

May be it will help somebody.

This is also set in project settings, under Project Settings > Modules > Kotlin. This was my specific problem... literally the last thing I tried.

Please check if you have the following three stuff set in idea:

  1. kotlin options in build.gradle
    kotlinOptions {
jvmTarget = '1.8'
}
  1. Check the relevant Idea preferences Preferences

  2. Check the project Facets enter image description here

//Just add the below code in your Build gradle(Module:app)

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

There is a gradle property used by Kotlin plugin which can be used. When you add

kotlin.setJvmTargetFromAndroidCompileOptions = true

to your gradle.properties file. When you have this configuration on your build.config

android{
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
}

problem sould be solved.

This worked for me in Gradle with Kotlin DSL:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions.jvmTarget = JavaVersion.VERSION_11.toString()

Settings > Build, Execution, Deployment > Compiler > Kotlin Compiler a setting called Target JVM version should have been set to 1.8.

solved it by editing build.gradle.kts

val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions.jvmTarget = "1.8"

Try this code, it solved my problem. I had a problem inViewModel Factory. //Just add the below code in your Build gradle(Module:app)

Error "Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option"

    kotlinOptions {
freeCompilerArgs = ['-Xjvm-default=compatibility']
jvmTarget = '1.8'
}