How to build, sign and distribute your Flutter Android application using Azure DevOps and AppCenter

levelfivecoder
5 min readMar 26, 2020

--

The COVID 19 has cancelled my summer plans, sporting events (Mainly F1). I got bored in a few days. Then I started to learn Flutter. So I created a sample application in Flutter. I choose the idea to create a COVID 19 tracker app with the help of some free exposed APIs. You can clone the code from Github.

I’ve always used to publish applications manually in the past years. But now, we can leverage DevOps tools to improve the build, test and deployment.The source code is hosted on Github. I can use Azure DevOps to connect to Github and create a Pipeline to build it. Let us have a look.

Installing Flutter build task for Azure DevOps

You can download the Flutter task for Azure DevOps from the Visual studio marketplace.

Flutter task link for download.

Creating a new build pipeline in Azure DevOps

First we need to connect the Github repository with Azure DevOps project.Then click the Pipelines menu and create a new build pipeline.

Next we need to connect the repository with the pipeline. We can choose Azure repos git, Github, bitbucket etc

I've selected GitHub, my code is hosted in GitHub and I prefer to use YAML way.

Next step we could create or select a yaml file from the repository or we can use the predefined templates.

When we select Existing Azure Pipelines YAML file then it will asks for the path in the repository, there we can specify the file.

The Azure pipeline YAML file contains all the settings that the build service should use to build and test the application, as well as generate the output artifacts that will be used to deploy the app later release pipeline(CD).

Flutter Build Pipeline

Open the azure-pipelines.yml, on the top of it you will see these lines:

As we can see, every time something is pushed on the master branch a new build will start on a virtual machine with the latest Mac os version.We can also build the Flutter android using windows/Ubuntu vm images.

Install Flutter

We can install the Flutter SDK to the agent by using the FlutterInstall task

Installs the Flutter SDK onto the running agent if not already installed. Then uses it for following tasks

Select the channel as stable and version as latest to get the stable latest version of Flutter SDK, We can specify any particular version and beta version by changing the channel and version options.

Build Flutter Android App

Build the flutter app and generate the build file

We can build the Flutter app using the FlutterBuild Task. The FlutterInstall Task will set the SDK path in the agent's environment values,

A mandatory thing is to set the target and projectDirectorty

projectDirectory should be the path to the folder that contains pubspec.yaml file.

target will be the build target platform, Valid values: “all”, “ios”, “apk”, “aab”.

There are other optional parameters available to set the build name, build flavour, version number etc

Sign APK

Sign the apk

Follow the below steps to sign your Android app.

  1. First, obtain a keystore file that contains your signing certificate. The Android documentation describes the process of generating a keystore file and its corresponding key.
  2. Upload your Keystore file to the Secure Files library. During upload, your Keystore will be encrypted and securely stored.
  3. Go to the Variables tab and add the following variables. In their Value column, enter your Keystore password, Key alias, and Key password. Be sure to click the lock icon. This will secure your passwords and obscure it in logs.
  4. Add the Android Signing task after the task that builds your APK like the above example.
  5. Set the passwords and Keystore details from the variables.

Copy APK files to the Artifacts & Publish.

We have our APK builded and signed, it’s time to get the APK generated on our machine. We first need to copy the APK to the artifact directories with a first task.

Flutter Test

Launch tests and publish a report as build test results. I've not created any testcases since it is a sample project. But we can run the testcases written in the projects using FlutterTest task from Azure DevOps.

Deploy Application to the AppCenter

To deploy the app to appcenter, we need to create a Release Pipeline in the Azure DevOps.

When we select a new build pipeline, We will get a window like below image

New Release Pipeline

We can select the APK file by clicking on the Add an artifact button

Here I've selected the atifact from the previously created CI build. We can mention the build pipeline as the source.

There will be options to set triggers to start release pipeline.

Add Stages to release pipeline

We can deploy the apps to different environment by creating multiple stages. The users can secure the deployment by setting Pre and Post deployment conditions.

Add Appcenter Deploy Task to stage

Add app center deploy task by selecting the task option from the stage.

The app center connection key is required for the deployment task. This service connection can be established by clicking the manage option. If its is already established.

App slug will be the combination of app center username and app name. The format will be username/appname

The release notes can be picked from the artifacts folder. So we need to copy the release notes to the artifacts folder while building the application. Also we can select the binary (APK) file path from the artifacts folder.

We can distribute to some specific groups by mentioning the group ids in the App center distribute Task.

We can also add a stage and task to publish to the play store. For that we need to establish a service connection between the play store and Azure DevOps.

Thanks for reading! I hope you have enjoyed the article :), If you want to see more code, follow me on Github.

--

--

levelfivecoder

Ajith Valliyankal | Mobile Developer | Xamarin | Android | Flutter