Flutter打包Release版本时Kotlin版本不兼容问题求助
Flutter Release构建报错:Kotlin版本不兼容
构建Flutter应用的Release版本时出现Kotlin版本不兼容错误,多个依赖模块(如in_app_update、screen_brightness_android等)及Jetpack组件的kotlin_module元数据二进制版本为1.9.0/1.8.0,但构建工具预期版本为1.5.1。Debug版本可正常运行。
错误日志
e: /home/muhdroid/Documents/Projects Education/abd_el_wahab_hegazy/build/in_app_update/.transforms/44939e9527cbf0759b903aa9f64015de/transformed/out/jars/classes.jar!/META-INF/in_app_update_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.5.1. e: /home/muhdroid/Documents/Projects Education/abd_el_wahab_hegazy/build/screen_brightness_android/.transforms/1e8fa44b7ebdc12cb8742c012a2b08c4/transformed/out/jars/classes.jar!/META-INF/screen_brightness_android_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.5.1. e: /home/muhdroid/Documents/Projects Education/abd_el_wahab_hegazy/build/wakelock_plus/.transforms/f6dd81657896465a3658863068a12742/transformed/out/jars/classes.jar!/META-INF/wakelock_plus_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.5.1. e: /home/muhdroid/Documents/Projects Education/abd_el_wahab_hegazy/build/volume_controller/.transforms/62b41e1f923cc855df2d35320ec5f6f3/transformed/out/jars/classes.jar!/META-INF/volume_controller_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.5.1. e: /home/muhdroid/Documents/Projects Education/abd_el_wahab_hegazy/build/package_info_plus/.transforms/9c9d2fad3b50b2bd23b3490a45f5d32f/transformed/out/jars/classes.jar!/META-INF/package_info_plus_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.5.1. e: /home/muhdroid/Documents/Projects Education/abd_el_wahab_hegazy/build/device_info_plus/.transforms/ccd2969d2b3501b88662c7ce2707f464/transformed/out/jars/classes.jar!/META-INF/device_info_plus_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.5.1. e: /home/muhdroid/.gradle/caches/transforms-3/abb0272b3c6600cd73455647d3d73261/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.5.1. e: /home/muhdroid/.gradle/caches/transforms-3/a5acbf2c50e89562ee6b04491c773b80/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.5.1. e: /home/muhdroid/.gradle/caches/transforms-3/2b71c9bf05dde48913d902212b8ef681/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.5.1. e: /home/muhdroid/.gradle/caches/transforms-3/d2bd0f9435a9f516bd463d5c9d4ede4b/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.5.1. e: /home/muhdroid/.gradle/caches/transforms-3/2e60c8de69dbcbad8db906e51762afa6/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.5.1.
项目build.gradle配置
buildscript { ext.kotlin_version = '1.9.0' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.1.2' // START: FlutterFire Configuration classpath 'com.google.gms:google-services:4.3.10' // END: FlutterFire Configuration 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 }
解决方法
1. 统一子模块的Kotlin版本配置
问题核心是子模块未继承根项目的Kotlin版本,导致Release构建时版本不匹配。修改根项目build.gradle的subprojects块,强制子模块使用根项目指定的Kotlin版本:
subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" // 添加以下配置 project.ext { kotlin_version = rootProject.ext.kotlin_version } tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach { kotlinOptions { jvmTarget = '1.8' apiVersion = rootProject.ext.kotlin_version.split('.')[0..1].join('.') languageVersion = rootProject.ext.kotlin_version.split('.')[0..1].join('.') } } }
2. 匹配Android Gradle插件与Kotlin版本兼容性
当前使用的AGP 7.1.2兼容的Kotlin版本范围是1.6.x~1.7.x,而你使用的Kotlin 1.9.0版本跨度太大,需调整为匹配版本:
修改根build.gradle中的ext.kotlin_version为1.7.20:
ext.kotlin_version = '1.7.20'
3. 清理缓存并重新构建
执行以下命令清除旧构建缓存,避免干扰:
flutter clean rm -rf ~/.gradle/caches flutter build apk --release
额外检查
若上述方法无效,查看android/app/build.gradle中是否单独指定了Kotlin版本,如有则删除该配置,确保继承根项目的版本设置。
内容的提问来源于stack exchange,提问作者muhdroid
相关产品推荐
相关产品推荐

