LoginSignup
34
25

More than 5 years have passed since last update.

【Android】Firebase Crashlyticsを最速で実装する

Posted at

Firebase Crashlyticsのβ版がリリースされました。
https://firebase.googleblog.com/2017/11/announcing-firebase-crashlytics-beta.html
スクリーンショット 2017-11-11 17.42.32.png

早速実装してみたいと思います。

Get started with Firebase Crashlytics

https://firebase.google.com/docs/crashlytics/get-started
ドキュメント通りに追記します。

  • project-level
build.gradle
buildscripts {
    repositories {
        maven {
           url 'https://maven.fabric.io/public'
        }
    }
    dependencies {
        classpath 'io.fabric.tools:gradle:1.24.4'
    }
}

allprojects {
    repositories {
       maven {
           url 'https://maven.google.com/'
       }
    }
}
  • app-level
build.gradle
apply plugin: 'io.fabric'

dependencies {
    compile('com.crashlytics.sdk.android:crashlytics:2.7.1@aar') {
       transitive = true
    }
    compile 'com.google.firebase:firebase-core:11.4.2'
}

Crashlyticsの画面

設定前はこのような画面ですが、
スクリーンショット 2017-11-11 15.52.20.png

build.gradleを設定してBuild->Runすると、自動的に表示が変わります。
スクリーンショット 2017-11-11 16.13.20.png

バグかわいい。
スクリーンショット 2017-11-11 16.13.20のコピー.png

Test your implementation

https://firebase.google.com/docs/crashlytics/force-a-crash
故意にアプリをクラッシュさせて、クラッシュレポートを見てみましょう。

MainActivity.kt
...

override fun onResume() {
    super.onResume()

    // Crashlytics CrashTest
    val crashButton = Button(this).apply {
        text = "crash!!"
        setOnClickListener {
            Crashlytics.getInstance().crash()
        }
    }
    addContentView(crashButton,
                   ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                          ViewGroup.LayoutParams.WRAP_CONTENT))

...

crash!!ボタンを押すとjava.lang.ArrayIndexOutOfBoundsExceptionで落ちます。

さて、Crashlyticsのコンソールですが、
スクリーンショット 2017-11-11 18.02.43.png

・・・。

おや・・・

レポートが来ない・・・。

Set up manual initialization

もう一度ドキュメントを読み返してみると、通ってきた手順とは別に設定がありました。
https://firebase.google.com/docs/crashlytics/upgrade-from-crash-reporting

こちらも一部参考にしてみます。

  • AndroidManifest.xmlに追記
AndroidManifest.xml
<meta-data android:name="firebase_crashlytics_collection_enabled" android:value="false"/>
  • Fabric.with(this, Crashlytics())追記
MainActivity.kt
...

override fun onResume() {
    super.onResume()

    // Crashlytics CrashTest
    Fabric.with(this, Crashlytics())
    val crashButton = Button(this).apply {
        text = "crash!!"
        setOnClickListener {
            Crashlytics.getInstance().crash()
        }
    }
    addContentView(crashButton,
                   ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                          ViewGroup.LayoutParams.WRAP_CONTENT))

...

再度crash!!ボタンでアプリをクラッシュさせてみます。
スクリーンショット 2017-11-11 18.14.29.png

今度は上手くレポートが届きました!
スクリーンショット 2017-11-11 18.16.41.png

まとめ

少しドキュメントの設定とは違いましたが、無事Crashlyticsのレポートを表示することができました。
環境によって手順が変わるかもしれないので、引き続き調査してみたいと思います。

一つ注意点ですが、クラッシュレポートはクラッシュした後の次回アプリ起動時に送信されます。

When testing, reopen your app after pressing the button to crash it to make sure Crashlytics has a chance to report the crash. The report should appear in the Firebase console within five minutes.

なので、クラッシュのテストを行う際は、

  • アプリをクラッシュさせる。
  • アプリをkillして、再起動する。
  • Crashlyticsのコンソールを確認する。

の手順がよさそうです。:thumbsup:

34
25
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
34
25