Xamarin Android – Integrating Crashlytics

This article details how you can integrate Crashlytics with a Xamarin Android Application. This content also appears in my Using Crashlytics With Xamarin Apps post where I walk through all the steps required to distribute Xamarin apps for beta testing with Fabric.

If you have done this process for iOS, do note that this is a little bit more fiddly and I recommend hanging on to the spoof project you create (as every build requires a rebuild of the spoof app, with updated version numbers).

Installing Fabric For Android Studio

Open up Android Studio and check the top right, if you have fabric installed you should see the following icon.

Screen Shot 2018-06-26 at 12.28.42.png
Once Fabric is installed, it appears on the far left of this toolbar.

If you don’t see this icon, you will need to install Fabric.

Go to File -> Preferences -> Plugins

Search for “Fabric” and install the plugin, reload Android Studio and you are ready to go!

 

Creating An Android Studio Project

Create a new Android Studio project, ensure that the company domain & package name are equal to your Xamarin’s package name. Complete the creation wizard (any options past this screen are irrelevant).

Screen Shot 2018-06-26 at 12.18.56.png

Load up the Fabric plugin and Log In, Select your organisation and now you should be able to add Crashlytics directly to your app.

Screen Shot 2018-06-26 at 12.37.56.png

Click on Crashlytics and on the next page, Install.

Screen Shot 2018-06-26 at 12.39.00.png

Since there is an Android Studio integration, we can let the IDE do the work for us (in terms of installing Crashlytics). On the next page simply hit Apply.

Screen Shot 2018-06-26 at 12.40.11.png

Now you will need to Build & Run your app.

Screen Shot 2018-06-26 at 12.41.43.png

One it has ran on your device, it should say in the Fabric plugin that Crashlytics is installed.

Screen Shot 2018-06-26 at 12.42.35.png

Now we can install Fabric & Crashlytics in our Xamarin Android project!

Configure Android Manifest

Now that we have created our spoof project, we can add our Fabric API keys to our xamarin app. This bit is a bit more sketchy on Android. Before you begin, ensure that the spoof projects build & version numbers match those of your app.

Screen Shot 2018-06-26 at 14.47.33.png

Ensure you do this otherwise you may have difficulty identifying different app builds in Fabric.

In Android Studio, open up your AndroidManifest.xml file, it should look like this:

Screen Shot 2018-06-26 at 14.49.46.png

Copy the api key (Highlighted above) from the manifest and open up the AndroidManifest file in your xamarin project. You will need to use an external editor to do this (I’m using Visual Studio Code) since VSC (For mac at least) won’t let you edit the source. Add the API key to your android manifest:

Api Key

<meta-data android:name="io.fabric.ApiKey" android:value="YOUR_API_KEY_HERE" />

Complete Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.axemasta.axemastaxamfabricdemo">
    <uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:label="Xamarin Fabric Demo">
        <meta-data android:name="io.fabric.ApiKey" android:value="YOUR_API_KEY_HERE" />
    </application>
</manifest>

Save your changes and close any external editors.

NOTE:

You will need to give your app permission to use the internet (if you haven’t already).

Screen Shot 2018-06-26 at 15.07.54.png

Configure Xamarin Project

Since we are essentially tricking Android Studio into distributing our app, we will need to copy over a build script in order to be able for Android studio to accept our app’s APK distributable.

Crashlytics Build Properties

Ensure that your android app has built & ran at least once (it should have done if you have been following along). Navigate to the following directory:

$ProjectFolder -> app -> build -> intermediates -> assets -> debug

Inside should be a file called “crashlytics-build.properties”, you will need to add this file to your xamarin project.

Open the file and check that the version numbers are correct (you can edit the file if they aren’t, it worked for me). Copy the file into the Assets folder of your Xamarin.Droid project.

Screen Shot 2018-06-26 at 15.39.34.png

Right click the “crashlytics-build.properties” file and ensure that the build action is set to “AndroidAsset”

Screen Shot 2018-06-26 at 15.40.36.png

Crashlytics Build Number

Before we complete this step, check if you already have a Strings.xml file already located in your project. In file explorer / finder navigate to:

$XamarinProject -> Droid -> Resources -> Values

If there is a file named “Strings.xml”, ensure that this is added to your xamarin project, otherwise create this file. Inside this file you will want to add the following:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="com.crashlytics.android.build_id">APP_BUILD_NUMBER</string>
</resources>

If you don’t do this, Crashlytics will throw the following (incredibly helpful) error:

"This app relies on Crashlytics. 
Please sign up for access at https://fabric.io/sign_up, 
install an Android build tool and ask a team member to invite you to this app's organization."

 

Install Fabric & Crashlytics To Your App

Firstly you will need to install the Fabric & Crashlytics NuGet packages in your Project.Droid directory.

Screen Shot 2018-03-02 at 16.06.44.png

Both packages are published by an author named “Michel Moreira”, make sure you get the latest versions of each. You only need to install there packages to the Droid project (which is handy).

Open up the MainActivity.cs and import the following namespaces:

using FabricSdk;
using CrashlyticsKit;

And inside the OnCreate method add the following code:

Crashlytics.Instance.Initialize();
Fabric.Instance.Initialize(Android.App.Application.Context);

Your MainActivity.cs will now look something like this (assuming you haven’t dont anything else in the app)

Screen Shot 2018-06-26 at 14.18.31.png

Android Crash Testing

At this point it might be a good idea to check that Crashlytics has been installed correctly.

Add the following line to the OnCreate method, anywhere after you have initialised Fabric & Crashlytics.

Crashlytics.Instance.Crash();

Now run the app (Build as release), it should crash. Make sure you read the console output to verify that the crash was legitimately caused by crashlytics. You may see the following error:

[Fabric] java.lang.IllegalArgumentException: 
Fabric could not be initialized, 
API key missing from AndroidManifest.xml. 
Add the following tag to your Application element

If this is the case, ensure you have the correct API key in your android manifest / it is in the right place!

Once you are sure that Crashlytics is correctly installed, run the app (with the crash line in). You must be building this to release (Crashlytics is disabled in debug). After the app crashes, uncomment the line and re-run the app. Once the app has loaded login to Fabric, find your app and look at the Crashlytics tab, you should see a crash.

Screen Shot 2018-06-26 at 16.40.15.png

Crashlytics is now setup, now our app is ready for distribution.

The End

If this article helped you, please let me know by commenting below.

You can find me on the following sites:

One thought on “Xamarin Android – Integrating Crashlytics

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s