Flutter构建APK时Kotlin版本不兼容问题求助
问题现象
执行flutter build apk命令时,清理项目后首次构建会弹出Kotlin版本不兼容警告,二次构建则无该提示。具体报错信息如下:
e: C:/Users/izgrablic/.gradle/caches/8.9/transforms/11d67339be20769aa23d66328d1f3d77/transformed/core-1.10.1/jars/classes.jar!/META-INF/core_release.kotlin_module: 模块使用不兼容的Kotlin版本编译。其元数据的二进制版本为1.8.0,预期版本为1.6.0。
e: C:/Users/izgrablic/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/2.0.0/b48df2c4aede9586cc931ead433bc02d6fd7879e/kotlin-stdlib-2.0.0.jar!/META-INF/kotlin-stdlib.kotlin_module: 模块使用不兼容的Kotlin版本编译。其元数据的二进制版本为1.9.0,预期版本为1.6.0。
e: C:/Users/izgrablic/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/2.0.0/b48df2c4aede9586cc931ead433bc02d6fd7879e/kotlin-stdlib-2.0.0.jar!/META-INF/kotlin-stdlib-jdk8.kotlin_module: 模块使用不兼容的Kotlin版本编译。其元数据的二进制版本为1.9.0,预期版本为1.6.0。
e: C:/Users/izgrablic/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/2.0.0/b48df2c4aede9586cc931ead433bc02d6fd7879e/kotlin-stdlib-2.0.0.jar!/META-INF/kotlin-stdlib-jdk7.kotlin_module: 模块使用不兼容的Kotlin版本编译。其元数据的二进制版本为1.9.0,预期版本为1.6.0。
项目配置文件
项目级 build.gradle
allprojects { repositories { google() mavenCentral() } } rootProject.buildDir = '../build' subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" } subprojects { project.evaluationDependsOn(':app') } tasks.register("clean", Delete) { delete rootProject.buildDir }
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" } 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.test.test_project" compileSdk = flutter.compileSdkVersion ndkVersion = flutter.ndkVersion compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId = "com.test.test_project" // You can update the following values to match your application needs. // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration. minSdk = flutter.minSdkVersion targetSdk = flutter.targetSdkVersion versionCode = flutterVersionCode.toInteger() versionName = flutterVersionName } signingConfigs { release { keyAlias keystoreProperties['keyAlias'] keyPassword keystoreProperties['keyPassword'] storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null storePassword keystoreProperties['storePassword'] } } } 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 "7.3.0" apply false id "org.jetbrains.kotlin.android" version "2.0.0" apply false } include ":app"
解决方案
问题核心是Kotlin插件版本与依赖库的元数据版本不匹配:settings.gradle中指定的Kotlin插件版本为2.0.0,但构建过程预期的元数据版本是1.6.0,首次构建缓存未命中时触发警告,二次构建缓存生效后警告消失。
具体修复步骤
统一Kotlin插件版本
将settings.gradle中的Kotlin插件版本调整为与当前Flutter SDK兼容的稳定版本(比如1.9.20,需匹配你的Flutter版本):id "org.jetbrains.kotlin.android" version "1.9.20" apply false强制统一Kotlin依赖版本
在项目级build.gradle中添加配置,强制所有子项目使用相同的Kotlin标准库版本:subprojects { configurations.all { resolutionStrategy { force "org.jetbrains.kotlin:kotlin-stdlib:1.9.20" force "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.9.20" force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.9.20" } } }清理缓存重新构建
执行flutter clean后再运行flutter build apk,确保缓存更新。
另外注意:当前Android Gradle插件(AGP)版本为7.3.0,建议搭配Kotlin 1.8.20及以上版本,避免版本兼容问题。
内容的提问来源于stack exchange,提问作者izidoraz

