/firebase-crashlytics

Enable Firebase Crashlytics for Capacitor apps

Primary LanguageJavaMIT LicenseMIT


Firebase Crashlytics

@capacitor-community/firebase-crashlytics

Capacitor community plugin for native Firebase Crashlytics.


Maintainers

Maintainer GitHub Social
Priyank Patel priyankpat @priyankpat_
Stewan Silva stewwan @StewanSilva
Daniel Pereira danielprrazevedo @DandanPrr

Installation

Using npm:

npm install @capacitor-community/firebase-crashlytics

Using yarn:

yarn add @capacitor-community/firebase-crashlytics

Sync native files:

npx cap sync

On iOS, no further steps are needed.

On Android, add firebase-crashlytics-gradle to build.gradle and register the plugin in your main activity:

build.gradle (root of the project):

buildscript {
  repositories {
    google()
    jcenter()
  }

  dependencies {
    ...

    // Add the line below
    classpath 'com.google.firebase:firebase-crashlytics-gradle:2.2.0'
  }
}

build.gradle (inside app directory):

// Add this at the bottom of the file
apply plugin: 'com.google.firebase.crashlytics'

MainActivity.java:

import com.getcapacitor.community.firebasecrashlytic.FirebaseCrashlytics;

public class MainActivity extends BridgeActivity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Initializes the Bridge
    this.init(
        savedInstanceState,
        new ArrayList<Class<? extends Plugin>>() {

          {
            // Additional plugins you've installed go here
            // Ex: add(TotallyAwesomePlugin.class);
            add(FirebaseCrashlytics.class);
          }
        }
      );
  }
}

Configuration

Download google-services.json and GoogleService-Info.plist and place it in their respective platform app directory.

  • Android: android/app
  • iOS: ios/App/App

Examples

Click here for an example on how to implement this plugin.

You can also clone the repository:

git clone https://github.com/priyankpat/capacitor-plugins-example
git checkout -b firebase-crashlytics

Supported methods

Name Android iOS Web
crash
setContext
setUserId
addLogMessage
setEnabled
isEnabled
didCrashDuringPreviousExecution
sendUnsentReports
deleteUnsentReports
recordException

Usage

import { Plugins } from "@capacitor/core";

const { FirebaseCrashlytics } = Plugins;

/**
 * Platform: Android/iOS
 * This method will throw an exception triggering crashlytics to log the event.
 * @params message - string message to record (mandatory)
 * @returns void
 */
FirebaseCrashlytic.crash({
  message: "This is a crash message: Capacitor is awesome! 😃",
});

/**
 * Platform: Android/iOS
 * Records a non-fatal report to send to Crashlytics.
 * If automatic data collection is disabled, this method queues up all the reports on a device to send to Crashlytics.
 * @params (android) message - message to record for non-fatal error
 * @params (ios) code - the error code (optional) (default: -1001)
 * @params (ios) domain - a string containing the error domain (optional)
 * @params (ios) message - message to record for non-fatal error
 * @returns none
 */

// Android Example
FirebaseCrashlytics.recordException({
  message: "This is a non-fatal message",
});

// iOS Example
FirebaseCrashlytics.recordException({
  message: "This is a non-fatal message",
  code: 401, // optional, default is -1001
  domain: "capacitor", // optional
});

/**
 * Platform: Android/iOS
 * Sets a custom key and value to be associated with subsequent fatal and non-fatal reports. When setting an object value, the object is converted to a string.
 * @param keys - a unique key associated to the report
 *        value - a unique value associated to the key (string | number | boolean)
 *        type - type of value ('string' | 'long' | 'double' | 'boolean' | 'int' | 'float')
 * @returns none
 */
FirebaseCrashlytics.setContext({
  key: "theme",
  value: "dark",
  type: "string",
});

FirebaseCrashlytics.setContext({
  key: "battery",
  value: 32,
  type: "int",
});

/**
 * Platform: Android/iOS
 * Records a user ID (identifier) that's associated with subsequent fatal and non-fatal reports.
 * @param userId - unique identifier
 * @returns none
 */
FirebaseCrashlytics.setUserId({
  userId: "507f191e810c19729de860ea", // e.g. mongodb document id for a specific user
});

/**
 * Platform: Android/iOS
 * Logs a message that's included in the next fatal or non-fatal report.
 * @params message - string message to record
 * @returns none
 */
FirebaseCrashlytics.addLogMessage({
  message: "This is a test message: Capacitor is awesome! 😃",
});

/**
 * Platform: Android/iOS
 * Enables/disables automatic data collection by Crashlytics.
 * @params enabled - true/false to enable/disable reporting
 * @returns none
 */
FirebaseCrashlytics.setEnabled({
  enabled: false,
});

/**
 * Platform: iOS
 * Indicates whether or not automatic data collection is enabled
 * @params enabled - true/false to enable/disable reporting
 * @returns none
 */
FirebaseCrashlytics.isEnabled();

/**
 * Platform: Android/iOS
 * Enqueues any unsent reports on the device to upload to Crashlytics.
 * This method only applies if automatic data collection is disabled.
 * @params enabled - true/false to enable/disable reporting
 * @returns none
 */
FirebaseCrashlytics.sendUnsentReports();

/**
 * Platform: Android/iOS
 * Deletes any unsent reports on the device.
 * This method only applies if automatic data collection is disabled.
 * @params enabled - true/false to enable/disable reporting
 * @returns none
 */
FirebaseCrashlytics.deleteUnsentReports();

Enable Crashlytics debug logging

If your forced crash didn't crash, crashed before you wanted it to, or you're experiencing some other issue with Crashlytics, you can enable Crashlytics debug logging to track down the problem.

iOS

To enable debug logging on your development device, specify the following command line argument in Xcode:

-FIRDebugEnabled

Android

To enable debug logging on your development device, set an adb shell flag before running your app:

adb shell setprop log.tag.FirebaseCrashlytics DEBUG

To view the logs in your device logs, run:

adb logcat -s FirebaseCrashlytics

To disable debug logging, set the flag back to INFO:

adb shell setprop log.tag.FirebaseCrashlytics INFO