Uploading Xamarin Android builds to Fabric.io Beta

In the past few days, while setting up one of the Jenkins builds for my recently founded software development business, Code Syndicate, I've stumbled across the issue to which I couldn't find an answer to on the Internet: using Fabric Beta with Xamarin projects on Android.

I've found a few articles here and there relating to iOS and the iOS developer set up the build configuration so that an iOS build is pushed to Fabric after the project is compiled from Git, but found nothing in regard to setting up an Android build upload.

After struggling with the issue for a couple of hours and finding a solution to the problem, I've decided to make an article about in hopes that it might help someone else. So ... let's get started!

In order to keep this short, this article only describes the steps taken to achieve the result along with a few explanations as to why each step is required, not the whole process of discovering the solution.

Disclaimer: This is not a tutorial or a definitive guide, it's just what worked for my current setup. Your results might vary.

Getting the tools

After looking at how the Fabric Android plugin works and how it can be used in Fastlane, I've found that most of the work is performed by a jar file conveniently named crashlytics-devtools.jar and that the most convenient way to get that jar is to download the zip file containing it from the ant build tools download page on Fabric's website.

In order to easily use those tools, we'll create a folder named Fabric in our Android project folder from the Xamarin solution and place the jar file there. We can exclude the XML files contained alongside the jar in the zip file as they are ant target definitions and will not be used by the Xamarin build chain.

Injecting Fabric information into the apk

In order for the Fabric dev tools to be able to upload the apk to Beta, a couple of things need to be generated by the dev tools and included in that apk. The Fabric dev tools will check the resulting apk for their presence when trying to upload it. These are:

  • a plain text asset (crashlytics-build.properties) containing various build information about the apk

  • an XML strings resource file (com_crashlytics_export_strings.xml) containing the build ID string named com.crashlytics.android.build_id

To generate those files, we'll need to call a command like this:

java -jar Fabric/crashlytics-devtools.jar -apiSecret <your_organisation_build_secret> -apiKey <your_organisation_api_key> -verbose -projectPath "Fabric" -androidManifest "Properties/AndroidManifest.xml" -androidRes "Resources" -androidAssets "Assets" -generateResourceFile

The values that for your_organisation_build_secret and your_organisation_api_key can be found in Fabric settings section of the website.

After running the command above in the Android project folder for the first time, the files should be generated in their proper places. The Fabric dev tools will also generate a fabric.properties file in your Fabric folder. That can be left as is and will not be used further.

To include those files in the generated apk, we'll need to set the appropriate build action for each file, by right-clicking on the files:

  • the Assets folder should now contain the file crashlytics-build.properties for which we need to set AndroidAsset as the build action

  • the Resources/values folder should now contain the file com_crashlytics_export_strings.xml for which we need to set AndroidResource as the build action

IMPORTANT! The build ID generated by the Fabric dev tools is a unique ID per build so we'll need to add the above step to our Xamarin Android project's build steps. To do that, right-click on the Android project, select Options, select the Build>Custom Commands tab from the left side, then add the command used to generate the files in the first place as a Before Build step, using ${ProjectDir} as the Working Directory.

Creating the first build

Before attempting to upload the first build to Beta I had previously created a blank Android project (using Android Studio + Fabric Plugin) with the same package name, ran and uploaded the apk from that project. Doing this will add the application to Fabric. Attempts to upload an apk without performing this step -might- fail.

In order to create the app entry in Fabric, create a new Android project with the same package name as the package name you are using in your Xamarin project and follow the steps provided on the Fabric website on adding Crashlytics/Fabric to that Android application. As a good measure, you can also upload the apk of the blank/dummy application to Beta as an initial build.

Uploading the apk to Beta

In order to upload the apk to Beta, command similar to this can be run in the Android project folder:

java -jar Droid/Fabric/crashlytics-devtools.jar -apiSecret <your_organisation_build_secret> -apiKey <your_organisation_api_key> -verbose -projectPath "Fabric" -androidManifest "Properties/AndroidManifest.xml" -androidRes "Resources" -androidAssets "Assets" -requireUploadSuccess -uploadDist "bin/Release/your.package.name.for.project-Signed.apk" -betaDistributionReleaseNotesFilePath "path/to/ReleaseNotes.txt"

Since I'm setting this up in Jenkins, the actual command that I've used as a Jenkins build step is somewhat along the line of this:

java -jar Droid/Fabric/crashlytics-devtools.jar -apiSecret <your_organisation_build_secret> -apiKey <your_organisation_api_key> -verbose -projectPath "${WORKSPACE}/Droid/obj/Release/android" -androidManifest "${WORKSPACE}/Droid/obj/Release/android/AndroidManifest.xml" -androidRes "${WORKSPACE}/Droid/obj/Release/res" -androidAssets "${WORKSPACE}/Droid/obj/Release/assets" -requireUploadSuccess -uploadDist "${WORKSPACE}/Droid/bin/Release/your.package.name.for.project-Signed.apk" -betaDistributionReleaseNotesFilePath "${WORKSPACE}/ReleaseNotes.txt"

ReleaseNotes.txt is a file stored in the Xamarin solution folder, which is also the root folder of the Git repository for that project and, thus, the folder that Jenkins uses as the ${WORKSPACE} folder.

Conclusion

By using the steps above we have generated an apk seen as valid by the Fabric dev tools and have uploaded it to Beta.

The process could be improved upon further and made cleaner (for example having a script file in the Fabric folder from out Xamaring Android project in order to avoid storing the Fabric API key and build secret in Jenkins and the Xamarin project itself), but I didn't see that as a priority at that moment.

In the end, I'm happy with how Xamarin, Fabric, Jenkins and Git get along with each other and that I can seamlessly send updated Xamarin builds to my clients whenever code is pushed to the corresponding branch of the repository.


Want to receive updates about new articles? Add me on Twitter or Google+