Flutter执行flutter run时JVM目标版本不匹配报错求助
问题
执行flutter run时出现如下编译错误:
FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':google_api_headers:compileDebugKotlin'. > Inconsistent JVM-target compatibility detected for tasks 'compileDebugJavaWithJavac' (1.8) and 'compileDebugKotlin' (17). Consider using JVM Toolchain: https://kotl.in/gradle/jvm/toolchain Learn more about JVM-target validation: https://kotl.in/gradle/jvm/target-validation
问题源于google_api_headers包的JVM目标版本与项目Java版本不匹配,已更新所有依赖、Android及Gradle版本至最新,问题仍存在。
环境信息
./gradlew -v输出:
------------------------------------------------------------ Gradle 8.9 ------------------------------------------------------------ Build time: 2024-07-11 14:37:41 UTC Revision: d536ef36a19186ccc596d8817123e5445f30fef8 Kotlin: 1.9.23 Groovy: 3.0.21 Ant: Apache Ant(TM) version 1.10.13 compiled on January 4 2023 Launcher JVM: 22.0.1 (Oracle Corporation 22.0.1+8-16) Daemon JVM: /Library/Java/JavaVirtualMachines/jdk-22.jdk/Contents/Home (no JDK specified, using current Java home) OS: Mac OS X 14.6.1 aarch64
- 本地JDK版本:17
配置文件
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.5.0" apply false id "org.jetbrains.kotlin.android" version "1.9.23" apply false id "com.google.gms.google-services" version "4.3.8" apply false } include ":app"
项目根目录build.gradle
allprojects { repositories { google() mavenCentral() // [required] flutter_background_geolocation maven { url "${project(':flutter_background_geolocation').projectDir}/libs" } maven { url 'https://developer.huawei.com/repo/' } // [required] background_fetch maven { url "${project(':background_fetch').projectDir}/libs" } } } ext { compileSdkVersion = 34 // or higher / as desired targetSdkVersion = 34 // or higher / as desired minSdkVersion = 23 // Required minimum appCompatVersion = "1.4.2" // or higher / as desired playServicesLocationVersion = "21.0.1" // or higher / as desired } rootProject.buildDir = '../build' subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" project.evaluationDependsOn(':app') } tasks.register("clean", Delete) { delete rootProject.buildDir }
app/build.gradle
plugins { id "com.android.application" id "kotlin-android" id "dev.flutter.flutter-gradle-plugin" id "com.google.gms.google-services" // Google Services plugin } def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { localPropertiesFile.withReader('UTF-8') { reader -> localProperties.load(reader) } } def flutterVersionCode = localProperties.getProperty('flutter.versionCode') if (flutterVersionCode == null) { flutterVersionCode = '1' } def flutterVersionName = localProperties.getProperty('flutter.versionName') if (flutterVersionName == null) { flutterVersionName = '1.0' } def keystoreProperties = new Properties() def keystorePropertiesFile = rootProject.file('key.properties') if (keystorePropertiesFile.exists()) { keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) } android { namespace "com.mycompany.test" compileSdkVersion 34 sourceSets { main.java.srcDirs += 'src/main/kotlin' } lintOptions { disable 'InvalidPackage' checkReleaseBuilds false } defaultConfig { applicationId "com.mycompany.test" minSdkVersion 23 targetSdkVersion 34 versionCode flutterVersionCode.toInteger() versionName flutterVersionName multiDexEnabled true } compileOptions { // Flag to enable support for the new language APIs (for flutter_local_notification) coreLibraryDesugaringEnabled true // Sets Java compatibility to Java 8 sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget=JavaVersion.VERSION_1_8 } signingConfigs { release { keyAlias keystoreProperties['keyAlias'] keyPassword keystoreProperties['keyPassword'] storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null storePassword keystoreProperties['storePassword'] } } 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 minifyEnabled true } } } flutter { source '../..' } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.10" coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.2' }
已执行flutter pub upgrade --major-versions更新所有依赖。
解决方案
针对google_api_headers包的JVM版本不兼容问题,可尝试以下修复方式:
1. 统一所有子项目的JVM目标版本
修改项目根目录build.gradle中的subprojects块,强制所有子项目(包括第三方插件)使用和主项目一致的JVM目标版本:
subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" project.evaluationDependsOn(':app') // 添加以下配置 tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach { kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() } } }
2. 使用Gradle JVM工具链统一版本
在项目根目录build.gradle中添加JVM工具链配置,指定使用JDK 17:
subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" project.evaluationDependsOn(':app') // 添加工具链配置 java { toolchain { languageVersion = JavaLanguageVersion.of(17) } } tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach { kotlinOptions { jvmTarget = JavaVersion.VERSION_17.toString() } } }
同时修改app/build.gradle中的版本配置,与工具链对齐:
compileOptions { coreLibraryDesugaringEnabled true sourceCompatibility JavaVersion.VERSION_17 targetCompatibility JavaVersion.VERSION_17 } kotlinOptions { jvmTarget=JavaVersion.VERSION_17 }
3. 临时降级google_api_headers包版本
在pubspec.yaml中指定该包的旧兼容版本,选择JVM目标版本为1.8的版本:
dependencies: google_api_headers: ^3.0.0 # 替换为实际兼容的旧版本号
4. 清理缓存后重新构建
执行以下命令清理缓存,避免旧编译文件干扰:
flutter clean rm -rf ~/.gradle/caches # Mac/Linux环境 # Windows环境请删除C:\Users\你的用户名\.gradle\caches文件夹 flutter pub get flutter run
内容的提问来源于stack exchange,提问作者LAZREQ
相关产品推荐
相关产品推荐

