How to connect to more than one firebase database from an android App
You'll need to initialize a second FirebaseApp
object with explicit options in your code:
FirebaseOptions options = new FirebaseOptions.Builder()
.setApiKey("AI...j0")
.setApplicationId("1:5...e0")
.setDatabaseUrl("https://myapp.firebaseio.com")
.build();
FirebaseApp secondApp = FirebaseApp.initializeApp(getApplicationContext(), options, "second app");
FirebaseDatabase secondDatabase = FirebaseDatabase.getInstance(secondApp);
secondDatabase.getReference().setValue(ServerValue.TIMESTAMP);
I got the configuration values from the second project's google-services.json. The API Key is under a property called api_key
, the Application ID came from a property called mobilesdk_app_id
and the database URL came from a property called firebase_url
.
Also see the documentation on using multiple projects in your application.
Is there any way to connect Firebase database with 2 projects?
One Firebase project can service many individual applications, including any mix of Android, iOS, and web based apps.
I suggest reading this page of documentation for more detailed explanations of what Firebase project really is.
Can we Use Multiple Database in a same Android Studio Project?
Yes you can. I did that recently in a project. Firebase is very easy just check the documentation.
You can actually use Firebase for everything. including storing and retrieving data. but, if you want to use another online DB, Web server or API that's totally doable.
I would recommend that you do a separate demo project using firebase and another Retrofit for example.
How to use two firebase projects in single Android app at same time?
Nothing to change your default app. Connect your app to the main firebase project normally. Also, register your app with the second firebase project. Now instead of importing the google-services.json
file to the android project directly create a Singleton class like below and replace it with your second firebase project details.
public class MySecondaryProject {
private static FirebaseApp INSTANCE;
public static FirebaseApp getInstance(Context context) {
if (INSTANCE == null) {
INSTANCE = getSecondProject(context);
}
return INSTANCE;
}
private static FirebaseApp getSecondProject(Context context) {
FirebaseOptions options1 = new FirebaseOptions.Builder()
.setApiKey("AI...gs")
.setApplicationId("1:5...46")
.setProjectId("my-project-id")
// setDatabaseURL(...) // in case you need firebase Realtime database
// setStorageBucket(...) // in case you need firebase storage MySecondaryProject
.build();
FirebaseApp.initializeApp(context, options1, "admin");
return FirebaseApp.getInstance("admin");
}
}
Now wherever you need this second project instance you can use like below.
FirebaseFirestore db = FirebaseFirestore.getInstance(); //- DEFAULT PROJECT
FirebaseFirestore secondDB = FirebaseFirestore.getInstance(MySecondaryProject.getInstance(this)); // - SECOND PROJECT
You must need to follow the singleton pattern to avoid duplicate instances otherwise the app will crash. you can use both the instances simulteneously.
For more details some useful links
- Official Documentation
- Some Stackoverflow questions
- some other source form itzone.com.vn
I think it will be the perfect answer of your question.
Can I use two Firebase project database in one single android app?
It is possible. When using just a single database there is a single FirebaseApp object that you don't have to explicitly handle. If you want to connect to another database, you would simply configure that object manually, like this:
FirebaseOptions options = new FirebaseOptions.Builder()
.setApplicationId("YOUR_APP_ID") // Required for Analytics.
.setApiKey("YOUR_API_KEY") // Required for Auth.
.setDatabaseUrl("https://YOUR_FIREBASE_URL.firebaseio.com/") // Required for RTDB.
.build();
FirebaseApp.initializeApp(this /* Context */, options, "secondary");
Then you can access your app by passing in the name you set for the app, then pass that in the request for the database instance:
FirebaseApp app = FirebaseApp.getInstance("secondary");
FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(app);
To be clear, you will still be able to access the other database in the standard way i.e.:
FirebaseDatabase database = FirebaseDatabase.getInstance();
Related Topics
How to Handle Dynamic Json in Retrofit
Android Camera: Data Intent Returns Null
Where Is Debug.Keystore in Android Studio
How to Install Trusted Ca Certificate on Android Device
How to Underline Text in an Android Layout
Disable Scrollview Programmatically
Android Room - Simple Select Query - Cannot Access Database on the Main Thread
What Is Difference Between Getsupportfragmentmanager() and Getchildfragmentmanager()
Html.Fromhtml Deprecated in Android N
Pros and Cons of Sqlite and Shared Preferences
Open Application After Clicking on Notification
Android - How to Disable the Click of Home Button
Set Android Shape Color Programmatically
Warning: This Asynctask Class Should Be Static or Leaks Might Occur
Android - Set Max Length of Logcat Messages