Build your Xamarin.Android application with Azure DevOps

During the development process it is important that you provide a test version for your testers to get feedback and improve your application. It is also important that you don’t create the corresponding packages on your machine instead use a pipeline.

Sebastian Jensen
medialesson
7 min readApr 7, 2022

--

Photo by Mike Benna on Unsplash

Introduction

I’m using Azure DevOps for hosting my projects. So I have all the needed code in the repository. After you pushed your solution to the corresponding Git repository, we are now able to setup our pipeline.

Create the keystore file

Before we can take a look at the process of setting up the pipeline, we need a keystore file. This file is needed for signing your application. We are opening our Xamarin project in Visual Studio. Now we open a terminal by using View > Terminal and in this terminal we are using the keytool tool to generate the keystore file.

We need to remember the alias value, because we need to specify this value later in our pipeline. The parameter validity is in days, so 20000 is a pretty large number, but we need to make sure that the key won’t expire, otherwise we are unable to update our Android application in the Google Play Store.

The following screenshot shows the command in action. First of all we have to specify a password, which we should also remember, because it is needed in the pipeline. This is important because if your keystore file get stolen and the thief knows the password, he is able to update your existing Xamarin.Android app in the Google Play Store. So make sure to store this information securely. Afterwards we need to answer some questions like the name or the organization. We just need to answer one question, so I answer the question for the two-letter country code. After this we have the option to validate all our data and confirm by typing yes. It is possible to secure this file with a different password for a higher security, but it is also legit to use the same password by just hitting enter and the file will be created.

Uploading the keystore file

As I mentioned in the previos paragraph, we need this keystore file in our pipeline. We open Azure DevOps, navigate to our project and then to Library, which is part of the Pipelines tab. We switch to Secure files and click on + Secure file. Now we can upload the created keystore file.

After the upload process we can specify a name of the keystore file, because we need this name in our pipeline.

Now we can switch to the Variable groups tab and we create a new group, called variables. This group contains three variables: KeyStore-Alias, KeyStore-FileName and KeyStore-Password. The variable KeyStore-Password should be created as a secure variable, so nobody can see the password. KeyStore-Alias and KeyStore-Password are filled with the values from the keystore creating process and KeyStore-FileName is the name of the uploaded file.

Creating the pipeline

Now it is time to create the pipeline. We select the Pipelines tab in the left menu. To create a new pipeline we are using the blue button (labeled New pipeline). In the first step we have to provide the location from our code. In my case I’m storing the code in the Azure Repos Git, but you can also use your code from GitHub, BitBucket or every other Git or Subversion source.

To continue I’m selecting Azure Repos Git. Now all possible repositories should be listed. In my case it is just one repository (Xamarin Playground), which I will select.

In the next step Azure DevOps provides some predefined templates for different pipelines. These are pretty basic, so I’m using Starter pipeline here to get a basic structure of the YAML file, but we will change it later on.

An editor will be displayed containing the current version of the YAML file. Now we need to update this file to be able to build our application and create the APK and AAB file. The Android Package with the file extension APK is the file format used by the Android operating system, and a number of other Android-based operating systems for distribution and installation of mobile apps. Google introduced the Android App Bundle (AAB) which is the new format for publishing apps to the Google Play Store. This bundle includes the application’s code and resources. It reduces the initial download size of the app due to the fact that only the needed parts are downloaded to the device.

The following code snippet shows the complete pipeline, but don’t worry I will explain it afterwards.

The top is defining the meta data. In my case I just want to listen to changes on the main branch, but you can define all your possible branches, like dev or staging as well.

The keyword variables connects our created varibale group to the pipeline. Make sure that the group value is matching the name of your variable group.

I’m using Jobs in this pipeline, because later I want to add the steps to create the iOS version into this pipeline as well. But for now we just have one job called BuildAndroid.

Due to the fact the the Android app already targets Android 12, we need Visual Studio 2022 to be able to build the project. That is the reason why we are using windows-2022, but we can also use windows-latest.

The next section contains some variables. In our case it is the current build configuration and the output directory.

Now the magic happens, because we are defining the steps for our pipeline. First of all we need to make sure that NuGet is installed. Afterwards we do a NuGet restore for our solution to install all the needed NuGet packages.

The XamarinAndroid@1 task is used to build the Android project. We specify the path to the csproj file of the Android project and pass the configuration.

The next task downloads the keystore from the secure files. We use the name value to get a reference to that file, because we will need this later.

Our pipeline will be able to create an APK file, which can be sideloaded to an Android device. For this we need to sign this file. Therefore we are using the AndroidSigning@3 task. We are searching for the APK file and we are using the defined variable to get access to the password, the alias and the keystore filename. In my case apksignerKeyPassword and apksignerKeystorePassword are identical, because in the creation process of the keystore file I don’t specify a different password.

The next two steps are copying and publishing the corresponding APK file.

The Google Play Store only accepts AAB files, which can’t be sideloaded to your Android device. But with our pipeline it should be possible to create the AAB file as well. Therefore we need another XamarinAndroid@1 task. But this time we specify some msbuild arguments to create the AAB file. As you can see in the arguments we are using the reference to our downloaded secure file here as well.

The last to steps are just copying and publishing the corresponding AAB file.

Let’s run the pipeline

Now it is time to check the pipeline. Just hit Save and decide if you want to create a separate branch with your pipeline. Now let’s run the pipeline.

As you can see the pipeline succeeded and if you click on the link 1 published in the section Related on the summary page you have access to the APK and the AAB file.

Conclusion

In this post I’ve showed how you can setup a pipeline to build and publish your Xamarin.Android application using Azure DevOps. This pipeline creates both files on the same run, which might be not that useful, because either you want a test version, which is the APK file or you want a store version, which is the AAB file. But you now have all needed tasks to create these files and you can decide which version you need by enabling or disabling the corresponding tasks.

--

--

Sebastian Jensen
medialesson

Senior Software Developer & Team Lead @ medialesson GmbH