Sun. Nov 24th, 2024

To add crash analytics to your Android project, you can use Firebase Crashlytics, which is a popular and easy-to-implement crash reporting tool. Here’s how to set it up:

Step 1: Set Up Firebase Project

  1. Go to the Firebase Console.
  2. Click on “Add Project” and follow the setup instructions.
  3. Once the project is created, add your Android app by following these steps:
    • In the Firebase Console, select your project, then click on “Add app” and choose “Android.”
    • Enter your Android package name and the SHA-1 key (optional but recommended).
    • Download the google-services.json file provided, and place it in your app’s app directory.

Step 2: Add Firebase Dependencies

In your project’s build.gradle files:

  1. In the project-level build.gradle file, add the Google services plugin:
classpath 'com.google.gms:google-services:4.3.14' 
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.7'
  1. In the app-levelbuild.gradle file:
    • Apply the Google services and Firebase Crashlytics plugins at the top:
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
  1. Add Firebase and Crashlytics dependencies:
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-crashlytics'
  1. Sync the project with Gradle files to download the necessary dependencies.

Step 3: Initialize Firebase in Your Application

In your Application class, initialize Firebase:

import com.google.firebase.FirebaseApp

class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
FirebaseApp.initializeApp(this)
}
}

Don’t forget to add this Application class in your AndroidManifest.xml file:

<application
android:name=".MyApplication"
... >
...
</application>

Step 4: Enable Crashlytics Data Collection (Optional)

Crashlytics collects crash reports by default. If you want to enable or disable collection programmatically, use:

FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true)

Step 5: Test Crashlytics

To test that Crashlytics is working, you can force a crash in your app by adding the following code:

FirebaseCrashlytics.getInstance().log("Test log message")
throw RuntimeException("Test Crash")

This will send a test crash report to Firebase Crashlytics once the app is launched.

Step 6: Check Reports on Firebase Console

After your app crashes with the test crash, go to the Firebase Console > Crashlytics section. It may take a few minutes to display the crash reports.

Additional Configuration

Firebase Crashlytics also allows you to log custom keys, messages, and analytics events that can be associated with crash reports for better debugging.

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *