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

31,495

Solution 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

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

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

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

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.

Share:
31,495
Coda Veto
Author by

Coda Veto

Apps & Jokes

Updated on November 14, 2021

Comments

  • Coda Veto
    Coda Veto over 2 years

    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.

  • Coda Veto
    Coda Veto over 3 years
    Thank you for your response! Because I do some logic checking before I run my app it runs before runApp(). Both are fine though as long as you use WidgetsFlutterBinding.ensureInitialized() in my case (stackoverflow.com/questions/63492211/…). I have read the guide multiple times. I have added these dependencies back after trying out multiple things, should have mentioned that as well - my bad. Edited my question with the latest update.
  • Vikrant Agrahari
    Vikrant Agrahari over 3 years
    Thanks a ton I followed the Docs and other Stackoverflow sources but this solve the issues.
  • Muhammad Amjad
    Muhammad Amjad over 3 years
    make it as a comment, not post an answer here
  • Lucas Correa
    Lucas Correa over 3 years
    I don't have reputation enough to post a comment yet.
  • thanhbinh84
    thanhbinh84 almost 3 years
    It seems this is not necessary for single firebase projects. If you want to setup multiple firebase projects for multiple environments, this is needed. Thanks a lot.
  • Delmontee
    Delmontee almost 3 years
    THANK YOU!! Yes, apply plugin: 'com.google.gms.google-services' was the problem for me too. That part isn't explained in any tutorial I've looked at.
  • Azhar Ali
    Azhar Ali over 2 years
    Thank you so much. it works for me as well.
  • Giant Brain
    Giant Brain about 2 years
    what is instead of above link? This link is empty.