Expo集成Firebase Analytics时提示‘Firebase is not configured’错误的配置排查咨询
Problem Description
I've run the command expo install expo-firebase-core expo-firebase-analytics to install dependencies, and downloaded google-services.json (for Android) and GoogleService-Info.plist (for iOS) from Firebase Console, placing them in my project root directory. However, when I call Analytics.logEvent, Expo Go throws an unhandled promise rejection error:
Possible Unhandled Promise Rejection (id: 0): Error: Firebase is not configured. Ensure that you have configured 'google-services.json' correctly
Here's my TopLevelComponent.js code:
import React from 'react' import * as Analytics from 'expo-firebase-analytics'; import { createRootNavigator } from './router' const RootNavigator = createRootNavigator() const TopLevelComponent = props => { const { screenProps } = props; const { checkLogin } = screenProps; const getActiveRouteName = navigationState => { if (!navigationState) { return null } const route = navigationState.routes[navigationState.index] // Parse the nested navigators if (route.routes) return getActiveRouteName(route) return route.routeName } return ( <RootNavigator onNavigationStateChange={async (prevState, currentState) => { const currentScreen = getActiveRouteName(currentState) const prevScreen = getActiveRouteName(prevState) if (prevScreen !== currentScreen) { checkLogin() Analytics.logEvent('event') } }} screenProps={props.screenProps} /> ); } export default TopLevelComponent
I'm using Expo SDK 44.0.6 and expo-firebase-analytics 6.0.1. Did I miss any configuration steps? Are there alternative ways to set up Firebase Analytics besides using these two config files?
Answer
Let's break down the issues and solutions step by step:
Expo Go doesn't support custom Firebase configuration
Expo Go is a pre-built universal app, which means it can't load your project-specificgoogle-services.jsonorGoogleService-Info.plistfiles. To use Firebase Analytics with your custom config, you need to either:- Use EAS Build to create a custom build of your app (this is the recommended approach for production)
- Run
expo prebuildto generate native project files, then build and run the app locally via Xcode/Android Studio
You need to reference config files in your Expo project config
Even if you place the config files in the root directory, Expo won't automatically detect them unless you specify their paths inapp.jsonorapp.config.js. Add these entries to your project config:{ "expo": { "android": { "googleServicesFile": "./google-services.json" }, "ios": { "googleServicesFile": "./GoogleService-Info.plist" } } }Double-check that the filenames and paths are exactly correct (case-sensitive on some systems).
Ensure Firebase is initialized before using Analytics
Whileexpo-firebase-coreusually handles initialization automatically, sometimes you need to explicitly initialize Firebase early in your app lifecycle. Add this to your root component (likeApp.js) before using Analytics:import * as FirebaseCore from 'expo-firebase-core'; // Inside your component's useEffect or at the top level FirebaseCore.initializeAsync();This ensures Firebase is ready before you call
Analytics.logEvent.Alternative: Configure Firebase via app config (no config files needed)
If you prefer not to use thegoogle-services.json/plistfiles, you can define your Firebase config directly inapp.config.js. Get the config values from Firebase Console (under Project Settings > General > Your apps > Firebase SDK snippet > Config) and add them like this:export default { expo: { // ... other expo config "firebase": { "apiKey": "YOUR_API_KEY", "authDomain": "YOUR_AUTH_DOMAIN", "projectId": "YOUR_PROJECT_ID", "storageBucket": "YOUR_STORAGE_BUCKET", "messagingSenderId": "YOUR_SENDER_ID", "appId": "YOUR_APP_ID", "measurementId": "YOUR_MEASUREMENT_ID" } } }This works with both managed and bare workflows, and avoids needing to manage the native config files.
Verify version compatibility
You're using Expo SDK 44 withexpo-firebase-analytics6.0.1, which is the correct version match (per Expo's SDK documentation). Just make sureexpo-firebase-coreis also on a version compatible with SDK 44 (it should be, since you installed it viaexpo installwhich pulls the right version).
If you still run into issues after trying these steps, try clearing your Expo cache with expo r -c and restarting the development server.
内容的提问来源于stack exchange,提问作者pgramaca

