LoginSignup
5
3

More than 5 years have passed since last update.

Firebase Crash Reportingを導入するとCrashした、ので直した

Posted at

何が起こったか

Firebase Notificationsの導入が完了したので、次にFirebase Crash Reportingを導入してみました。
アプリ起動直後はなんとも無いのですが、数秒経つと、「問題が発生したため、...」といういつものメッセージとともに、下記のようなスタックトレースが。

07-06 21:38:55.954 24480-24480/hm.orz.chaos114.android.tumekyouen.debug:background_crash E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                                           Process: hm.orz.chaos114.android.tumekyouen.debug:background_crash, PID: 24480
                                                                                                           java.lang.RuntimeException: Unable to create application hm.orz.chaos114.android.tumekyouen.App: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. 
                                                                                                               at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4809)
                                                                                                               at android.app.ActivityThread.access$1600(ActivityThread.java:154)
                                                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1452)
                                                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                                               at android.os.Looper.loop(Looper.java:224)
                                                                                                               at android.app.ActivityThread.main(ActivityThread.java:5526)
                                                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                                            Caused by: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. 
                                                                                                               at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
                                                                                                               at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
                                                                                                               at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source)
                                                                                                               at com.google.firebase.messaging.FirebaseMessaging.getInstance(Unknown Source)
                                                                                                               at hm.orz.chaos114.android.tumekyouen.App.onCreate(App.java:39)
                                                                                                               at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1014)
                                                                                                               at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4806)
                                                                                                               at android.app.ActivityThread.access$1600(ActivityThread.java:154) 
                                                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1452) 
                                                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                                               at android.os.Looper.loop(Looper.java:224) 
                                                                                                               at android.app.ActivityThread.main(ActivityThread.java:5526) 
                                                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

どんなコードだったか

App.java の39行目は、

FirebaseMessaging.getInstance().subscribeToTopic("all");

のように、 /topics/all を受信するように設定していました。

どうやって直したか

http://stackoverflow.com/a/37355454 を参考にしました。
ここでは、 FirebaseDatabasegetInstance でしたが、似たようなものだろう、ということで、

if (!FirebaseApp.getApps(this).isEmpty()) {
    FirebaseMessaging.getInstance().subscribeToTopic("all");
}

としたところ、クラッシュしなくなりました。
また、通知もちゃんと受信できているようです。

何が起こっているのか

http://stackoverflow.com/a/37355454http://stackoverflow.com/a/37376472 に詳しく書いてあるっぽいのです。

Crash Reportingは background_crash という別プロセスを立ち上げて、クラッシュ情報を送信するそうです。
別プロセスということは、別のApplicationインスタンスが生成されます。
で、これ以上深くはわからないのですが、 FirebaseApp.getInstance で例外が発生しているので、その辺りが上手く初期化されてないのかなーと。
https://developers.google.com/android/reference/com/google/firebase/FirebaseApp とかみると、FirebaseInitProvider とか関係あるかもですね。

5
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3