Can Multiple Android Application Access Same Firebase Database

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.

Multiple Application in same Firebase Project

Since "both apps should use the same database", you need to use the same project for authentication.

One of the classical ways in this case is to use a role-based access control strategy. Admin users would have a specific "Admin" role and only users with this role can read/write Admin data.

Custom Claims are the way to implement such role-based access control in Firebase. The documentation explains it very well (including how to use the custom claims in Firebase security rules -to restrict access to data based on these claims- as well as how to use them to hide/show elements of your application user interface depending on the role).

You may also be interested by this article, which shows how to create an Admin module for managing users access and roles.

This SO answer may also interest you, in particular the answers to the last two OP questions ("Can I build an Admin Panel inside the client app?" and "Should I build an Admin Panel in another app and using Cloud Functions?")

Same firebase account for different Play Console Account's apps?

The short answer is, it depends.

If the two apps are identical or closely related (eg. an admin-only app that controls the flow of the client app), then yes, you'll want to configure both apps to use the same Firebase project so that they both have access to the same data.

Using the same Firebase project in multiple apps is pretty common, especially in the scenario outlined above as well as deploying to multiple platforms. Take, for example, an app that you have deployed on the web, iOS, as well as Android. These apps run on different platforms, but they are essentially the exact same app, with the exact same functionality. You don't want to be maintaining multiple projects containing duplicate data, so it's a good idea to connect different versions of the same app to the same Firebase project.

On the other hand, if these apps are not related at all (eg a quiz game and a social media app), then it doesn't make sense for them to be using the same Firebase project as the two apps are completely separate from one another. In this scenario, using the same project would lead to disorganized data, as well as making it potentially more difficult to secure and query as the apps have separate functions, and thus separate logic to control the flow of data.

It sounds like your use case falls into the first category. You have the same app on multiple Play Console accounts. You probably want the data to remain consistent between them, so you'd want to use a single Firebase project.

Multiple Firebase projects in a single android application

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

Sample Image

Sample Image

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.



Related Topics



Leave a reply



Submit