LoginSignup
2
7

More than 3 years have passed since last update.

Firebase Crashlyticsを導入する

Last updated at Posted at 2018-05-31

セットアップ

Firebaseプロジェクトの設定

- 新規プロジェウトを作成するか、既存プロジェクトが必要です。
-Crashlytics を有効にする: Firebase コンソールで Clashlyticsを設定をクリックします。

Androidプロジェクトに Crashlytics SDK を追加する

FirebaseSDKはすでにインストール済みであることを前提に進めます。

プロジェクトのbuild.gradleにSDKを追加

Crashlyticswを利用するには、google-services-plugin 3.1.2 以降が必要です。

buildscript {
    ext.kotlin_version = '1.2.41'
    repositories {
        google()
        jcenter()
        // 追加
        maven {
            url 'https://maven.fabric.io/public'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // Add the Google Services plugin (check for v3.1.2 or higher).
        classpath 'com.google.gms:google-services:4.3.3'

        // Add the Fabric Crashlytics plugin.
        classpath 'io.fabric.tools:gradle:1.31.2'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        // 追加
        maven {
            url 'https://maven.google.com/'
        }
    }
}

アプリのbuild.gradleにSDKを追加


apply plugin: 'io.fabric'

dependencies {

    .......

    implementation('com.crashlytics.sdk.android:crashlytics:2.9.1@aar') {
        transitive = true
    }
    implementation 'com.google.firebase:firebase-core:16.0.0'

}

これでセットアップは完了です。

実装のテスト

意図的にクラッシュを発生させて実装状況をテストします。
ボタンクリックイベントなどで以下のコードを実行し、強制的にクラッシュを発生させます。

    Crashlytics.getInstance().crash();

クラッシュ発生後に再度アプリを起動し、Crashlyticsにクラッシュを送信させます。
この内容は5分ほどで反映されます。

Crashlyticsのデバッグモードをオンにする

強制クラッシュが発生せず、Crashlyticsが動作しない場合、Crashlyticsのデバッグモードをオンにする必要があります。

Androidでデバッグモードをオンにするには、まず自動初期化機能をオフにする必要があります。
AndroidManifest.xmlに以下の記述を追加します。

<meta-data android:name="firebase_crashlytics_collection_enabled" android:value="false" />

次にデバッガをオンにして手動でCrashlyticsを初期化します。

    val fabric = Fabric.Builder(this)
            .kits(Crashlytics())
            .debuggable(true)           // Enables Crashlytics debugger
            .build()
    Fabric.with(fabric)

参考

https://firebase.google.com/docs/crashlytics/get-started?hl=en&platform=android
日本語のドキュメントは古くて使えないので英語を参照した方が幸せになれますよ

2
7
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
2
7