添加firebase_core依赖后Flutter应用无法运行,请求协助
问题:添加Firebase依赖后Flutter Android构建失败
未添加firebase_core时应用运行正常,使用flutter pub add命令添加firebase_core或firebase_auth后,构建出现错误:Gradle任务:firebase_core:extractDebugAnnotations执行失败,无法加载groovy-json的模块元数据。
相关配置文件
android/app/build.gradle
def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { localPropertiesFile.withReader('UTF-8') { reader -> localProperties.load(reader) } } def flutterRoot = localProperties.getProperty('flutter.sdk') if (flutterRoot == null) { throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") } def flutterVersionCode = localProperties.getProperty('flutter.versionCode') if (flutterVersionCode == null) { flutterVersionCode = '1' } def flutterVersionName = localProperties.getProperty('flutter.versionName') if (flutterVersionName == null) { flutterVersionName = '1.0' } apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { compileSdkVersion flutter.compileSdkVersion ndkVersion flutter.ndkVersion compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } 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.myapp" // You can update the following values to match your application needs. // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration. // minSdkVersion flutter.minSdkVersion minSdkVersion 21 targetSdkVersion flutter.targetSdkVersion // targetSdkVersion 31 versionCode flutterVersionCode.toInteger() versionName flutterVersionName } 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 "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" }
main.dart
import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/material.dart'; import 'package:myapp/firebase_options.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: const MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({Key? key, required this.title}) : super(key: key); 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( 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.headline4, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment', child: const Icon(Icons.add), ), // This trailing comma makes auto-formatting nicer for build methods. ); } }
构建错误日志
Launching lib\main.dart on Redmi Note 7 Pro in debug mode... FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':firebase_core:extractDebugAnnotations'. > Could not resolve all dependencies for configuration ':firebase_core:detachedConfiguration2'. > Could not load module metadata from C:\Users\Sagar\.gradle\caches\modules-2\metadata-2.97\descriptors\org.codehaus.groovy\groovy-json\3.0.8\a8be1fe3b3911d3d3425fe720cf42835\descriptor.bin * 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 9m 31s Running Gradle task 'assembleDebug'... 583.5s Exception: Gradle task assembleDebug failed with exit code 1
解决方案
这类问题大多由Gradle缓存损坏或依赖兼容性问题导致,按以下步骤处理:
清理缓存
- 删除本地Gradle缓存目录:
C:\Users\你的用户名\.gradle\caches - 回到项目根目录,执行命令清理Flutter缓存并重新获取依赖:
flutter clean flutter pub get
- 删除本地Gradle缓存目录:
更新Gradle版本
- 打开项目根目录下的
android/build.gradle,升级Gradle插件和Kotlin版本到稳定版,示例:buildscript { ext.kotlin_version = '1.8.0' 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.15' } } - 修改
android/gradle/wrapper/gradle-wrapper.properties中的Gradle分发地址:distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
- 打开项目根目录下的
固定Firebase依赖版本
- 在
pubspec.yaml中指定与当前Flutter SDK兼容的Firebase依赖版本,示例:dependencies: flutter: sdk: flutter firebase_core: ^2.15.0 firebase_auth: ^4.10.0
- 在
重新构建
- 执行
flutter run重新构建项目;若仍报错,在Android Studio中选择File -> Invalidate Caches... -> 勾选Invalidate and Restart重启IDE。
- 执行
内容的提问来源于stack exchange,提问作者Sameer P
相关产品推荐
相关产品推荐

