Firebase Crash Reporting in Android

In previous article we have learned about how to send push notification in android by using Firebase cloud messaging. If you have not read this please Firebase push notification in android read this link.

It will helps you to making configuration of basic steps.
Now in this article we are learning about to track our crash report by using Firebase.

Here again I am configuring the basic setups.
Please see the below snap shot that help you to get the json mapping file and paste inside your app folder.





Now you need to add below dependency in your build.gradle root project and project.

build.gradle root project

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'
        classpath 'com.google.gms:google-services:3.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}


build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "com.sunil.firebasecrashreport"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.0'
    testCompile 'junit:junit:4.12'
    compile 'com.google.firebase:firebase-crash:9.8.0'
    compile 'com.google.android.gms:play-services-analytics:9.8.0'

}
// Add to the bottom of the file
apply plugin: 'com.google.gms.google-services'


MainActivity.java

import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.firebase.crash.FirebaseCrash;

public class MainActivity extends AppCompatActivity {

    private boolean isCrashKnow = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Check if google play services is up to date
        final int playServicesStatus = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
        if(playServicesStatus != ConnectionResult.SUCCESS){
            //If google play services in not available show an error dialog and return
            final Dialog errorDialog = GoogleApiAvailability.getInstance().getErrorDialog(this, playServicesStatus, 0, null);
            errorDialog.show();
            return;
        }

        Button btnCrash = (Button) findViewById(R.id.crash);
        btnCrash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                FirebaseCrash.logcat(Log.INFO, "MainActivity", "Crash button clicked");

                // If known crash the exception and report is using
                // Crash.report(). Otherwise throw the exception and let Firebase Crash automatically
                // report the crash.
                if (isCrashKnow) {
                    try {
                        throw new NullPointerException();
                    } catch (NullPointerException ex) {
                        // [START log_and_report]
                        FirebaseCrash.logcat(Log.ERROR, "MainActivity", "NPE caught");
                        FirebaseCrash.report(ex);
                        // [END log_and_report]
                    }
                } else {
                    throw new NullPointerException();
                }
            }
        });

    }
}



AndroidManifest.xml

<manifest package="com.sunil.firebasecrashreport" xmlns:android="http://schemas.android.com/apk/res/android">


    <uses-permission android:name="android.permission.INTERNET">
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">

    <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/AppTheme">

       <activity android:name="com.sunil.firebasecrashreport.MainActivity">

           <intent-filter>

               <action android:name="android.intent.action.MAIN"/>


              <category android:name="android.intent.category.LAUNCHER"/>

          </intent-filter>

       </activity>
    </application>
</manifest>


Errors take up to 20 minutes to show up in the Crash Reporting console. Check back to see your report there. This is only drawback i can see. It might be in beta case, Google team may be solve this after release. You can see crash on console.

Here is the crash report details in my dashboard that I got after 24 hours . It may more delay yours also.




I hope it will helps you to understand crash analytics by using firebase.

3 comments:

  1. i also got this kind of crash report and mail also....but sir firebase initialized succesfully

    ReplyDelete
  2. Replies
    1. I did not get your point clearly. If you got this error you need to solve this by try and catch.

      Delete

Debug Database in Android

In my last tutorial, we have learned how we can use persistence database in android. We also understood which database wrapper we can use...

Contact Me

Name

Email *

Message *