You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

更新Flutter后如何解决“JVM目标兼容性不一致”错误?

解决Flutter更新后:flutter_image_compress_common:compileDebugKotlin JVM目标兼容性不一致问题

问题描述

更新Flutter到3.16.3稳定版后,编译Android项目时出现以下错误:

任务:flutter_image_compress_common:compileDebugKotlin执行失败,错误为“JVM目标兼容性不一致”

当前配置信息:

android/build.gradle

buildscript {
ext.kotlin_version = '1.9.21'
repositories {
    google()
    mavenCentral()
}

dependencies {
    classpath 'com.android.tools.build:gradle:8.2.0'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath 'com.google.gms:google-services:4.3.15'
}}

app/build.gradle

compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '17'}

Flutter版本

Flutter 3.16.3 • channel stable • 
Framework • revision b0366e0a3f (8 days ago) • 2023-12-05 19:46:39 -0800
Engine • revision 54a7145303
Tools • Dart 3.2.3 • DevTools 2.28.4

解决方案

问题根源是flutter_image_compress_common模块使用的JVM目标版本和主项目不一致,需统一全局Kotlin编译配置:

  1. 修改android/build.gradle,在allprojects块中添加全局配置,强制所有模块使用JVM目标17:
allprojects {
    repositories {
        google()
        mavenCentral()
    }

    // 统一所有模块的Kotlin JVM目标版本
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
        kotlinOptions {
            jvmTarget = '17'
        }
    }
}
  1. 清理项目缓存:

    • 执行命令:flutter clean
    • 进入Android目录,执行命令:./gradlew clean(Windows系统用gradlew clean)
  2. 重新编译项目,运行flutter run即可。

内容的提问来源于stack exchange,提问作者Adesh Yadav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 23:28:22