Flutter 3.27 JVM目标兼容性不一致问题求助
Flutter 3.27 JVM目标兼容性不一致问题解决方案
问题说明
使用Flutter 3.27构建Release版本时,出现compileReleaseJavaWithJavac(JVM目标17)与compileReleaseKotlin(JVM目标21)的兼容性冲突。已尝试以下操作但未解决:
- 升级Kotlin至2.0.20
- 切换JDK版本为17/21并配置Java Home
- 升级Gradle至8.10.2
当前配置文件
app/build.gradle
plugins { id "com.android.application" id "kotlin-android" // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins. id "dev.flutter.flutter-gradle-plugin" } android { namespace = "com.example.giplapp" compileSdk = flutter.compileSdkVersion ndkVersion = "27.0.12077973" compileOptions { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } kotlinOptions { jvmTarget = JavaVersion.VERSION_17 } kotlin { jvmToolchain(17) } defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId = "com.example.giplapp" // You can update the following values to match your application needs. // For more information, see: https://flutter.dev/to/review-gradle-config. minSdk = flutter.minSdkVersion targetSdk = flutter.targetSdkVersion versionCode = flutter.versionCode versionName = flutter.versionName } buildTypes { release { // TODO: Add your own signing config for the release build. // Signing with the debug keys for now, so `flutter run --release` works. signingConfig = signingConfigs.debug } } } flutter { source = "../.." }
settings.gradle
pluginManagement { def flutterSdkPath = { def properties = new Properties() file("local.properties").withInputStream { properties.load(it) } def flutterSdkPath = properties.getProperty("flutter.sdk") assert flutterSdkPath != null, "flutter.sdk not set in local.properties" return flutterSdkPath }() includeBuild("$flutterSdkPath/packages/flutter_tools/gradle") repositories { google() mavenCentral() gradlePluginPortal() } } plugins { id "dev.flutter.flutter-plugin-loader" version "1.0.0" id "com.android.application" version '8.7.3' apply false id "org.jetbrains.kotlin.android" version "2.0.20" apply false } include ":app"
可行解决步骤
1. 全局统一所有模块JVM配置
部分第三方Flutter插件可能自带独立Gradle配置,会覆盖主项目设置。在项目根目录的build.gradle中添加全局配置,强制所有模块使用JVM 17:
subprojects { afterEvaluate { project -> if (project.hasProperty('android')) { project.android { compileOptions { sourceCompatibility JavaVersion.VERSION_17 targetCompatibility JavaVersion.VERSION_17 } kotlinOptions { jvmTarget = JavaVersion.VERSION_17 } kotlin { jvmToolchain(17) } } } } }
2. 匹配Kotlin与Flutter版本兼容性
Kotlin 2.0.x与Flutter 3.27存在兼容性适配问题,尝试降级Kotlin至官方推荐的稳定版本1.9.24:
修改settings.gradle中的Kotlin版本:
id "org.jetbrains.kotlin.android" version "1.9.24" apply false
3. 明确指定JDK路径
在local.properties中添加JDK 17的绝对路径,避免Gradle使用系统默认JDK:
java.home=/path/to/your/jdk17
同时在IDE的项目SDK配置中,确认指向JDK 17。
4. 清理构建缓存
执行以下命令彻底清除旧配置残留:
flutter clean ./gradlew cleanBuildCache
之后重新构建项目。
5. 排查插件的JVM配置
通过命令查看所有模块的JVM目标设置,定位异常插件:
./gradlew projectProperties | grep jvmTarget
若发现某插件硬编码JVM目标为21,可替换为兼容版本,或通过全局配置强制覆盖。
内容的提问来源于stack exchange,提问作者Amritpal Singh
相关产品推荐
相关产品推荐

