Build your .NET MAUI 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 4, 2024

--

Introduction

I’m starting a new .NET MAUI application, which will be hosted on Azure DevOps. So I have all the needed code in the repository. In this blog post I will show you how you can easily setup a pipeline to build your .NET MAUI Android application using the current version of .NET 8. After you pushed your solution to the corresponding Git repository, we are now able to setup our pipeline.

Preparation

We open Visual Studio 2022 and create a new project. As the template we will use .NET MAUI App.

In the next step we provide the project name, for example SimpleMauiApp and the location where the files should be stored.

Finally we select .NET 8.0 as the framework for our application. After pressing the Create button the solution will be opened within Visual Studio.

We can immediately commit these files to git, because we are using this as our base structure to create the 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 our application. We are opening our .NET MAUI project in Visual Studio. Now we open a terminal by using View > Terminal and in this terminal we are using a command line tool to generate the keystore file, called keytool.

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 (round about 54 years), 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 .NET MAUI 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 the generation by typing yes.

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. You can use a every name you like, in my demo case I will call it VariableGroup. 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 (MAUI 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) back in 2018 which is the new format for publishing apps to the Google Play Store. App bundles cannot be installed on Android devices. The Android package (APK) is Android’s installable, executable format for apps. App bundles must be processed by a distributor into APKs so that they can be installed on devices. 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. Make sure to update the VaribaleGroup with your name of the variable group.

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. We also add some other values here directly, for example the .NET version we want to use. This makes it easier to maintain, because if there is a new version available, we can simply update the variable value.

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 BuildMAUIAndroid. We use windows-2022 as the virtual machine.

Now the magic happens, because we are defining the steps for our pipeline.

In the first step we install the desired version of the .NET SDK. Next we install .NET MAUI, by using the dotnet workload command.

The next task downloads the keystore from the secure files. After the download we copy the file to the System.DefaultWorkingDirectory, because we need to access it in the next step.

Finally we are building and publishing our application. We use dotnet publish for that and specify the PROJECT_PATH, the FRAMEWORK and the BUILD_CONFIGURATION, which are defined as variables.

The next two steps are copying and publishing the corresponding APK and AAB files by using the default .NET build and publish process.

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 shown how you can setup a pipeline to build and publish your .NET MAUI Android application using Azure DevOps. This pipeline creates the APK and the AAB file, because you can use the APK file to install it on your test device or the store version, which is the AAB file.

So stay tuned and happy coding!

--

--

Sebastian Jensen
medialesson

Senior Software Developer & Team Lead @ medialesson GmbH