【发布时间】:2020-04-17 19:09:12
【问题描述】:
我正在处理以下问题中描述的相同错误,但提出的所有解决方案都没有解决我的问题。
Why kotlin gradle plugin cannot build with 1.8 target?
Android Studio throws build error in Kotlin project which calls static method in java interface
Intellij refuses to set the Kotlin target jvm to 1.8?
在我的情况下有一个小的区别。我正在使用 Android Studio 3.5,我最近将 Kotlin 版本升级到 1.3.61 最初我收到了通常的错误消息:
calls to static methods in Java interfaces are prohibited in JVM target 1.6. Recompile with '-jvm-target 1.8'
我更改了 文件 -> 设置 -> Kotlin 编译器 -> 目标 JVM 版本 并在此更改后编译没有错误,但后来当我尝试在模拟器上部署时,构建再次失败。因此,当它构建应用程序时它可以工作,当它进行完整构建以部署它时,它会失败,但理论上它们使用相同的配置。当我在 gradle 窗口中单击设置时,无论选择哪个节点,它都会打开主设置的同一窗口。有什么区别? 我检查了所有可能的设置和配置,我还检查了项目结构和构建/运行/调试模板,但我找不到其他可以设置属性的地方。 我试图在 gradle 构建 JvmTarget 中设置,但是对于这个 Kotlin 版本,该属性不再存在。我考虑了一个下载问题,并将 mavenCentral 添加到存储库中,但没有任何改变。如果最近发生了一些变化,这个页面似乎不是最新的: https://kotlinlang.org/docs/reference/using-gradle.html 我还能尝试什么?为什么只有完整构建失败?
更新:
目前我通过添加非静态方法来解决这个问题,但它会在性能方面付出代价。所以我希望解决方案迟早会出现。同时我正在添加构建文件,虽然我认为它不会为问题的描述添加很多内容:
//Originally the plugins were declared with the syntax "apply plugin ...."
//Changing to this form did not change the result
plugins {
id('com.android.application')
id('org.jetbrains.kotlin.android')
id('org.jetbrains.kotlin.android.extensions')
}
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId ..........
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
// Commented out because the property JvmTarget is not found with Kotlin 1.3.61
// compileReleaseKotlin {
// JvmTarget='1.8'
// }
// compileDebugKotlin {
// JvmTarget='1.8'
// }
lintOptions {
abortOnError false
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//I tried also JDK 7
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
//Follow other dependencies which are irrelevant
// ...
}
【问题讨论】:
-
你能分享你的构建文件吗?如果我没记错的话,你也可以直接用字符串指定版本(“1.8”)
标签: android-studio kotlin