Integrate Crashlytics for Android by Fabric Tutorial

Udemy Generic 728x90

Objective

In this tutorial, we will learn how to integrate Crashlytics for Android by Fabric in your Android Application through a simple example.




Crashlytics is a crash and issue reporting service provided by Fabric. It provides various simple APIs to reports crashes and also stores user information & other details related to crash. Through Crashlytics you can track all the fatal and non-fatal issues happening in your app and their complete information with device, orientation, carrier etc.

In this tutorial, we will learn how to integrate Crashlytics and track fatal and non-fatal issue in Android application.
 

Step 2

Create New Project

Create a new project in Android Studio, named CrashlyticsDemo.
 

Step 3

Integrate Fabric Plugin

To use Crashlytics in your project, you need to integrate Fabric plugin in Android Studio as this is the easiest way to integrate Crashlytics kit. To integrate Fabric plugin you need to first create an account at fabric.io.

fabric-signup

After creating account, it will send you a confirmation mail. You need to confirm account creation by clicking on the confirmation link received in your inbox. Once, confirmation is complete, you will be redirected to Install Fabric screen and will be asked to choose your IDE.

fabric-platform

Choose Android Studio. Next, you will be asked to go through a list of tasks to integrate the Fabric plugin. Go through all of those steps to integrate plugin in Android Studio.


fabric-pref
fabric-browse
fabric-plugin
fabric-browse1

Your Fabric Plugin integration is now complete.
 

Step 4

Integrate Crashlytics in App

Next step in this tutorial is integrating Crashlytics in app. Click on the Fabric plugin icon in the toolbar of Android Studio and login using your credentials.

Fabric-inte

Once logged it will show organizations added in your account. Select an organization for your app and click on Next button.

fabric-org

Click on the Next button will take you to the list of all the kits available in Fabric plugin. Select Crashlytics from the list and it will take you to Install screen. Click on the Install button to install Crashlytics kit in your app and accept terms and condition.

crashlytics

During installation process, Fabric plugin will show you all the modification needed to be done to integrate Crashlytics kit and ask you to confirm it. To confirm, press on next button and plugin will start modifying app’s code.

crashlytics-code

When all modifications are complete then it will ask you to run your application to complete the integration.

crashlytics-build

Once integration is complete, Crashlytics will start tracking all the crashes happening in the app without any extra code.
 

Step 5

Force a Test Crash

To check whether the integration is working or not, you can throw an uncaught exception. So, add a button in layout file and through exception on the click of button. Open your activity_main.xml file and add the following code snippet :

<br />
&lt;Button<br />
    android:layout_width=&quot;wrap_content&quot;<br />
    android:layout_height=&quot;wrap_content&quot;<br />
    android:text=&quot;Generate Exception&quot;<br />
    android:id=&quot;@+id/button&quot;<br />
    android:layout_alignParentTop=&quot;true&quot;<br />
    android:layout_centerHorizontal=&quot;true&quot;<br />
    android:layout_marginTop=&quot;195dp&quot;<br />
    android:onClick=&quot;generateException&quot;/&gt;<br />

Next, open Main Activity file and the following code snippet to implement uncaught exception that will be caused on the click of the button.

<br />
/**<br />
 * Method to force a test crash to check whether crash reporting<br />
 * is working or not<br />
 * @param view<br />
 */<br />
public void generateException(View view){<br />
    throw new NullPointerException(&quot;test crash&quot;);<br />
}<br />

Now, execute your application and you will see Generate Exception button. Click on the button and it will cause a crash. Now open your application again because Crashlytics sends crash reports to its server only after app is reopened. To check whether the crash is reported or not, open Fabric dashboard and select CrashlyticsDemo app and you will be able to see to your Test Crash registered there.

crashlytics-demo
crashlytics-demo1

 

Step 6

Handle Caught Exceptions

In addition to automatically reporting your app’s crashes, Crashlytics for Android can also log caught exceptions from app’s catch blocks. To log caught exceptions, add Crashlytics.logException() method to your catch block and pass exception as an argument.
Here is the code sample for the same :

<br />
try {<br />
    // Trying to convert a string in JSON object<br />
    // which result will throw an execption<br />
    JSONObject obj = new JSONObject(&quot;something&quot;);<br />
}<br />
catch (Exception e) {</p>
<p>    // Log caught exception<br />
    Crashlytics.logException(e);<br />
}<br />

All caught exceptions logged by Crashlyitcs will appear as non-fatal issue in Fabric dashboad. Your issue summary will contain all the state information you are use to getting from crashes along with breakdowns by Android version and hardware device.




Udemy Generic 728x90

Spread the word. Share this post!