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.
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).
Load up the Fabric plugin and Log In, Select your organisation and now you should be able to add Crashlytics directly to your app.
Click on Crashlytics and on the next page, Install.
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.
Now you will need to Build & Run your app.
One it has ran on your device, it should say in the Fabric plugin that Crashlytics is installed.
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.
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:
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:
<meta-data android:name="io.fabric.ApiKey" android:value="YOUR_API_KEY_HERE" />
<?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.
You will need to give your app permission to use the internet (if you haven’t already).
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.
Right click the “crashlytics-build.properties” file and ensure that the build action is set to “AndroidAsset”
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.
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:
Your MainActivity.cs will now look something like this (assuming you haven’t dont anything else in the app)
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.
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.
Crashlytics is now setup, now our app is ready for distribution.
If this article helped you, please let me know by commenting below.
You can find me on the following sites: