I am using firebase remote config in my app. I get rare IllegalStateException crashes when using this call (crash happens when accessing remoteConfig
instance):
Firebase.remoteConfig.fetchAndActivate().addOnCompleteListener { task ->
when (task.isSuccessful) {
true -> Log.d(APP_TAG, "Config params updated: ${task.result}")
false -> Log.e(APP_TAG, "Config params loading ERROR!")
}
}
This is stacktrace:
Caused by java.lang.IllegalStateException:
at com.google.firebase.FirebaseApp.getInstance (FirebaseApp.java:179)
at com.google.firebase.remoteconfig.FirebaseRemoteConfig.getInstance (FirebaseRemoteConfig.java:81)
at com.google.firebase.remoteconfig.RemoteConfigKt.getRemoteConfig (RemoteConfig.kt:32)
at myapp.mypackage.FbRemoteConfig.fetchFreshValues (FbRemoteConfig.kt:83)
at android.app.ActivityThread.handleReceiver (ActivityThread.java:4981)
Impossible to reproduce on my devices, works like a charm, 100x out of 100x, for days and weeks.
However, somehow it crashes occasionally on other people’s devices, but only maybe for ~ 1-2% users. I can’t put my finger on why.
Typical advice I googled mentions calling FirebaseApp.initializeApp(this)
in Application class, but that doesn’t seem to be correct – it’s not even mentioned in docs, it’s not mentioned in any tutorials whatsoever, and according to the latest stackoverlow posts it’s not necessary to initialize firebase anymore in 2024 to use remote config. And besides, it works just fine for me & vast majority of users…So most probably that’s not the case.
But other than that, I could not find anything…Why do I get these mysterious, rare IllegalStateException?
This is my app level build.gradle:
apply plugin: 'com.google.gms.google-services'
...
dependencies {
...
implementation(platform("com.google.firebase:firebase-bom:33.4.0"))
implementation("com.google.firebase:firebase-config")
implementation("com.google.firebase:firebase-analytics")
}
This is my project level buildgradle:
...
dependencies {
...
classpath 'com.google.gms:google-services:4.4.2'
}
2
Answers
Possible fix for a safer side is to add a condition to check whether your activity or fragment is alive or not. This is especially important if you are using the fragment lifecycle callbacks such as onAttach() and onDetach().
Try upgrading Android Gradle Plugin version to 8.1.0 as mentioned here Firebase Initialization Crash After Updating com.google.gms:google-services to 4.4.0