Flutter continuous build / deployment using AppCenter

Flutter is Google’s UI toolkit for building beautiful, natively compiled applications for mobile, web, and desktop from a single codebase. After extensively working on Xamarin; i decided to give flutter a go an was pleasantly surprised by ease-of- development. Though new to Dart; i realized that  the learning curve is extremely flat for someone already well aware of C# / Java / Typescript.

One thing i missed was contineuous deployment via appcenter.ms. Appcenter (previously known as Hockey app) provides interface to deploy and manage apps. Appcenter has built in support for native iOS Swift / Objective C and Android Java / Kotlin based apps along with Xamarin Forms.

App Center lacks suport for flutter apps for now. So i decided to figure out how to enable flutter app deployment via AppCenter

In this exercise; I will be connecting to Github to get the code from github; compile it and build apk (for android) / or ipa (for iOS)

First; Get flutter SDK, and make an app and run it; or alternately, if you already have an app open .gitignore file present at the base folder of the flutter app.

Remove or comment out gradle-wrapper.jar, gradlew and gradlew.bat from the .gitignore file and add the file to github Appcenter can recognize an app for android build only when it detects these files from the Github code.

On appcenter, the custom scripts can be hooked to the build at three stages

Stage 1: Post Clone with filename appcenter-post-clone.sh

Stage 2: Pre Buildwith filename appcenter-pre-build.sh

Stage 3: Post Build with filename appcenter-post-build.sh

Since we want to get flutter SDK and set it up just after cloning our app code we will harness the Stage 1: Post Clone stage. That means we will add a file named appcenter-post-clone.sh to project/android/app

The contents of the file is as (you can also get it from https://github.com/Microsoft/appcenter-build-scripts-examples/tree/master/flutter

#!/usr/bin/env bash
#Place this script in project/android/app/

cd ..

# fail if any command fails
set -e
# debug log
set -x

cd ..
git clone -b master https://github.com/flutter/flutter.git
export PATH=`pwd`/flutter/bin:$PATH

flutter channel stable
flutter doctor

echo "Installed flutter to `pwd`/flutter"

flutter build apk --release

#copy the APK where AppCenter will find it
mkdir -p android/app/build/outputs/apk/; mv build/app/outputs/apk/release/app-release.apk $_

After adding the project structure will be like 

 

Once the above changes are made add the file to github and commit / push the code.

On the appcenter.ms; make a new project by choosing android java kotlin based app and choose github to fetch the code. 

Once the above is configured. Choose the build settings. 

The settings is the “settings cog” which ONLY appears on row hover. 

 

on settings; choose the appropriator settings and click on save and build:

 

Once the build has started the script will fetch flutter and compile the app and produce apk which can be distributed.

 

Thus we can build an flutter app on appcenter..