Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FirebaseApp.initializeApp(this) Crashes NullPointerException

My application class is like below:

override fun onCreate() {
    super.onCreate()
    FirebaseApp.initializeApp(this)
    FirebaseCrashlytics.getInstance()
}

FirebaseApp.initializeApp(this) crashes on many devices except ours (we tested on 20+ devices and it works fine)

The crash report is taken from the Google Play Console and its stacktrace is:

java.lang.RuntimeException: 
  at android.app.ActivityThread.handleBindApplication (ActivityThread.java:6737)
  at android.app.ActivityThread.access$2000 (ActivityThread.java:273)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2020)
  at android.os.Handler.dispatchMessage (Handler.java:112)
  at android.os.Looper.loop (Looper.java:216)
  at android.app.ActivityThread.main (ActivityThread.java:7625)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:524)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:987)
Caused by: java.lang.NullPointerException: 
  at androidx.core.os.UserManagerCompat.isUserUnlocked (UserManagerCompat.java:42)
  at com.google.firebase.FirebaseApp.initializeAllApis (FirebaseApp.java:573)
  at com.google.firebase.FirebaseApp.initializeApp (FirebaseApp.java:302)
  at com.google.firebase.FirebaseApp.initializeApp (FirebaseApp.java:266)
  at com.google.firebase.FirebaseApp.initializeApp (FirebaseApp.java:251)
  at com.superapp.MyApplication.onCreate (MyApplication.kt:92)
  at android.app.Instrumentation.callApplicationOnCreate (Instrumentation.java:1162)
  at android.app.ActivityThread.handleBindApplication (ActivityThread.java:6717)
  at android.app.ActivityThread.access$2000 (ActivityThread.java:273)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2020)
  at android.os.Handler.dispatchMessage (Handler.java:112)
  at android.os.Looper.loop (Looper.java:216)
  at android.app.ActivityThread.main (ActivityThread.java:7625)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:524)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:987)

Project level build.gradle:

repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
        maven { url 'https://plugins.gradle.org/m2/' }
        maven {
            url 'https://developer.huawei.com/repo/'
        }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.0"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
        classpath "com.google.gms:google-services:4.3.3"
        classpath "com.google.firebase:firebase-crashlytics-gradle:2.4.1"
        classpath "com.huawei.agconnect:agcp:1.6.0.300"
        classpath "de.timfreiheit.resourceplaceholders:placeholders:0.3"
        classpath "com.google.firebase:firebase-appdistribution-gradle:1.3.1"
        classpath "org.jacoco:org.jacoco.core:0.8.7"
    }

app module level build.gradle:

// at the top
apply plugin: "com.android.application"
apply plugin: "com.google.firebase.crashlytics"
apply plugin: "de.timfreiheit.resourceplaceholders"
apply plugin: "com.google.firebase.appdistribution"
apply plugin: "com.huawei.agconnect"

// at the bottom
apply plugin: "com.google.gms.google-services"

The crash occurs on API level 28, 29, 30, 31 and 32

Is this Google issue or are we doing something wrong?

like image 617
Egemen Hamutçu Avatar asked Feb 07 '26 07:02

Egemen Hamutçu


1 Answers

This may be a bug in the UserManagerCompat, but generally calling init shouldn't be necessary for most apps, and that may alleviate your issue:

For a vast majority of apps, FirebaseInitProvider will handle the initialization of Firebase for the default project that it's configured to work with, via the data contained in the app's google-services.json file. This ContentProvider is merged into the app's manifest by default when building with Gradle, and it runs automatically at app launch. No additional lines of code are needed in this case.

https://firebase.google.com/docs/reference/android/com/google/firebase/FirebaseApp

If you can't avoid calling initializeApp you could consider adjusting the google services version to avoid the bug as in this answer:

Make sure to call FirebaseApp.initializeApp(Context) first in Android

like image 132
Chris Avatar answered Feb 09 '26 07:02

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!