This page describes how to use more than one Firebase project in your app.
Many apps need only a single Firebase project and the default set up described in the Get Started guides. Examples of when it can be useful to use multiple Firebase projects include:
- Setting up your development environment to use different Firebase projects based on build type or target.
- Accessing the content from multiple Firebase projects in your app.
Support different environments
One common use case is to support separate Firebase projects for your development and production environments.
The Web and Admin SDKs are configured by directly passing values to their initialization functions. For these SDK, you can use a runtime check to select development or production configuration variables.
Android and Apple platforms (and their Unity and C++ wrappers) normally load
configuration from a configuration file: GoogleService-Info.plist
on Apple
platform and google-services.json
on Android. These files are read into an
options object ( FIROption
or FirebaseOptions
) that is referenced by the
Firebase application object ( FIRApp
or FirebaseApp
).
For these platforms, switching between environments is usually implemented as a build time decision, through use of different configuration files for each environment.
Support multiple environments in your Apple application
By default, FirebaseApp.configure()
will load the GoogleService-Info.plist
file
bundled with the application. If your development and production environments
are configured as separate targets in Xcode, you can:
- Download both
GoogleService-Info.plist
files - Store the two files in different directories
- Add both to your Xcode project
- Associate the different files with the different targets using the Target Membership panel:
If the builds are part of a single target, the best option is to give both
configuration files unique names (e.g. GoogleService-Info-Free.plist
and GoogleService-Info-Paid.plist
). Then choose at runtime which plist to load.
This is shown in the following example:
// Load a named file . let filePath = Bundle . main . path ( forResource : " MyGoogleService " , ofType : " plist " ) guard let fileopts = FirebaseOptions ( contentsOfFile : filePath ! ) else { assert ( false , " Couldn't load config file " ) } FirebaseApp . configure ( options : fileopts )
Support multiple environments in your Android application
In Android, the google-services.json
file is processed into Android string
resources by the Google Services gradle plugin. You can see
which resources are created in the Google Services Plugin documentation on Processing the JSON file
.
You can have multiple google-services.json
files for different build variants
by placing google-services.json
files in dedicated directories named for each
variant under the app module root. For example, if you have "development" and
"release" build flavors, your configuration could be organized like this:
app/
google-services.json
src/development/google-services.json
src/release/google-services.json
...
To learn more, see the Google Services Plugin documentation on Adding the JSON file .
These resources are then loaded by the FirebaseInitProvider , which runs before your application code and initializes Firebase APIs using those values.
Because this provider is just reading resources with known names, another option is to add the string resources directly to your app instead of using the Google Services gradle plugin. You can do this by:
- Removing the
google-services
plugin from your rootbuild.gradle
- Deleting the
google-services.json
from your project - Adding the string resources directly
- Deleting
apply plugin: 'com.google.gms.google-services'
from your appbuild.gradle
Use multiple projects in your application
Sometimes you need to access different projects using the same APIs - for example, accessing multiple database instances. In most cases there is a central Firebase application object that manages the configuration for all the Firebase APIs. This object is initialized as part of your normal setup. However, when you want to access multiple projects from a single application, you’ll need a distinct Firebase application object to reference each one individually. It’s up to you to initialize these other instances.
In both cases, you need to first create a Firebase options object to hold the configuration data for the Firebase application. Full documentation for the options can be found in the API reference documentation for the following classes:
- Swift:
FirebaseOptions(googleAppID:gcmSenderID:)
- Android:
FirebaseOptions.Builder
- Web:
initializeApp()
- C++:
firebase::App::Create
- Unity:
FirebaseApp.Create
- Node.js:
initializeApp
- Java:
FirebaseOptions.Builder
The use of these classes to support multiple projects in an application is shown in the following examples:
Swift
// Configure with manual options. Note that projectID and apiKey, though not // required by the initializer, are mandatory. let secondaryOptions = FirebaseOptions ( googleAppID : " 1 : 27992087142 : ios : 2 a4732a34787067a " , gcmSenderID : " 27992087142 " ) secondaryOptions . apiKey = " AIzaSyBicqfAZPvMgC7NZkjayUEsrepxuXzZDsk " secondaryOptions . projectID = " projectid - 12345 " // The other options are not mandatory, but may be required // for specific Firebase products. secondaryOptions . bundleID = " com . google . firebase . devrel . FiroptionConfiguration " secondaryOptions . trackingID = " UA - 12345678 - 1 " secondaryOptions . clientID = " 27992087142 - ola6qe637ulk8780vl8mo5vogegkm23n . apps . googleusercontent . com " secondaryOptions . databaseURL = " https : //myproject.firebaseio.com " secondaryOptions . storageBucket = " myproject . appspot . com " secondaryOptions . androidClientID = " 12345. apps . googleusercontent . com " secondaryOptions . deepLinkURLScheme = " myapp : // " secondaryOptions . storageBucket = " projectid - 12345. appspot . com " secondaryOptions . appGroupID = nil . swift