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
I think it will be the perfect answer of your question.