更新到新的 Kotlin 版本后 Flutter 构建失败 我正在开发一个 Flutter 项目,在更新一些依赖项后,我遇到了构建错误。我使用的是最新版本的 Kotlin,当我运行 flutter build apk 时,它失败并出现以下错误:
e: C:/Users/Moham/.gradle/caches/transforms-3/1a36c150372f3edd1303b1fe7aa45a27/transformed/fragment-1.7.1-api.jar!/META-INF/fragment_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0.
Running Gradle task 'assembleRelease'... 251.8s
┌─ Flutter Fix ──────────────────────────────────────────────────────────────────────────────────────────┐
│ [!] Your project requires a newer version of the Kotlin Gradle plugin. │
│ Find the latest version on https://kotlinlang.org/docs/releases.html#release-details, then update the │
│ version number of the plugin with id "org.jetbrains.kotlin.android" in the plugins block of │
│ C:\flutter_app\lklk-live\android\settings.gradle. │
│ │
│ Alternatively (if your project was created before Flutter 3.19), update │
│ C:\flutter_app\lklk-live\android\build.gradle │
│ ext.kotlin_version = '<latest-version>' │
└────────────────────────────────────────────────────────────────────────────────────────────────────────┘
似乎表明 Kotlin 版本不兼容。这是我的 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
}
设置.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
}
settings.ext.flutterSdkPath = flutterSdkPath()
includeBuild("${settings.ext.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.20" apply false
}
include ":app"
看来我需要更新 Kotlin Gradle 插件,但我不确定如何正确解决这个问题。我还在 C: lutter_app\lklk-live ndroid uild.gradle 中看到了更新插件的建议,但我已经将 kotlin_version 设置为最新版本(2.0.20)。
有人遇到过这个问题吗?如何解决 Kotlin 版本不兼容问题? Flutter(频道稳定,3.24.0,在 Microsoft Windows [版本 10.0.19045.2486],区域设置 en-US)
将此代码添加到您的
android/build.gradle
:
buildscript {
ext.kotlin_version = '2.0.20' //Your preferred Kotlin Version
repositories {
google()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}