构建Flutter发布版APK时遭遇Kotlin版本不匹配错误
解决Flutter发布版APK构建时的Kotlin版本不匹配错误
构建Flutter应用发布版APK时,出现Kotlin版本不兼容错误:部分依赖模块使用Kotlin 1.8.0编译,但项目预期的Kotlin版本为1.6.0。完整错误日志如下:
Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. Font asset "MaterialIcons-Regular.otf" was tree-shaken, reducing it from 1645184 to 4768 bytes (99.7% reduction). Tree-shaking can be disabled by providing the --no-tree-shake-icons flag when building your app. e: C:/Users/Isha/.gradle/caches/transforms-3/8c41ed3860a273958155a41f357a07e9/transformed/jetified-activity-1.7.2/jars/classes.jar!/META-INF/activity_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. e: C:/Users/Isha/.gradle/caches/transforms-3/d645703c60279586ad1c1d11764fe8ad/transformed/lifecycle-livedata-2.6.1/jars/classes.jar!/META-INF/lifecycle-livedata_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. e: C:/Users/Isha/.gradle/caches/transforms-3/a3a8030c267591e301080bb0745fd6fa/transformed/lifecycle-viewmodel-2.6.1/jars/classes.jar!/META-INF/lifecycle-viewmodel_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. e: C:/Users/Isha/.gradle/caches/transforms-3/cfc05146a65b906361abd9a0d0aeb398/transformed/lifecycle-livedata-core-2.6.1/jars/classes.jar!/META-INF/lifecycle-livedata-core_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. e: C:/Users/Isha/.gradle/caches/transforms-3/95f6396d570a41c31db3d4c0c454d290/transformed/jetified-lifecycle-viewmodel-savedstate-2.6.1/jars/classes.jar!/META-INF/lifecycle-viewmodel-savedstate_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. e: C:/Users/Isha/.gradle/caches/transforms-3/3c10a5b904a5e328c6703093db7674ef/transformed/jetified-core-ktx-1.10.1/jars/classes.jar!/META-INF/core-ktx_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. e: C:/Users/Isha/.gradle/caches/transforms-3/d06854d15b1bcfc7d8e0fc1e94cc2f00/transformed/core-1.10.1/jars/classes.jar!/META-INF/core_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. e: C:/Users/Isha/.gradle/caches/transforms-3/6ba7836a74be5ea1a94299c533dbc1a3/transformed/lifecycle-runtime-2.6.1/jars/classes.jar!/META-INF/lifecycle-runtime_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. e: C:/Users/Isha/.gradle/caches/modules-2/files-2.1/androidx.lifecycle/lifecycle-common/2.6.1/10f354fdb64868baecd67128560c5a0d6312c495/lifecycle-common-2.6.1.jar!/META-INF/lifecycle-common.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. e: C:/Users/Isha/.gradle/caches/transforms-3/268dca3e65a9a8099f7954be0cd7a76d/transformed/jetified-savedstate-1.2.1/jars/classes.jar!/META-INF/savedstate_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. e: C:/Users/Isha/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.8.22/636bf8b320e7627482771bbac9ed7246773c02bd/kotlin-stdlib-1.8.22.jar!/META-INF/kotlin-stdlib-jdk7.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. e: C:/Users/Isha/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.8.22/636bf8b320e7627482771bbac9ed7246773c02bd/kotlin-stdlib-1.8.22.jar!/META-INF/kotlin-stdlib.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. e: C:/Users/Isha/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.8.22/636bf8b320e7627482771bbac9ed7246773c02bd/kotlin-stdlib-1.8.22.jar!/META-INF/kotlin-stdlib-jdk8.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. e: C:/Users/Isha/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.8.22/1a8e3601703ae14bb58757ea6b2d8e8e5935a586/kotlin-stdlib-common-1.8.22.jar!/META-INF/kotlin-stdlib-common.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'... 252.3s √ Built build\app\outputs\flutter-apk\app-release.apk (39.7MB).
当前配置
android/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-wrapper.properties
#Fri May 31 12:56:29 PKT 2024 distributionBase=GRADLE_USER_HOME distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip distributionPath=wrapper/dists zipStorePath=wrapper/dists zipStoreBase=GRADLE_USER_HOME
解决方案
1. 统一项目Kotlin版本
在android/build.gradle文件顶部添加buildscript块,指定与依赖匹配的Kotlin版本(这里用1.8.22,和错误日志中出现的kotlin-stdlib版本一致):
修改后的android/build.gradle:
buildscript { ext.kotlin_version = '1.8.22' repositories { google() mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:7.4.2" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } 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 }
2. 清理Gradle缓存
- 执行Flutter清理命令:
flutter clean - 删除本地Gradle缓存目录:
C:/Users/Isha/.gradle/caches
3. 重新构建发布版APK
执行构建命令:
flutter build apk --release
内容的提问来源于stack exchange,提问作者Isha Bhutto
相关产品推荐
相关产品推荐

