Flutter集成Firebase时出现Gradle编译错误求助
Flutter Gradle编译错误:修改minSdkVersion后仍提示低于cloud_firestore要求的19
环境与问题概述
- 使用的Flutter依赖:
- firebase_messaging: ^14.2.5
- firebase_core: ^2.7.0
- flutter_local_notifications: ^8.2.0
- cloud_firestore: ^4.4.3
- 编译错误提示:项目minSdkVersion 16低于cloud_firestore要求的19,已尝试将
android/app/build.gradle中的minSdkVersion flutter.minSdkVersion改为minSdkVersion 21,但问题仍未解决。
错误日志
Launching lib\main.dart on sdk gphone64 x86 64 in debug mode... Running Gradle task 'assembleDebug'... G:\Porjects\Flutter\commerce_app\android\app\src\debug\AndroidManifest.xml Error: uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [:cloud_firestore] G:\Porjects\Flutter\commerce_app\build\cloud_firestore\intermediates\merged_manifest\debug\AndroidManifest.xml as the library might be using APIs not available in 16 Suggestion: use a compatible library with a minSdk of at most 16, or increase this project's minSdk version to at least 19, or use tools:overrideLibrary="io.flutter.plugins.firebase.firestore" to force usage (may lead to runtime failures) FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:processDebugMainManifest'. > Manifest merger failed : uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [:cloud_firestore] G:\Porjects\Flutter\commerce_app\build\cloud_firestore\intermediates\merged_manifest\debug\AndroidManifest.xml as the library might be using APIs not available in 16 Suggestion: use a compatible library with a minSdk of at most 16, or increase this project's minSdk version to at least 19, or use tools:overrideLibrary="io.flutter.plugins.firebase.firestore" to force usage (may lead to runtime failures) * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 5s ┌─ Flutter Fix ─────────────────────────────────────────────────────────────────────────────────┐ │ The plugin cloud_firestore requires a higher Android SDK version. │ │ Fix this issue by adding the following to the file │ │ G:\Porjects\Flutter\commerce_app\android\app\build.gradle: │ │ android { │ │ defaultConfig { │ │ minSdkVersion 21 │ │ } │ │ } │ │ │ │ Note that your app won't be available to users running Android SDKs below 19. │ │ Alternatively, try to find a version of this plugin that supports these lower versions of the │ │ Android SDK. │ │ For more information, see: │ │ https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration │ └───────────────────────────────────────────────────────────────────────────────────────────────┘ Exception: Gradle task assembleDebug failed with exit code 1
解决方案
1. 检查并修改全局minSdk配置
很多Flutter项目会在android/build.gradle中统一定义minSdkVersion变量,仅修改app模块的配置可能不生效:
打开android/build.gradle,找到ext代码块,修改minSdkVersion为21:
buildscript { ext { minSdkVersion = 21 compileSdkVersion = 33 targetSdkVersion = 33 } // 其他配置... }
确保android/app/build.gradle中的minSdkVersion引用这个变量,或者直接写死21:
android { defaultConfig { minSdkVersion 21 // 或 minSdkVersion rootProject.ext.minSdkVersion // 其他配置... } }
2. 清理构建缓存并重新编译
缓存残留可能导致配置修改不生效,执行以下操作:
- 在Android Studio中点击
File > Invalidate Caches... > Invalidate and Restart - 或者在项目根目录执行命令:
flutter clean flutter pub get cd android && gradlew clean cd .. && flutter run
3. 排查其他依赖的minSdk冲突
部分第三方插件可能会强制指定minSdk版本,执行flutter pub deps查看完整依赖树,确认是否有其他插件要求更低的minSdk,导致合并manifest时冲突。如果有,要么升级该插件版本,要么统一提升项目minSdk到所有依赖要求的最低值。
4. 临时使用overrideLibrary(不推荐)
如果必须保留低版本minSdk,可以通过强制覆盖来绕过检查,但可能导致运行时API兼容错误:
打开android/app/src/main/AndroidManifest.xml,先添加tools命名空间:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
然后添加override配置:
<uses-sdk android:minSdkVersion="16" tools:overrideLibrary="io.flutter.plugins.firebase.firestore"/>
内容的提问来源于stack exchange,提问作者Ahmed Maged
相关产品推荐
相关产品推荐

