Flutter集成Firebase遇Gradle构建异常,寻求解决方案
解决Flutter集成Firebase的Gradle异常问题
尝试在Flutter应用中集成Firebase,但运行时一直抛出Gradle异常错误。以下是相关文件内容:
android/app/build.gradle 文件内容
plugins { id "com.android.application" id "kotlin-android" // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins. id "dev.flutter.flutter-gradle-plugin" id 'com.google.gms.google-services' } def localProperties = new Properties() def localPropertiesFile = rootProject.file("local.properties") if (localPropertiesFile.exists()) { localPropertiesFile.withReader("UTF-8") { reader -> localProperties.load(reader) } } def flutterVersionCode = localProperties.getProperty("flutter.versionCode") if (flutterVersionCode == null) { flutterVersionCode = "1" } def flutterVersionName = localProperties.getProperty("flutter.versionName") if (flutterVersionName == null) { flutterVersionName = "1.0" } android { namespace "com.example.firebase_one" compileSdkVersion flutter.compileSdkVersion ndkVersion flutter.ndkVersion compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 compileSdkVersion 34 } kotlinOptions { jvmTarget = '1.8' } sourceSets { main.java.srcDirs += 'src/main/kotlin' } defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId = "com.example.firebase_one" // You can update the following values to match your application needs. // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration. minSdk = flutter.minSdkVersion targetSdk = flutter.targetSdkVersion versionCode = flutterVersionCode.toInteger() versionName = flutterVersionName multidexEnabled = true } buildTypes { release { // TODO: Add your own signing config for the release build. // Signing with the debug keys for now, so `flutter run --release` works. signingConfig = signingConfigs.debug } } } flutter { source = "../.." } dependencies { implementation platform('com.google.firebase:firebase-bom:33.1.0') }
android/build.gradle 文件内容
buildscript { ext.kotlin_version = '1.7.10' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.2.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms.google-services:4.4.2' } } 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 }
main.dart 文件内容
import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/material.dart'; void main() async{ WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), home: const MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title; @override State<MyHomePage> createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { int _counter = 0; void _incrementCounter() { setState(() { _counter++; }); } @override Widget build(BuildContext context) { // return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ const Text( 'You have pushed the button this many times:', ), Text( '$_counter', style: Theme.of(context).textTheme.headlineMedium, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment', child: const Icon(Icons.add), ), // This trailing comma makes auto-formatting nicer for build methods. ); } }
可能的解决方案
修复compileSdkVersion重复定义冲突
在android/app/build.gradle的android块中,compileSdkVersion被重复定义了两次(外层和compileOptions内),删除compileOptions中的重复行:compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 // 移除该行:compileSdkVersion 34 }确认Firebase配置文件存在
检查android/app目录下是否存在从Firebase控制台下载的google-services.json文件,缺少该文件会导致初始化失败。更新Gradle相关依赖版本
当前使用的Gradle插件和Google Services插件版本可能存在兼容问题,更新为匹配版本:
修改android/build.gradle中的依赖: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.4.10' }同时更新
gradle/wrapper/gradle-wrapper.properties中的Gradle版本,确保与插件兼容:distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip添加Firebase Core显式依赖
虽然引入了Firebase BOM,仍需显式添加Core依赖保证初始化正常,在android/app/build.gradle的dependencies块中补充:implementation platform('com.google.firebase:firebase-bom:33.1.0') implementation 'com.google.firebase:firebase-core'清理缓存并重新构建
执行以下命令清理项目缓存后重新构建:flutter clean flutter pub get cd android && ./gradlew clean build cd .. && flutter run
内容的提问来源于stack exchange,提问作者Starc
相关产品推荐
相关产品推荐

