文章目录
- 一、错误记录
- 二、解决方案
一、错误记录
在 Android Studio 中编译执行 Android 工程 , 报如下错误 :
e: Unknown JVM target version: 1.9
Supported versions: 1.6, 1.8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18
Task :app:compileDebugKotlin FAILED
FAILURE: Build failed with an exception.
- What went wrong: Execution failed for task ‘:app:compileDebugKotlin’. A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction Compilation error. See log for more details
完整报错信息 :
代码语言:javascript复制> Task :app:compileDebugKotlin
'compileDebugJavaWithJavac' task (current target is 1.8) and 'compileDebugKotlin' task (current target is 1.9) jvm target compatibility should be set to the same Java version.
e: Unknown JVM target version: 1.9
Supported versions: 1.6, 1.8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18
> Task :app:compileDebugKotlin FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
> Compilation error. See log for more details
* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:compileDebugKotlin'.
* Get more help at https://help.gradle.org
BUILD FAILED in 2m 2s
30 actionable tasks: 30 executed
二、解决方案
报错的核心问题 , 发现未知的 Java 虚拟机版本 1.9 , 支持的 JVM 版本号只能是 1.6, 1.8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18
中的版本号 ;
e: Unknown JVM target version: 1.9
Supported versions: 1.6, 1.8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18
在 Settings 设置中 , 设置的 JDK 版本是 11 版本的 ;
在 build.gradle 中 , 发现有 如下设置 , 其中设置了 jvmTarget 为 1.9 版本 ;
代码语言:javascript复制android {
namespace 'kim.hsl.databinding_demo'
compileSdk 32
kotlinOptions {
jvmTarget = '1.9'
}
}
这是由 Android Studio 自动生成的版本 , 居然报错 ;
将该版本修改为 9 , kotlinOptions { jvmTarget = ‘9’ } 然后重新编译 , 编译通过 ;
核心文件代码示例 :
代码语言:javascript复制android {
namespace 'kim.hsl.databinding_demo'
compileSdk 32
kotlinOptions {
jvmTarget = '9'
}
}
修改后的完整配置文件 :
代码语言:javascript复制plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'kim.hsl.databinding_demo'
compileSdk 32
defaultConfig {
applicationId "kim.hsl.databinding_demo"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
// 启用 DataBinding
dataBinding {
enabled = true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '9'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}