How to Use Crashlytics in Firebase (Tutorial)

Are you interested in learning how to use Firebase Crashlytics? You've arrived to the right location.

You're well aware that the impact of a crush and a crash in your life is nearly equivalent when you first encounter them — to shock you.

Crashlytics in Firebase

You can't sleep because your app crashed and you need to fix it before your boss knows, and you can't sleep because you have a crush. Life is a sad thing.

Of course, this post isn't about how to acquire your crush; instead, it's about how to fix technical problems.

This is where Firebase Crashlytics can help.

You may view a detailed picture of crashes, which helps you make your software more stable.

Furthermore, you can track down the source of the crashes, similar to how the FBI hunts down a hacker.

A competent developer understands that pleased customers stay on your app longer and recommend it to their friends, which helps your app rank higher in the Google Play Store.

What is Firebase Crashlytics, and How does it Work?

Firebase Crashlytics is a Firebase tool that collects, analyses, and organises your app crash information on the fly.

It also helps you figure out which issues are the most critical so you can prioritise and resolve them before they become a headache for your users.

The best part is that we provide you with a fantastic interactive real-time dashboard that notifies you of any new concerns, no matter how minor or major.

Furthermore, crashes are intelligently categorized across different versions of your software, each with its own stack trace that can be viewed in the dashboard.

You may add context to each of the stack traces using Crashlytics' logs and key-value pairs, and figure out what your app was doing before it crashed.

When your app crashes, this additional data is delivered to Crashlytics, which is displayed on the dashboard and provides you with the context you need to assist discover the specific cause of the crash.

In the same way, non-fatal mistakes are recorded in the dashboard. Crashlytics will record native stack trace if your app is developed using NDK on Android.

To put it another way, you can rely on Crashlytics to keep your app stable.

You'll be able to spend less time troubleshooting and more time doing the things you enjoy.

[Step-by-Step] How to Use Firebase Crashlytics

Make sure your app is already registered and configured with Firebase before beginning the guide.

For instance, if your app makes use of any of Firebase's products, it has already been registered with Firebase.

If not, create a new project and use Firebase to integrate your app.

Crashlytics may be implemented in four stages after you've integrated Firebase into your app.

Step 1 – Create a Crashlytics Account in the Console

  1. Select your project from the Console.
  2. After that, go to the left side of the navbar and click Crashlytics.
  3. Select your app from the top dropdown menu if you have more than one app registered with your Firebase project.
  4. In my situation, I'm adding new Crashlytics to my app, thus I click Next after selecting "No establish a Firebase app."

Step 2 – In the Android project, include the Crashlytics SDK

2.1) Setting in Build.Gradle(Project level)

  • Now you may construct your Android Studio project. Add Google Maven's repository to the build script -> repositories in the Gradle(project level) file.
buildscript {
    repositories {
        google()
        jcenter()

        // Add Google's Maven repository.
        maven {
            url 'https://maven.fabric.io/public'
        }
}
  • Then, inside the build, add Google Services and Fabric Crashlytics. Now sync the Gradle file (project level).
dependencies
{
        classpath 'com.android.tools.build:gradle:3.6.2'
        // Add the Google Services plugin (check for v3.1.2 or higher).
        classpath 'com.google.gms:google-services:4.3.3'
        //The Fabric Crashlytics plugin should be installed.
        classpath 'io.fabric.tools:gradle:1.31.2'
        
        //NOTE: Your application dependencies should not be placed here; they should be placed elsewhere.
        //in the build.gradle files for each module.
}

Here's the whole shebang. File for Gradle (Project level) You can check my settings if you have any problems.

//Configuration settings common to all sub-projects/modules can be added to the top-level build file.

buildscript 
{
    repositories 
{
        google()
        jcenter()

        //Add the Maven repository from Google.
        maven {
            url 'https://maven.fabric.io/public'
        }
    }

    dependencies 
{
        classpath 'com.android.tools.build:gradle:3.6.2'

        //Install the Google Services add-on (check for v3.1.2 or higher). 'com.google.gms:google-services:4.3.3' is a classpath.
        //Add the Fabric Crashlytics plugin. classpath 'io.fabric.tools:gradle:1.31.2'
        
        //NOTE: Your application dependencies should not be placed here; they should be placed elsewhere.
        //in the build.gradle files for each module
    }
}

allprojects 
{
    repositories 
{
        google()
        jcenter()
    }
}

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

2.2) Setting in Build.Gradle(App level)

  • Crashlytics dependencies should be included to Build. Gradle (app level) and then Sync Now in the upper right corner.
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
//Add the Fabric plugin.
apply plugin: 'io.fabric'

dependencies 
{
    //Add the Google Analytics dependency (recommended).
    implementation 'com.google.firebase:firebase-analytics:17.3.0'

    //Add the dependency for Firebase Crashlytics.
    implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
}

Here is my complete Build.Gradle(app level) file.

apply plugin: 'com.android.application'
// Add the Fabric plugin.
apply plugin: 'io.fabric'

android 
{
    compileSdkVersion 29
    defaultConfig 
{
        applicationId "com.example.demodemo"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes 
{
        release 
{
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies 
{
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.1.0'

    //implementation 'com.googel.android.gms:play-services:12.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.firebase:firebase-database:19.2.1'
    implementation 'com.google.firebase:firebase-messaging:20.1.3'
    testImplementation 'junit:junit:4.12'
    implementation 'com.google.firebase:firebase-core:17.2.3'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    //add for remote configuration
    implementation 'com.google.firebase:firebase-config:19.1.3'
    implementation 'com.google.firebase:firebase-analytics:17.2.3'

    //Add the Google Analytics dependency (recommended).
    implementation 'com.google.firebase:firebase-analytics:17.3.0'
    //Add the dependency for Firebase Crashlytics.
    implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
}
apply plugin: 'com.google.gms.google-services'

Note that you do not need to initialise Crashlytics; it will do so automatically once it is installed.

Step 3 – Do Some Code

To keep this tutorial basic, I'm going to add a basic button to the XML layout.

The activity main.xml file can be found below.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="40dp"
    tools:context="MainActivity">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Crash button"
        android:id="@+id/fetchButton"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Now I'm writing a few lines of code to check for a crash in order to test the crash result. Simply copy and paste the code below into the OnCreate() function of MainActivity.java. I'm adding this line for the purposes of testing. Otherwise, you won't need to put these lines of code in the real programme because they'll be called whenever there's a crash. The Firebase console will keep track of it.

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      //just to throw in a crash
      Crashlytics.getInstance().crash();
    }
});

Below is the complete MainActivity.java code

package com.example.demodemo;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

import com.crashlytics.android.Crashlytics;

public class MainActivity extends AppCompatActivity 
{
    private Button button;

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

        button = findViewById(R.id.fetchButton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //just to throw in a crash
                Crashlytics.getInstance().crash();
            }
        });
    }
}

Step 4 – Build and Run Your Project

Now you may use the app.

After that, launch the app and press the button. Both the app and the Logcat window of the android studio crash.

Now navigate to the Firebase Console and verify that the installation was completed successfully.

After that, select “Go to Crashlytics dashboard” from the drop-down menu.

When you go to the Crashlytics dashboard now, you can immediately see the crashes. You can filter the Crashlytics dashboard by version when you arrive from the top.

Now scroll down to one of your crashes and click on it.

When you click on the crash, a full stack trace appears. A comprehensive detailed graph, devices, device operating system, device status, log file, data, key, and so on are all available.

You can see a lot of data that is automatically created by analytics.

Post a Comment

Previous Post Next Post