Android Studio 在 Kotlin 项目中引发构建错误,该项目在 Java 接口中调用静态方法
我在 Android Studio 中有一个 Kotlin 项目.我正在从 Kotlin 代码调用 Java 接口中的静态方法.构建失败并出现错误,
I have a Kotlin project in Android Studio. I am calling a static method in Java interface from the Kotlin code. The build fails with the error,
Calls to static methods in Java interfaces are prohibited in JVM target 1.6. Recompile with '-jvm-target 1.8'
我的 build.gradle 中有以下内容,
I have the following in my build.gradle,
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
我还在 Kotlin 编译器设置中将目标 JVM 版本更改为 1.8.尽管如此,构建仍会引发错误.还尝试使缓存无效并重新启动.
I have also changed the Target JVM version to 1.8 in Kotlin compiler settings. Still, the build throws the error. Also tried Invalidating cache and restarting.
Android Studio 版本:3.0.1
Android Studio version: 3.0.1
在 android gradle 中设置 Kotlin 编译选项的正确方法.对您的应用级别 Build.gradle
Proper way to set Kotlin compile options in android gradle. Make these changes to your app level Build.gradle
改变
实现org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
到
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
然后写这个任务
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
kotlinOptions {
jvmTarget = '1.8'
apiVersion = '1.1'
languageVersion = '1.1'
}
}
就在下面(不一定)
repositories {
mavenCentral()
}
有关详细信息,请参阅此处