Android Studio 在 Kotlin 项目中引发构建错误,该项目在 Java 接口中调用静态方法 [英] Android Studio throws build error in Kotlin project which calls static method in java interface

查看:20
本文介绍了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()
}

有关详细信息,请参阅此处

这篇关于Android Studio 在 Kotlin 项目中引发构建错误,该项目在 Java 接口中调用静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