Flutter集成Firebase构建APK遇Kotlin依赖冲突及黑屏问题
解决Flutter Firebase项目构建APK的Kotlin版本冲突与黑屏问题
核心问题分析
- Kotlin依赖冲突:项目同时引入kotlin-stdlib 1.7.10和1.8.22版本,触发
checkReleaseDuplicateClasses任务失败。 - 版本兼容性不匹配:修改Kotlin/AGP版本后未同步对应依赖,导致运行时兼容性异常,引发APP黑屏。
- 配置文件错误:你提供的
(app) build.gradle内容与settings.gradle重复,属于无效配置,需修正。
分步解决方案
1. 统一Kotlin插件与依赖版本
在settings.gradle指定兼容的Kotlin插件版本,同时在app/build.gradle强制所有依赖使用统一的kotlin-stdlib版本:
修改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" // 适配Gradle 7.6.3,选择兼容Kotlin 1.9.x的AGP版本 id "com.android.application" version "7.4.2" apply false id "com.google.gms.google-services" version "4.4.0" apply false // 使用与AGP兼容的稳定版Kotlin插件 id "org.jetbrains.kotlin.android" version "1.9.20" apply false } include ":app"
修正app/build.gradle(替换你当前重复的无效内容):
plugins { id "com.android.application" id "kotlin-android" id "dev.flutter.flutter-gradle-plugin" id "com.google.gms.google-services" } android { namespace "你的应用包名" compileSdk 34 defaultConfig { applicationId "你的应用包名" minSdk 21 targetSdk 34 versionCode 1 versionName "0.1.0" } buildTypes { release { signingConfig signingConfigs.debug // 正式打包请替换为自定义签名配置 minifyEnabled false shrinkResources false } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = "1.8" } } flutter { source "../.." } dependencies { // 引入Kotlin BOM统一管理版本 implementation platform("org.jetbrains.kotlin:kotlin-bom:1.9.20") implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" // Firebase依赖通过BOM统一版本(按需添加) implementation platform("com.google.firebase:firebase-bom:32.7.0") } // 强制所有依赖使用指定Kotlin标准库版本,解决冲突 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" } }
2. 验证Gradle与AGP版本兼容性
你的Gradle版本为7.6.3,与AGP 7.4.2完全兼容,无需修改gradle-wrapper.properties。
3. 清理缓存并重新构建
执行以下命令彻底清理旧缓存,避免依赖残留:
flutter clean rm -rf ~/.gradle/caches flutter build apk --release
关键说明
- 版本对应逻辑:AGP 7.x系列兼容Kotlin 1.7.x-1.9.x,选择1.9.20是因为它是稳定版且与Firebase依赖兼容性更佳。
- 强制版本的作用:通过
resolutionStrategy.force可迫使所有第三方依赖(包括Firebase)使用指定的Kotlin标准库版本,彻底消除重复类问题。 - 黑屏修复原理:版本不匹配会导致Flutter引擎与Android原生层通信异常,统一版本后即可解决运行时兼容性问题。
内容的提问来源于stack exchange,提问作者Ali
相关产品推荐
相关产品推荐

