Configure Multiple Projects

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.

Supporting 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 iOS (and their Unity and C++ wrappers) normally load configuration from a configuration file: GoogleService-Info.plist on iOS 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 iOS application

By default, FIRApp.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:

Target Membership panel

If the builds are part of a single target, the best option is to choose one environment as the default, and include the GoogleService-Info.plist file for that project. Then, you can rename the alternate GoogleService-Info.plist used by the other project to a unique filename, add that to your project, and then choose at runtime whether to load the default .plist file, or the alternate file. This is shown in the following example:

// Load a named file.
let filePath = Bundle.main.path(forResource: "MyGoogleService", ofType: "plist")
guard let fileopts = FIROptions.init(contentsOfFile: filePath)
  else { assert(false, "Couldn't load config file") }
FIRApp.configure(with: fileopts)

Support multiple environments in your Android application

In Android, the google-service.json file is processed into resource entries 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 types and product variation by placing google-services.json files in different directories named for the built variant nested under the module root. This is shown in the following example:

// development and release are build types.
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.

Because this provider is just reading resources under a known name, another option is to configure the resource values directly in your app instead of using the Google Services gradle plugin. You can do this by removing the following line from your build.gradle file and configuring the resource values directly in your app:

apply plugin: 'com.google.gms.google-services'

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:

The use of these classes to support multiple projects in an application is shown in the following examples:

iOS

// Configure with manual options.
guard let secondaryOptions = FIROptions.init(googleAppID: "1:27992087142:ios:2a4732a34787067a", bundleID: "com.google.firebase.devrel.FiroptionConfiguration", gcmSenderID: "27992087142", apiKey: "AIzaSyBicqfAZPvMgC7NZkjayUEsrepxuXzZDsk", clientID: "27992087142-ola6qe637ulk8780vl8mo5vogegkm23n.apps.googleusercontent.com", trackingID: nil, androidClientID: nil, databaseURL: "https://myproject.firebaseio.com", storageBucket: "myproject.appspot.com", deepLinkURLScheme: nil)
  else { assert(false, "Couldn't configure options") }

Android

// Manually configure Firebase Options
FirebaseOptions options = new FirebaseOptions.Builder()
        .setApplicationId("1:27992087142:android:ce3b6448250083d1") // Required for Analytics.
        .setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw") // Required for Auth.
        .setDatabaseUrl("https://myproject.firebaseio.com") // Required for RTDB.
        .build();

Web

var secondaryAppConfig = {
    apiKey: "<API_KEY>",
    authDomain: "<PROJECT_ID>.firebaseapp.com",
    databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
    storageBucket: "<BUCKET>.appspot.com",
};

Node.js

var admin = require("firebase-admin");

var serviceAccount = require("path/to/serviceAccountKey.json");

var secondaryAppConfig = {
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
};

Java

FirebaseOptions secondaryAppConfig = new FirebaseOptions.Builder()
  .setServiceAccount(new FileInputStream("path/to/serviceAccountKey.json"))
  .setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
  .build();

After you have initialized this options object, you can use it to configure an additional Firebase application instance. Note that in all the examples shown below we use the string secondary. This name is used to retrieve the application instance, and to distinguish it from other instances, including the default instance (named [DEFAULT]). You should pick a string appropriate to the intended use of the other Firebase project.

The following snippets demonstrate connecting to an alternative Realtime Database (the APIs for other Firebase features follow the same pattern).

iOS

// Configure an alternative FIRApp.
FIRApp.configure(withName: "secondary", options: secondaryOptions)

// Retrieve a previous created named app.
guard let secondary = FIRApp.init(named: "secondary")
  else { assert(false, "Could not retrieve secondary app") }


// Retrieve a Real Time Database client configured against a specific app.
let secondaryDb = FIRDatabase.database(app: secondary)

Android

// Initialize with secondary app.
FirebaseApp.initializeApp(this /* Context */, options, "secondary");

// Retrieve secondary app.
FirebaseApp secondary = FirebaseApp.getInstance("secondary");
// Get the database for the other app.
FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(secondary);

Web

// Initialize another app with a different config
var secondary = firebase.initializeApp(secondaryAppConfig, "secondary");

// Retrieve the database.
var secondaryDatabase = secondary.database();

Node.js

// Initialize another app with a different config
var secondary = firebase.initializeApp(secondaryAppConfig, "secondary");

// Retrieve the database.
var secondaryDatabase = secondary.database();

Java

// Initialize another app with a different config
FirebaseApp secondary = FirebaseApp.initializeApp(secondaryAppConfig, "secondary");

// Retrieve the database.
FirebaseDatabase otherDatabase = FirebaseDatabase.getInstance(otherApp);

Send feedback about...

Need help? Visit our support page.