【错误记录】Android Studio 编译报错 ( e: Unknown JVM target version: 1.9 Supported versions: 1.6, 1.8, 9, 10 )

文章讲述了在AndroidStudio中遇到编译错误,具体为未知的JVM目标版本1.9,支持的版本为1.6到1.8及9到18。解决方案是将build.gradle文件中的kotlinOptions的jvmTarget从1.9改为9,然后重新编译,问题得到解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >





一、错误记录



在 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

完整报错信息 :

> 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 版本 ;

android {
    namespace 'kim.hsl.databinding_demo'
    compileSdk 32
    
    kotlinOptions {
        jvmTarget = '1.9'
    }
}

在这里插入图片描述

这是由 Android Studio 自动生成的版本 , 居然报错 ;

将该版本修改为 9 , kotlinOptions { jvmTarget = ‘9’ } 然后重新编译 , 编译通过 ;

核心文件代码示例 :

android {
    namespace 'kim.hsl.databinding_demo'
    compileSdk 32
    
    kotlinOptions {
        jvmTarget = '9'
    }
}

修改后的完整配置文件 :

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'
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值