'Flutter Firebase and Android issue - unable to initialise. Cannot find google-services.json with latest (sept 2020) migration instructions executed

I'm a Flutter developer and for the past two days I have been trying to get my app working for Android. It's quite a big app with a lot of different functionalities (mostly google maps and firebase) that work perfectly fine on iOS. However, now that I'm trying to get the Android part working I seem unable to start the app at all due to some Firebase issue.

FlutterFire is responsible for most Firebase packages and they just released a couple updates. I have spent quite some time refactoring my project to conform to most breaking changes. The issue that I'm facing has something to do with the new update. The error that I'm getting doesn't bring me anywhere closer to a solution unfortunately. I think it has something to do with the Android part not being able to find the google-services.json. As I mentioned, everything is working fine on iOS. So my logical conclusion would be that the Flutter code is fine as well. Google/StackOverflow/FlutterFire issues all seem to misguide me to issues that have no answers for me.

TL;DR When compiling to Android, Flutter App doesn't start because Firebase cannot find my google-services.json. Here's the stacktrace:

E/flutter (15568): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: [core/not-initialized] Firebase has not been correctly initialized. Have you added the "google-services.json" file to the project? 
E/flutter (15568):     
E/flutter (15568):     View the Android Installation documentation for more information: https://firebaseextended.github.io/flutterfire/docs/installation/android
E/flutter (15568):     
E/flutter (15568): #0      MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:86:9)
E/flutter (15568): <asynchronous suspension>
E/flutter (15568): #1      Firebase.initializeApp (package:firebase_core/src/firebase.dart:43:25)
E/flutter (15568): #2      mainCommon (package:userapp/main/main_common.dart:31:18)
E/flutter (15568): #3      main (package:userapp/main/main_dev.dart:6:9)
E/flutter (15568): #4      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:233:25)
E/flutter (15568): #5      _rootRun (dart:async/zone.dart:1190:13)
E/flutter (15568): #6      _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (15568): #7      _runZoned (dart:async/zone.dart:1630:10)
E/flutter (15568): #8      runZonedGuarded (dart:async/zone.dart:1618:12)
E/flutter (15568): #9      _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:225:5)
E/flutter (15568): #10     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
E/flutter (15568): #11     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
E/flutter (15568):

I have done the following things so far:

  1. Followed the migration instructions on https://firebase.flutter.dev/docs/migration/.
  2. Placed my google-services.json in android/app as well as android/app/src/main, android/app/src/profile and android/app/src/debug (I know the latter 3 are not essential but I have seen it being mentioned a couple times).
  3. Make sure I call WidgetsFlutterBinding.ensureInitialized() before calling await Firebase.initalizeApp().
  4. Make sure I call Firebase.initializeApp() before I call runApp().
  5. Rebuilt the project with different versions of plugins inside my pubspec.yaml and/or both my application and app build.gradle.
  6. Witheld myself numerous times from throwing the good old laptop out of the window.

Even when I remove all of my code and leave my app with nothing more than the following code I still get the same error.

WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());

My app/build.gradle has the following config:

compileSdkVersion 29

sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
}

lintOptions {
    disable 'InvalidPackage'
}

defaultConfig {
    applicationId "stackoverflow.example.package"
    minSdkVersion 21
    targetSdkVersion 29
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}

And the following dependencies:

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.google.firebase:firebase-messaging:20.2.4'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.android.support:multidex:2.0.1'
    implementation 'com.google.firebase:firebase-perf:19.0.6'
}

I apply the following plugins:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.firebase-perf'
apply plugin: 'com.google.firebase.crashlytics'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

My android/build.gradle has to following dependencies (I have updated these to specific and latest versions in an attempt to fix this issue without success):

dependencies {
    classpath 'com.android.tools.build:gradle:3.5.4'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61"
    classpath 'com.google.gms:google-services:4.3.3'
    classpath 'com.google.firebase:perf-plugin:1.3.1'
    classpath 'com.google.firebase:firebase-crashlytics-gradle:2.2.1'
}

I have also updated my google-services.json multiple times downloading the latest version from Firebase but that also did not help. I find it weird to not see anyone else with this problem. I hope any of you are able to figure out what's going on. Thanks a lot in advance.

