Reputation: 7
I'm trying to build an android application using Firebase that demands two separate databases (teachers and students) in a single application. I searched for the solution all over the internet but all I got is this solution- Multiple Firebase projects in one app, which seems to be a great solution but I didn't understand.
So, how do I merge two firebase projects in a single application?
Upvotes: 0
Views: 2524
Reputation: 1361
You don't need to implement the communities solution that is described in that link anymore.
According to this:
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.
you need to first create a Firebase options object to hold the configuration data for the Firebase application, like this:
// Manually configure Firebase Options. The following fields are REQUIRED:
// - Project ID
// - App ID
// - API Key
FirebaseOptions options = new FirebaseOptions.Builder()
.setProjectId("my-firebase-project")
.setApplicationId("1:27992087142:android:ce3b6448250083d1")
.setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw")
// setDatabaseURL(...)
// setStorageBucket(...)
.build();
After you have initialized this options object, you can use it to configure an additional Firebase application instance.
For example, if you have another app instance named "otherApp", you can then do this:
// Initialize with secondary app
FirebaseApp.initializeApp(this /* Context */, options, "otherApp");
// Retrieve secondary FirebaseApp
FirebaseApp secondary = FirebaseApp.getInstance("otherApp");
By this way you connect to an alternative Realtime Database.
Upvotes: 0
Reputation: 801
Yes, you can follow that guide https://firebase.google.com/docs/projects/multiprojects#java.
Basically, you need to initiate the firebase app providing configuration manually
// [START firebase_options]
// Manually configure Firebase Options. The following fields are REQUIRED:
// - Project ID
// - App ID
// - API Key
FirebaseOptions options1 = new FirebaseOptions.Builder()
.setProjectId("my-firebase-project")
.setApplicationId("1:27992087142:android:ce3b6448250083d1")
.setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw")
// setDatabaseURL(...)
// setStorageBucket(...)
.build();
FirebaseOptions options2 = new FirebaseOptions.Builder()
.setProjectId("my-firebase-project")
.setApplicationId("1:27992087142:android:ce3b6448250083d1")
.setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw")
// setDatabaseURL(...)
// setStorageBucket(...)
.build();
// [END firebase_options]
// [START firebase_secondary]
// Initialize with secondary app
FirebaseApp.initializeApp(this /* Context */, options1, "first");
FirebaseApp.initializeApp(this /* Context */, options2, "secondary");
FirebaseApp first = FirebaseApp.getInstance("first");
FirebaseApp secondary = FirebaseApp.getInstance("secondary");
You can get all required data from google-service.json
Later you can get a database from that FirebaseApp
, for example
FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(secondary);
Then you just work with the project like before.
Upvotes: 2