You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

迁移Flutter Gradle插件后,如何修复FirebaseOptions加载失败错误?

如何修复错误:‘Failed to load FirebaseOptions from resource. Check that you have defined values.xml correctly.’?

错误日志

E/flutter ( 6571): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(java.lang.Exception: Failed to load FirebaseOptions from resource. Check that you have defined values.xml correctly., Exception, Cause: null, Stacktrace: java.lang.Exception: Failed to load FirebaseOptions from resource. Check that you have defined values.xml correctly.
E/flutter ( 6571):  at io.flutter.plugins.firebase.core.FlutterFirebaseCorePlugin.lambda$optionsFromResource$4$io-flutter-plugins-firebase-core-FlutterFirebaseCorePlugin(FlutterFirebaseCorePlugin.java:207)
E/flutter ( 6571):  at io.flutter.plugins.firebase.core.FlutterFirebaseCorePlugin$$ExternalSyntheticLambda2.run(Unknown Source:4)
E/flutter ( 6571):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
E/flutter ( 6571):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
E/flutter ( 6571):  at java.lang.Thread.run(Thread.java:1012)
E/flutter ( 6571): , null)
E/flutter ( 6571): #0      FirebaseCoreHostApi.optionsFromResource (package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart:242:7)
E/flutter ( 6571): <asynchronous suspension>
E/flutter ( 6571): #1      MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:89:25)
E/flutter ( 6571): <asynchronous suspension>
E/flutter ( 6571): #2      Firebase.initializeApp (package:firebase_core/src/firebase.dart:43:31)

问题背景

调用await Firebase.initializeApp();后立即抛出该错误。问题出现在移除已弃用的Flutter Gradle插件imperative apply方式,迁移至新的声明式apply方式之后,创建新项目测试时也遇到相同问题。应用仅针对iOS和Android平台构建,不包含Web平台。环境:firebase_core: ^2.27.1


修复方案

1. 检查Android端values.xml配置

  • 打开android/app/src/main/res/values/values.xml文件,确认是否包含完整的Firebase配置参数,格式参考:
    <resources>
        <!-- 其他原有配置 -->
        <string name="firebase_api_key" translatable="false">你的Firebase API密钥</string>
        <string name="firebase_app_id" translatable="false">你的Firebase应用ID</string>
        <string name="firebase_messaging_sender_id" translatable="false">你的消息发送ID</string>
        <string name="firebase_project_id" translatable="false">你的Firebase项目ID</string>
        <string name="firebase_storage_bucket" translatable="false">你的存储桶地址</string>
    </resources>
    
  • 若缺少上述参数,从Firebase控制台重新下载google-services.json文件,将其放置在android/app/目录下(声明式Gradle配置仍依赖该文件自动注入配置),或手动提取文件中的对应字段补充到values.xml。

2. 验证Gradle声明式配置正确性

  • 检查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
        }
        settings.ext.flutterSdkPath = flutterSdkPath()
    
        includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle")
    
        repositories {
            google()
            mavenCentral()
            gradlePluginPortal()
        }
    }
    
    plugins {
        id "dev.flutter.flutter-plugin-loader" version "1.0.0"
        id "com.android.application" version "7.3.0" apply false
        id "org.jetbrains.kotlin.android" version "1.7.10" apply false
        id "com.google.gms.google-services" version "4.4.0" apply false
    }
    
    include ":app"
    
  • 检查android/app/build.gradle文件,确保顶部已应用必要插件:
    plugins {
        id "com.android.application"
        id "kotlin-android"
        id "dev.flutter.flutter-gradle-plugin"
        id "com.google.gms.google-services"
    }
    

3. 手动初始化Firebase(绕过资源加载)

若配置无误仍报错,可在代码中手动传入FirebaseOptions,替代从资源加载的方式:

import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart'; // 可通过`flutterfire configure`命令生成该文件

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  runApp(MyApp());
}
  • 若未生成firebase_options.dart,可临时硬编码参数(生产环境不推荐):
await Firebase.initializeApp(
  options: FirebaseOptions(
    apiKey: "你的Firebase API密钥",
    appId: "你的Firebase应用ID",
    messagingSenderId: "你的消息发送ID",
    projectId: "你的Firebase项目ID",
    storageBucket: "你的存储桶地址",
  ),
);

4. 清理缓存并重建项目

执行以下命令清除缓存后重新构建:

flutter clean
cd android && ./gradlew clean
cd .. && flutter pub get
flutter run

内容的提问来源于stack exchange,提问作者sanjay c

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 05:44:54