2

I have an Android application with a multi-module.

  1. app - Actual app which uses firebase's Analytics and crashlytics.
  2. data_source - An android module that uses firebase's crashlytics and firestore.

When I configure firebase in both modules, I need to paste the google-services.json file in both modules. But since the firebase project is first configured with the ID of the 1st module. It doesn't allow me to paste the same google-services.json file in the data_source module.

I get the error as -> "No matching client found for package name 'com.myproject.data_source'"

How can I use firebase services in the multi-modules app. I have 4 total modules and all using different firebase services based on there needs(Crashlytics as a common one).

Mohammed Uzair
  • 145
  • 1
  • 11

1 Answers1

4

This was simple.

Download the google-services.json file and paste inside the module(package-name) that you have defined in the firebase(in my case com.myproject.app).

Now to use firebase in any other modules.

  1. Make sure you have added the below line in the root build.gradle file in the dependencies block.

    classpath 'com.google.gms:google-services:4.3.3'
    
  2. Add the required firebase services in your other modules dependencies block in build.gradle file. In my case Firestore(Add any that you want).

    implementation 'com.google.firebase:firebase-firestore:21.4.3'
    implementation 'com.google.firebase:firebase-firestore-ktx:21.4.3'
    
  3. Now when ever you want to consume the firebase services in your other modules

SomeCommonObjectClass

    //You can find all the below details in the google-services.json file, NOTE: All details are fake and random created API keys, please use your in the downloaded file.
    val options = FirebaseOptions.Builder()
        .setProjectId("yourfirebaseprojectname")
        .setApplicationId("1:1083305160395:android:48c09b2789790789324g92734")
        .setApiKey("AIzaKJAHDK8ASD7FAODF9A9DSF0AF908ADSF")
        .build()

    Firebase.initialize(context, options, "yourfirebaseprojectname")

Your class where you need the instance, in my case. UserDataStore

    //Analytics
    FirebaseAnalytics.getInstance(context)

    /Firestore
    val db = FirebaseFirestore.getInstance()

    val docRef = db.collection("Users").document()

...rest you know, as usual. Just the initialization part needs to be configured.

Mohammed Uzair
  • 145
  • 1
  • 11