How can I make multiple app flavors use the same google-services.json file?

Deadly 提交于 2020-01-04 03:01:14

问题


I would like to have one google-services.json file, with one application ID, connected to one Firebase "app" in one Firebase "project," used by a variety of flavors of my Android app.

My flavors are branded differently, but they work very much the same, and I want the analytics data from all of them to end up in the same place in Firebase. However, they all (of course) have Android application IDs/package names that are suffixed differently, and none of those flavor package names match the one in the google-services.json, which doesn't have a suffix at all.

I've seen many ways to connect different flavors of an Android app to different Firebase "apps" in the same Firebase "project," but that's not what I want to do: I want to connect different Android app flavors to the same Firebase "app."

Is there a way to do this with Firebase, or should I look elsewhere?

Thanks, Dan Wiebe


回答1:


Indeed my previous answer was not enough to answer your question so I've created an step by step guide.

Step by Step

  1. Build the application with the product flavor you want as default.

  2. Remove the apply plugin: 'com.google.gms.google-services from your app/build.gradle. This plugin reads the values in google-services.json and copies them on compile time in an xml values file. We will do this manually in step 4.

  3. Create a new (or use an existing) xml values file in the main folder.

  4. Go to app/build/generated/res/google-services/{productFlavor}/debug/values/values.xml and copy those values into the values xml created in Step 3. Replace {productFlavor} with the product flavor selected into Step 1.

  5. Delete the google-services.json

Now the credentials from your default application will be used and you don't need to initialise the way I mentioned in my previous answer




回答2:


According to the documentation you need a default project in the google-services.json but in addition to the default you can add your analytics project.

In the event that an app requires access to another Firebase project in addition to the default project, initializeApp(Context, FirebaseOptions, String) must be used to create that relationship programmatically.

So you want to checkout FirebaseApp#initializeApp(Context context, FirebaseOptions options, String name) to add your analytics project. The documentation how to be initialised can be found here.



来源:https://stackoverflow.com/questions/45682158/how-can-i-make-multiple-app-flavors-use-the-same-google-services-json-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!