Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase crashes on Android during anonymous login

I am getting this crash in about 70% of devices in the market. However it never came up during testing.

public class MyApp extends Application{
public void onCreate() {
        super.onCreate();
        FirebaseDatabase.getInstance().setPersistenceEnabled(true);
        FirebaseAuth.getInstance().signInAnonymously();
    }
}

This is the error.

    Fatal Exception: java.lang.RuntimeException: Unable to create application com.mycompany.myapp.activities.MyApp: java.lang.ClassCastException: com.google.android.gms.internal.hc cannot be cast to com.google.firebase.auth.c
com.google.android.gms.internal.hc cannot be cast to com.google.firebase.auth.c
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4927)
    at android.app.ActivityThread.access$1500(ActivityThread.java:153)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5633)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassCastException: com.google.android.gms.internal.hc cannot be cast to com.google.firebase.auth.c
    at com.google.android.gms.internal.aa.a(Unknown Source)
    at com.google.firebase.auth.FirebaseAuth.a(Unknown Source)
    at com.google.firebase.auth.FirebaseAuth.d(Unknown Source)
    at com.google.firebase.auth.FirebaseAuth.<init>(Unknown Source)
    at com.google.firebase.auth.FirebaseAuth.<init>(Unknown Source)
    at com.google.android.gms.internal.z.<init>(Unknown Source)
    at com.google.firebase.auth.FirebaseAuth.c(Unknown Source)
    at com.google.firebase.auth.FirebaseAuth.b(Unknown Source)
    at com.google.firebase.auth.FirebaseAuth.b(Unknown Source)
    at com.tesseractmobile.solitairesdk.activities.SolitaireApp.onCreate(SolitaireApp.java:123)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1020)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4924)

I am using firebase 9.2.1

compile "com.google.firebase:firebase-core:${firebaseVersion}"
compile "com.google.firebase:firebase-database:${firebaseVersion}"
//For dynamic links
compile "com.google.firebase:firebase-invites:${firebaseVersion}"
//Authentication - for logging winning games
compile "com.google.firebase:firebase-auth:${firebaseVersion}"

Right now I am just disabling firebase in our app. We were just testing it to see how stable it is. I have no clue why it worked on all of our test devices then crashed this bad in the wild.

Update: I was able to get a video of the issue using Firebase Test Lab. It happened when the robotest clicked on an Ad (Ad supported app) then came back to the app. So maybe it has something to do with FirebaseAuth.getInstance().signInAnonymously(); getting called twice? However Application.onCreate() should only be called once. I know there is a known bug with Firebase crash reporting that will cause two Application instances to be created but we are not using that sdk util they fix the issue.

like image 797
theJosh Avatar asked Jul 22 '16 19:07

theJosh


1 Answers

I used Firebase Test Lab to find the issue. It crashed on one of their devices and I watched the video of what happened. Our app is ad supported. We don't click our own ads during testing so this never came up. The crash happens after a user clicks an ad and then reenters the app. The solution I found was the same as this issue. FirebaseApp with name [DEFAULT] doesn't exist

        if (!FirebaseApp.getApps(this).isEmpty()){ 
            FirebaseDatabase.getInstance().setPersistenceEnabled(true);
            FirebaseAuth.getInstance().signInAnonymously();
        }

I think Application.onCreate() is called more than once. Though that should never happen.

EDIT: THIS ANSWER DOES NOT WORK

After a full week of testing by a 4 person QA team. Running 58 unit and connected tests with espresso on 7 test devices. Then running multiple tests on dozens of cloud devices using Google Test Lab. The problem never happened. Then on release day, 500 crashes in the first half hour!? 70% crash rate. So back to square one.

FINAL ANSWER: Proguard issue

I added these two line to my proguard-rules.pro

#Firebase Authentication
-keepattributes Signature
-keepattributes *Annotation*

Problem solved.

like image 68
theJosh Avatar answered Nov 07 '22 20:11

theJosh



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!