Flutter打包APK报错:Kotlin元数据版本不兼容及资源缺失
解决Flutter APK构建失败的两类关键问题
问题概述
构建Flutter APK时触发两类核心错误:
- 多个模块提示:
Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.7.1, expected version is 1.5.1 - Android资源链接失败:
error: resource android:attr/lStar not found
错误原因及解决方案
1. Kotlin版本不兼容问题
原因:依赖模块使用Kotlin 1.7.x编译,但项目Gradle插件版本与Kotlin版本不匹配,导致编译时版本校验失败。
解决步骤:
- 更新项目根目录
android/build.gradle中的Gradle插件版本,使其适配Kotlin 1.7.x:buildscript { ext.kotlin_version = '1.7.10' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.4.2' // 升级至匹配Kotlin 1.7.x的版本 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms:google-services:4.3.10' } } - 清理缓存后重新构建:
flutter clean cd android && ./gradlew clean cd .. && flutter build apk
2. Android资源链接失败(lStar属性缺失)
原因:android:attr/lStar是Android 13(API 33)新增属性,部分依赖库(如core-1.9.0)引入该属性,但项目部分模块的编译配置未适配API 33。
解决步骤:
- 在
android/app/build.gradle中统一compileSdkVersion和targetSdkVersion为33:android { compileSdkVersion 33 defaultConfig { minSdkVersion 21 targetSdkVersion 33 // 升级至API 33 applicationId "org.mycwc" versionCode flutterVersionCode.toInteger() versionName flutterVersionName multiDexEnabled true } } - 若仍有问题,强制依赖兼容版本的core库:
android { // 其他配置... configurations.all { resolutionStrategy { force 'androidx.core:core-ktx:1.8.0' // 规避含lStar属性的高版本core库 } } }
修改后的完整配置示例
根目录android/build.gradle
buildscript { ext.kotlin_version = '1.7.10' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.4.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms:google-services:4.3.10' } } allprojects { repositories { google() mavenCentral() } }
app模块android/app/build.gradle
android { compileSdkVersion 33 kotlinOptions { jvmTarget = '1.8' } sourceSets { main.java.srcDirs += 'src/main/kotlin' } defaultConfig { applicationId "org.mycwc" minSdkVersion 21 targetSdkVersion 33 versionCode flutterVersionCode.toInteger() versionName flutterVersionName multiDexEnabled true } configurations.all { resolutionStrategy { force 'androidx.core:core-ktx:1.8.0' } } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" }
内容的提问来源于stack exchange,提问作者Deepak
相关产品推荐
相关产品推荐

