2

I have been looking for a method in order to add the google-services.json at runtime as we use same base code to configure the application for the different client, hence we want to add google-services.json at runtime.

I have tried several links but am unable to find such a way to do it.

Any help will be very much appreciated.

Vikas Pal
  • 58
  • 9

1 Answers1

2

The google-services.json is actually never read at runtime. During the build process of your Android app, the relevant information from google-services.json is translated into your App's XML resources, and that is where it's read when the app starts. Since the destination where the build plugin puts the values is hard-wired, you can't configure it to have multiple sets of configuration data.

Instead of trying to read google-services.json at runtime, I'd recommend configuring the FirebaseApp instance in your code explicitly, like this:

// 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();

Now you can determine yourself where to keep the relevant information from the google-services.json and which ones to use when the app starts.

and then:

// 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);

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807