Flutter AAR: Deploy to Maven and GCP Artifact Registry

Sebastian Schneider
Google Cloud - Community
4 min readMay 12, 2020

Adding Flutter to existing apps is now getting used more and more as it’s officially supported by the Flutter team.

However, managing versions and easy integration currently isn’t that easy, especially if you work with a team or multiple workstations.

On Android, you can either add a locally built Maven repository or add the source code directly, both of which you can’t easily distribute to your team. Therefore every Android developer has to install your Flutter repository and built the AAR or source for him-/herself, which also requires a Flutter SDK installed everywhere.

In this tutorial, I will show you how you can implement an easy workflow for your CI/CD process, publish the Flutter AAR to your Maven repository, or the GCP Artifact Registry, which makes the workflow of implementing your Flutter module much more streamlined.

Prerequisites

I already assume you know how to add Flutter to existing apps and have the latest version of Maven installed.
The minimum required version is 3.3.1 , as this adds support for extensions. Check your version with mvn --version

In case you want to use the GCP Artifact Registry, you also have to have installed the latest version of the Google Cloud SDK, have already applied for access to the closed alpha here, and be logged in with gcloud auth login or a service account. The required scope is “Artifact Registry Admin”

Building the local Maven repository

You can build the AAR as usual, with one slight exception:

Normally, the used version number is always 1.0 , but recently support to add a build number was implemented in the stable channel. You want to build your AAR like this:

flutter build aar --build-number x.x.x

This build number is later used in Maven to keep track of the version history, e.g. 1.7.5. Increment it every time you update your codebase.

The command creates a local Maven repository in path_to_your_app/build . Our goal is to upload it to our remote Maven repository, but that isn’t as easy as grabbing the .aar and uploading it.

If you use any plugin, Flutter will create a separate AAR for the plugin with the build number defined above, the only link between them is the .pom file. We have to upload each AAR separately to Maven.

Uploading to our remote Maven repository

With GCP Artifact Registry:

If you want to use your Maven repository instead of the GCP Artifact Registry, you can skip to the “With any Maven repository” step.

Configuring gcloud

You have to create a Maven repository within your GCP project: https://console.cloud.google.com/artifacts/create-repo
You can choose the location which suits you.

Now we have to configure gcloud to use your created repository:

gcloud beta artifacts print-settings mvn --project=PROJECT-ID --repository=REPOSITORY-NAME --location=YOUR-REGION

Enable GCP Artifact Registry Maven extension

To use the GCP Artifact Registry with Maven, we have to add an extension which handles custom repository links:

In your working directory path_to_your_app/build add a subdirectory .mvn

mkdir .mvn

Add a file called extensions.xml to this subdirectory:

<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.0.0 http://maven.apache.org/xsd/core-extensions-1.0.0.xsd">
<extension>
<groupId>com.google.cloud.artifactregistry</groupId>
<artifactId>artifactregistry-maven-wagon</artifactId>
<version>2.0.1</version>
</extension>
</extensions>

Use these Maven config settings

That’s it for the GCP Artifact Registry setup. Now you can continue with the general implementation, using these settings (replacing placeholders):

  • -DrepositoryId="cloud-artifacts"
  • -Durl="artifactregistry://REGION-maven.pkg.dev/PROJECT-ID/REPOSITORY-NAME

With any Maven repository:

Now we have to get every .pom file within our working directory path_to_your_app/build and push it, including the linked AAR, our remote Maven repository.

To find and list all .pom files we run

find . -name "*.pom" -type f

Now we have to execute a Maven command on each of the pom files, also substituting the .pom file ending with .aar for the file to upload.

The deploy command looks like this:

find . -name "*.pom" -type f -exec sh -c 'mvn deploy:deploy-file -DrepositoryId="REPOSITORY-ID" -Durl="REPOSITORY-URL" -DpomFile="$0" -Dfile="${0%.pom}.aar"' '{}' \;

Make sure to replace REPOSITORY-ID and REPOSITORY-URL with your configuration.

That’s it! Now your entire Flutter AAR Maven repository is available on your Maven remote host!

Adding the GCP Artifact Registry Maven repository to Gradle

Now it is quite simple to add the Maven repository to our existing Android App, and resolve our new dependency!

Notice: To use this step the Google Cloud SDK has to be installed and a user has to be authenticated with the correct access rights as mentioned in the prerequisites.

Add the following snippet to your project build.gradle :

plugins {
id "com.google.cloud.artifactregistry.gradle-plugin" version "2.0.1"
}

Now you can use the GCP Artifact registry within your app build.gradle :

apply plugin: 'com.google.cloud.artifactregistry.gradle-plugin'[...]repositories {
[...]
maven {
url "artifactregistry://REGION-maven.pkg.dev/PROJECT-ID/REPOSITORY-NAME"
}
}
dependencies {
debugImplementation 'com.your.package:flutter_debug:+'
releaseImplementation 'com.your.package:flutter_release:+'
profileImplementation 'com.your.package:flutter_profile:+'
[...]
}

If you want to use your maven repository, simply replace the URL with yours.

You also can use your version number instead of + , if you don’t always want to work with your latest release.

--

--