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:
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.
After hours of struggle, I am able to figure out the solution.
buildscript {
dependencies {
// ... other dependencies
classpath 'com.google.gms:google-services:4.3.3'
}
}
/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'
}