Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to configure Firebase analytics without google-services.json configuration file?

I would like to use Firebase analytics for one of my library modules. I would like to program in such a way that configuration file (google-services.json) should be accessible from either client app folder or configure the same from the client side.

Is there a way I could implement the above mentioned scenario?

Thanks in advance.

like image 402
Arun Chand Avatar asked Jan 12 '17 06:01

Arun Chand


People also ask

Should I add Google-services json to Git?

The general answer is yes, the google-services. json is safe to check in to your repo and is something that should be shared among engineers on your team. The JSON file does not contain any super-sensitive information (like a server API key).

How can I add two Google-services json in the same project?

There is no way to use two google-services. json files in a single Android app. The file name is the same between them and they need to be in the same location. So one will overwrite the other in that case.


2 Answers

You can initialize your library project configurations manually as below,

For more info, kindly refer working-with-multiple-firebase-projects-in-an-android-app

Also go-through how-does-firebase-initialize-on-android to understand; how the Firebase module is getting initialized by itself.

For Android apps using Firebase, there is a central FirebaseApp object that manages the configuration for all the Firebase APIs. This is initialized automatically by a content provider when your app is launched, and you typically never need to interact with it. However, when you want to access multiple projects from a single app, you'll need a distinct FirebaseApp to reference each one individually. It's up to you to initialize the instances other than the default that Firebase creates for you.

FirebaseOptions options = new FirebaseOptions.Builder()
       .setApplicationId("1:530266078999:android:481c4ecf3253701e") // Required for Analytics.
       .setApiKey("AIzaSyBRxOyIj5dJkKgAVPXRLYFkdZwh2Xxq51k") // Required for Auth.
       .setDatabaseUrl("https://project-1765055333176374514.firebaseio.com/") // Required for RTDB.
       .build();
FirebaseApp.initializeApp(this /* Context */, options, "secondary");

Hope I've answered your question.

like image 125
Karthi R Avatar answered Sep 20 '22 18:09

Karthi R


I've found interesting workaround. Try the following: put your google-services.json to any sample app google provides for Firebase integration. Compile it.

Take a look on path app/build/generated/res/google-services/debug/values/ there should be generated values.xml file with bunch of strings mentioned in errors you described. Copy these strings to value.xml file of your project. That's it.

like image 44
Gennadiy Ryabkin Avatar answered Sep 19 '22 18:09

Gennadiy Ryabkin