5

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

KENdi
  • 7,576
  • 2
  • 16
  • 31
Dan Wiebe
  • 449
  • 1
  • 4
  • 11

2 Answers2

7

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

Martin Metselaar
  • 347
  • 1
  • 2
  • 11
  • 1
    Cool. Thanks. This card is now back in my backlog with a BLOCKED sticker, but it'll come back up, and when it does we'll try this. Thanks again. – Dan Wiebe Aug 16 '17 at 17:57
  • 1
    Somebody else picked up the card, but he says your solution worked fine. Thanks: now we don't have to go looking beyond Firebase for our analytics solution! – Dan Wiebe Aug 17 '17 at 15:33
  • Nice! Was glad to help and also learn some more about Firebase! – Martin Metselaar Aug 21 '17 at 08:04
  • Unfortunately I couldn't get this to work using an Ionic/Capacitor Android project. However it has given me some hope that maybe it can be done. – MadMac Feb 14 '21 at 22:36
  • I got the same result by simply changing the appId in the google-services.json file each time I built the Android app. This works better in my situation. – MadMac Feb 15 '21 at 03:05
0

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.

Martin Metselaar
  • 347
  • 1
  • 2
  • 11
  • For a code example you can look at the following [StackOverflow question](https://stackoverflow.com/questions/45546833/use-multiple-firebase-accounts-in-single-android-app-for-google-analytics) – Martin Metselaar Aug 15 '17 at 09:55
  • Thanks for the suggestion. If I understand this properly, each flavor of my Android app will have its own throwaway Firebase app, just for defaulting purposes. Then there'll be one additional Firebase app, and my Android app will initially default to the flavor-specific Firebase app, and then--before actually sending any traffic to it--switch over to the additional non-flavor-specific Firebase app and log its analytics there. Right? – Dan Wiebe Aug 15 '17 at 14:36
  • I also understand that when I call FirebaseApp.initializeApp, I'm supposed to designate which of the Firebase apps I want to initialize by the mobilesdk_app_id I put in the setApplicationId() call to the FirebaseOptions.Builder. – Dan Wiebe Aug 15 '17 at 14:41
  • Unfortunately, when I do that, the FirebaseAnalytics instance I get from FirebaseAnalytics.getInstance() still talks to the flavor-specific Firebase app that's not supposed to see any traffic, and the non-flavor-specific Firebase app whose mobilesdk_app_id I put in the FirebaseOptions doesn't see anything. Ideas? – Dan Wiebe Aug 15 '17 at 14:42
  • As a matter of fact, that .setApplicationId() in the builder doesn't seem to matter at all. I tried passing it "booga" rather than the value from the .json file and it still logged just fine to the flavor-specific Firebase app. – Dan Wiebe Aug 15 '17 at 15:25