17

I have added following code to initialise there projects.

FirebaseOptions options = new FirebaseOptions.Builder()
            .setApplicationId("1:129574837465:android:0123456773a52cf4f6") // Required for Analytics.
            .setApiKey("iubdeibneh8gzDt7Xn9f-jdjjdjdjdj") // Required for Auth.
            .setDatabaseUrl("https://databasename-d7r7.firebaseio.com") // Required for RTDB.
            .build();

FirebaseApp.initializeApp(context /* Context */, options, "secondary");


FirebaseOptions options2 = new FirebaseOptions.Builder()
            .setApplicationId("1:129574837465:android:0123456773a52cf4f6") // Required for Analytics.
            .setApiKey("iubdeibneh8gzDt7Xn9f-jdjjdjdjdj") // Required for Auth.
            .setDatabaseUrl("https://databasename2-d7r7.firebaseio.com") // Required for RTDB.
            .build();

FirebaseApp.initializeApp(context /* Context */, options2, "secondary2");


FirebaseOptions options3 = new FirebaseOptions.Builder()
            .setApplicationId("1:129574837465:android:0123456773a52cf4f6") // Required for Analytics.
            .setApiKey("kjdkj-o_3nk4jn4k3kjk23j") // Required for Auth.
            .setDatabaseUrl("https://databasename3-d7r7.firebaseio.com") // Required for RTDB.
            .build();

FirebaseApp.initializeApp(context /* Context */, options3, "secondary3");

After initialisation my app is running fine. I can use FirebaseAuth, and FirebaseRTDB just fine but it is throwing error when it has to access firebase_Application_Id for analytics.

I have cross checked the ids from google-services.json files of all the projects. I don't know why but it throws error saying:

Missing google_app_id. Firebase Analytics disabled.

I couldn't figure out the root of this error.

mark922
  • 1,136
  • 2
  • 11
  • 20

7 Answers7

33

In my case the problem was with incomplete Firebase configuration.

I was missing

buildscript {
    ...
    dependencies {
        ...
        classpath 'com.google.gms:google-services:4.3.3' // google-services plugin
    }
}

allprojects {
    ...
    repositories {
        google()
    }
}

from build.gradle.

And

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'

from app/build.gradle.

Michael Alan Huff
  • 3,462
  • 3
  • 28
  • 44
Anton Malyshev
  • 8,686
  • 2
  • 27
  • 45
  • 3
    Thanks! `// ADD THIS AT THE BOTTOM` also works at the top. Fixed it for me. – gregn3 May 14 '20 at 03:22
  • 1
    Adding this solve my connection issue too. I am looking at the FlutterFire tutorial on the setup, but do no realize I need to update the Android project too. It seems FlutterFire CLI didn't help me to auto add this line and the tutorial didn't emphasis me to add back this lines. – Rico Chan Oct 08 '22 at 15:45
7

Could you double check if google-services.json is located at your Android app module root directory?

enter image description here

If it is there, make sure google-services.json has the mobilesdk_app_id key. It should be located under the client_info node.

 {
   ...,
   "client": [
     {
        "client_info": {
          "mobilesdk_app_id": "random_string",
          ...
        }
     }
   ]
 }
  • 1
    Yes. I cross checked it again. It is in the proper location and "mobilesdk_app_id":"VALUE" is also there. – mark922 Mar 22 '17 at 06:01
  • 2
    The main problem was I wanted to initialise the default firebase project programmatically. I have to use at least one google-services.json file to initialise the default project. Here is how the firebase initialises itself : [How does Firebase initialize on Android?](https://firebase.googleblog.com/2016/12/how-does-firebase-initialize-on-android.html). – mark922 Dec 19 '17 at 06:42
6

The issue could be due to instant app being enabled

Solution : Manually add google_app_id to the strings.xml file — as told here

enter image description here

Update : In case app crashes without any warning or error, try this (maven):

Go to project level build.gradle & check if it looks exactly like this:

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

and the code suddenly works and when you'll look at other answers you will find the same.

Garvit Jain
  • 1,862
  • 2
  • 19
  • 27
1

Initialize FirebaseApp early in OnCreate() using APP ID and Client key:

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

Source: https://firebase.googleblog.com/2016/12/working-with-multiple-firebase-projects-in-an-android-app.html

lashgar
  • 5,184
  • 3
  • 37
  • 45
1

@Anton Malyshev 's answer is correct.

    ↓ project gradle

buildscript {
    ...
    dependencies {
        ...
        classpath 'com.google.gms:google-services:4.3.3' // google-services plugin
    }
}

allprojects {
    ...
    repositories {
        google()
    }
}

    ↓ app gradle

apply plugin: 'com.google.gms.google-services'
gregn3
  • 1,728
  • 2
  • 19
  • 27
1

Manually add google_app_id to the strings.xml file like:

You can get google_app_id from google-services.json, in which it is defined as mobilesdk_app_id.

double-beep
  • 5,031
  • 17
  • 33
  • 41
vids
  • 381
  • 3
  • 5
0

For my case I increased the gradle version from
classpath 'com.android.tools.build:gradle:3.4.1' to

classpath 'com.android.tools.build:gradle:4.3.8'

in build.gradle project level and the problem disappeared

J.Ongoma
  • 65
  • 1
  • 5