Update: I removed and added a couple dependencies in the build.gradle (even though the migration guide says to remove them all, this doesn't always work) and reverted the android project back to Java instead of Kotlin. This allowed to reconfigure some firebase messaging in a better way. This seems to have changed something but still gives me some kind Firebase initialise error. [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: MissingPluginException(No implementation found for method Firebase#initializeCore on channel plugins.flutter.io/firebase_core) I won't sleep till I fix this ♞

Update 2: I ended up creating a new project carefully copying firebase and multiple other packages from the original project. After that I copied every single file in lib/android along with their respective configurations in build.gradle's and manifests. For iOS I just copied the entire project and that worked immediately. All seems to work now in the 'new' project. Still completely unsure what the culprit was since I've copied the exact project over to a new one. Anyway hope this helps anyone with this issue. I've wasted five days on this 😄, I wish you not the same whoever you are.

Muhammad's answer below seems to have helped a lot of people as well, try my solution as a last resort.



Solution 1:[1]

After hours of struggle, I am able to figure out the solution.

  1. First, add the 'google-services' plugin as a dependency inside of the android/build.gradle file:
buildscript {
   dependencies {
      // ... other dependencies
      classpath 'com.google.gms:google-services:4.3.3'
   }
}
  1. Lastly, execute the plugin by adding the following underneath the line apply plugin: 'com.android.application', within the /android/app/build.gradle file:
apply plugin: 'com.google.gms.google-services'

Building for Android# Open the /android/app/build.gradle file. Under dependencies add the multidex module, and enable it within the defaultConfig:

android {
  defaultConfig {
    // ...
    minSdkVersion 16
    targetSdkVersion 28
    multiDexEnabled true
  }
}

dependencies {
   implementation 'com.android.support:multidex:1.0.3'
}

Solution 2:[2]

Personally, the answer from @MuhammadAmjad showed me a missing line from my graddle file in the app level. Specifically, I missed the following:

apply plugin: 'com.google.gms.google-services'

I am not sure how this happened but another page that Google has for installing Firebase was missing this detail. I would suggest people having this issue to very quickly see if this is the culprit in case they had the same issue with me, which would save them a lot of time. Thanks again to @MuhammadAmjad.

Solution 3:[3]

I follow these steps (Brian and Muhammed said above) and execute

flutter build apk --debug
flutter build apk --profile
flutter build apk --release

and then, run app! it works for me!

Solution 4:[4]

Just invalidate cache and restart Android Studio

I was not offered the possibility to sync gradle files (as specified by the Google documentation), so I suspected that it has to do with old gradle files being somewhat used by the app.

I fixed this problem with File->Invalidate Cache and Restart.

As a rule of thumb, in IT, restarting will fix 50% of your problems.

Solution 5:[5]

Add dependencies

classpath 'com.google.gms:google-services:4.3.8'

under project/build.gradle

Then add apply plugin

apply plugin: 'com.google.gms.google-services'

you can either paste it at the bottom or paste it with the other apply plugins.

under project/android/app/build.gradle

this one worked for me.

Solution 6:[6]

I see too many things that are probably not right. I'm not sure about step 4 (Make sure I call Firebase.initializeApp() before I call runApp()), in my case I initialize the firebase after runapp.

void main() {
  initializeDateFormatting().then((_) => runApp(MyApp()));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      // Initialize FlutterFire
      future: Firebase.initializeApp(),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {

Another thing I see is the link you post, I recommend you read it again, in some point it said:

Ensure that you're not manually importing any Firebase Android SDK dependencies in your android/app/build.gradle files dependencies { ... } section. e.g. remove any lines similar to: implementation 'com.google.firebase:firebase-{PRODUCT}:{VERSION}

Wich is something that I see in your build.gradle dependencies. Your gradle is too diferent from mine, wich is closer to be the original from the startup app in Android Studio.

Hope this help a bit.

Solution 7:[7]

  1. please update the firebase core dependency to firebase_core: "0.5.2+1" after that, I added

  2. WidgetsFlutterBinding.ensureInitialized(); Firebase.initializeApp(); runApp(MaterialApp(debugShowCheckedModeBanner: false, home: MyApp()));

    in main method

  3. and update all the in build.gaddle in project and app directory.

Solution 8:[8]

If it doesn't work even though you've done all the dependencies, remove the app from the emulator. Then create a new app in the same project at firebase. Repeat the steps. Reinstall the application. That's how my problem was solved.

Solution 9:[9]

Before any of the Firebase services can be used, FlutterFire needs to be initialized. Execute below commands and follow instructions:

# Install the CLI if not already done so
dart pub global activate flutterfire_cli

# Run the `configure` command, select a Firebase project and platforms
flutterfire configure

First import the firebase_core plugin and generated firebase_options.dart file:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform, //This line is necessary
  );
  runApp(MyApp());
}

The DefaultFirebaseOptions.currentPlatform is imported from our generated firebase_options.dart file.

Solution 10:[10]

I had that problem . when i added apply plugin: 'com.google.gms.google-services' in gradle, it started working.

Solution 11:[11]

  1. Download the google-services.json file from firebase
  2. add the google-services plugin to your app/build.gradle file: dependencies { classpath 'com.google.gms:google-services:4.3.4' // ... }
  3. In android/build.gradle add apply plugin: 'com.google.gms.google-services' above

android { //... }

Ref: https://developers.google.com/android/guides/google-services-plugin

Solution 12:[12]

I was tackling this problem also and I followed the solution of Brian but it was not working for me properly. Then I just downgraded my Gradle wrapper version from 6.8.3 to 6.5 and boom my app starts working properly.

So my suggestion is just to follow Brian's answer and if it does not work for you then try downgrading or upgrading your Gradle version.