Firebase Crashlytics with Xamarin

Gulam Ali Hakim
Sep 3, 2019 · 4 min read
Image for post
Image for post
Image is taken from https://www.saneen.in/xamarinandfirebase/

Introduction:

Firebase Crashlytics is a lightweight, realtime crash reporter that helps you track, prioritize, and fix stability issues that erode your app quality. Crashlytics saves you troubleshooting time by intelligently grouping crashes and highlighting the circumstances that lead up to them.

Find out if a particular crash is impacting a lot of users. Get alerts when an issue suddenly increases in severity. Figure out which lines of code are causing crashes.

Key capabilities:

  • Curated crash reports: Crashlytics synthesizes an avalanche of crashes into a manageable list of issues, provides contextual information, and highlights the severity and prevalence of crashes so you can pinpoint the root cause faster.
  • Cures for the common crash: Crashlytics offers Crash Insights, helpful tips that highlight common stability problems and provide resources that make them easier to troubleshoot, triage, and resolve.
  • Integrated with Analytics: Crashlytics can capture your app’s errors as app_exception events in Analytics. The events simplify debugging by giving you access a list of other events leading up to each crash, and provide audience insights by letting you pull Analytics reports for users with crashes.
  • Realtime alerts: Get realtime alerts for new issues, regressed issues, and growing issues that might require immediate attention.

Add Firebase to your Xamarin.iOS app

  1. Create a Firebase project in the Firebase console, if you don’t already have one. If you already have an existing Google project associated with your mobile app, click Import Google Project. Otherwise, click Create New Project.
  2. Click Add Firebase to your iOS app and follow the setup steps. If you’re importing an existing Google project, this may happen automatically and you can just download the config file.
  3. When prompted, enter your app’s bundle ID. It’s important to enter the bundle ID your app is using; this can only be set when you add an app to your Firebase project.
  4. In the end, you’ll download a GoogleService-Info.plist file. You can download this file again at any time.

Configure Firebase in your Xamarin.iOS app

Once you have your GoogleService-Info.plist file downloaded in your computer, do the following steps in Visual Studio:

  1. Add GoogleService-Info.plist file to your app project.
  2. Set GoogleService-Info.plist build action behaviour to Bundle Resource by Right-clicking/Build Action.
  3. Add the Xamarin.Firebase.iOS.Core NuGet to your iOS project: https://www.nuget.org/packages/Xamarin.Firebase.iOS.Crashlytics/
  4. Add the following lines of code somewhere in your app, typically in your AppDelegate’s FinishedLaunching method (don't forget to import Firebase.Core and Firebase.Crashlytics namespace):
App.Configure ();
Crashlytics.Configure ();

Test your Firebase Crashlytics implementation

Force a crash to test your implementation, You don’t have to wait for a crash to know that Crashlytics is working. You can use the SDK to force a crash by adding the following code to your app:

Crashlytics.SharedInstance.Crash ();

Note: The above code should be outside a try-catch, Otherwise it will be handled at Runtime.

Enable Crashlytics debug mode

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 mode to track down the problem:

public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
...
Fabric.Fabric.SharedSdk.Debug = true;
...
return true;
}

Log non-fatal exceptions

In addition to automatically reporting your app’s crashes, Crashlytics lets you log non-fatal exceptions.

On iOS, you do that by recording NSError objects, which Crashlytics reports and groups much like crashes:

Crashlytics.SharedInstance.RecordError (error);

When using the RecordError method, it's important to understand the NSError structure and how Crashlytics uses the data to group crashes. Incorrect usage of the RecordError method can cause unpredictable behaviour and may require Crashlytics to limit reporting of logged errors for your app.

What about NSExceptions?

Crashlytics doesn’t offer a facility for logging/recording NSException instances directly. Generally speaking, the Cocoa and Cocoa Touch API’s are not exception-safe. That means the use of in your Objective-C library code or in a third-party Objective-C code can have very serious unintended side-effects in your process, even when used with extreme care. You should never use statements in your Objective-C code. Please refer to Apple’s documentation on the topic.

Firebase Crashlytics for Xamarin.Android

Crashlytics is part of Fabric.io and now Firebase. This folder contains bindings to enable Crashlytics to be used in Xamarin.Android apps.

Setup

  1. Make sure you have a project set up in the Firebase Developer Console.
  2. Download your project’s file and place it in the folder of your Xamarin.Android project.
  3. Add the Xamarin.Android.Crashlytics NuGet package: https://www.nuget.org/packages/Xamarin.Android.Crashlytics/
  4. Restart your IDE
  5. Add google-services.json to your Xamarin.Android app project.
  6. Set the build action google-services.json to GoogleServicesJson
  7. Create a string resource with the name com.crashlytics.android.build_id. The value can be whatever you want to uniquely identify a particular build with.
  8. Clean and Rebuild your solution.

Initialization

You can initialize the Crashlytics SDK with the following code in your main activity’s OnCreate:

Fabric.Fabric.With(this, new Crashlytics.Crashlytics());

Optionally, you can use the included Mono Exception helper to catch unhandled android exceptions and format them more nicely:

Crashlytics.Crashlytics.HandleManagedExceptions();

Test your Firebase Crashlytics implementation

Force a crash to test your implementation, Just like iOS, you don’t have to wait for a crash to know that Crashlytics is working. You can use the SDK to force a crash by adding the following code to your app:

Crashlytics.Crashlytics.Instance.Crash();

Note: The above code should be outside a try-catch, Otherwise it will be handled at Runtime.

Summary:

This blog described and discussed the Firebase Crashlytics setup, configuration and usage in your Xamarin Apps.

In case you think I’ve missed something, go ahead and add it in the comments — I will make sure I add any other content in the post. Also, if you find something incorrect in the blog please go ahead and correct me in the comments.

You can reach out to me on LinkedIn! I am always available! :P

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store