Post

Preparing a self hosted Azure Agent for Flutter and Android SDK

Outline

In this blog post I will show you how to prepare a self hosted Azure Agent for Flutter and Android SDK. This is useful if you want to run your Flutter builds, tests and even publish to the Google Play Store on a self hosted Azure Agent. The following steps will be covered:

Install Flutter

The first step is to install Flutter. This can be done by following the official guide on the Flutter website. But here is a quick summary of the steps:

Create a directory for the Flutter SDK

1
2
mkdir ~/flutter_sdk
cd ~/flutter_sdk

Download the Flutter SDK

1
wget https://storage.googleapis.com/flutter_infra_release/releases/stable/linux/flutter_linux_3.19.5-stable.tar.xz

Extract the Flutter SDK archive

1
tar xf flutter_linux_3.19.5-stable.tar.xz

Add the Flutter SDK to your PATH

This is best done by editing your .bashrc file:

Edit the .bashrc file

1
nano ~/.bashrc

and adding the following line at the end of the file:

1
export PATH="$PATH:`pwd`/flutter/bin"

Execute the .bashrc file

1
source ~/.bashrc

Now you should be able to run flutter doctor and see the output.

Run flutter doctor

1
flutter doctor

If you are on WSL2 and get this error:

1
/usr/bin/env: ‘bash\r’: No such file or directory

Then you need to edit the /etc/wsl.conf file

1
sudo nano /etc/wsl.conf

and add the following lines:

1
2
[interop]
appendWindowsPath=false

Then restart the WSL2 instance:

1
wsl --shutdown

Install Android SDK

Install the prerequisites

This guide assumes you are running Ubuntu. If you are running a different Linux distribution, the package manager commands might be different. We need to install the following prerequisites:

  • OpenJDK 17
  • Unzip
1
2
3
sudo apt update
sudo apt install openjdk-17-jdk
sudo apt install unzip

Create a directory for the Android SDK

1
2
3
cd ~/
mkdir ~/android
cd ~/android

Download the Android SDK

Get the latest version of the Android SDK from the Android Developer website. Scroll down to the “Command line tools only” section and download(get the URL) the latest version for Linux.

1
wget https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip

(Amend the URL to the latest version.)

Extract the Android SDK archive

1
unzip commandlinetools-linux-11076708_latest.zip

Re-arrange the folder structure

The Android SDK expects the cmdline-tools folder to be inside another cmdline-tools folder. So we need to re-arrange the folder structure.

1
2
3
4
5
6
mkdir android_sdk
mv cmdline-tools android_sdk
cd android_sdk
mv cmdline-tools latest
mkdir cmdline-tools
mv latest cmdline-tools

The folder structure should now look like this:

1
2
3
4
5
6
7
8
9
10
android
├── android_sdk
│   ├── build-tools
│   ├── cmdline-tools
│   ├── emulator
│   ├── licenses
│   ├── platform-tools
│   ├── platforms
│   └── tools
└── commandlinetools-linux-11076708_latest.zip

And the cmdline-tools folder should contain the latest folder.

1
2
3
4
5
6
android/android_sdk/cmdline-tools/
└── latest
    ├── NOTICE.txt
    ├── bin
    ├── lib
    └── source.properties

Install the minimum required SDK components

1
2
cd ~/android/android_sdk/cmdline-tools/latest/bin
./sdkmanager "platform-tools" "platforms;android-34" "build-tools;34.0.0"

Accept the licenses

1
./sdkmanager --licenses

Configure Flutter to use the Android SDK

1
flutter config --android-sdk ~/android/android_sdk

Accept the Android SDK licenses

1
flutter doctor --android-licenses

Run flutter doctor again

1
flutter doctor

You should now see that the Android SDK is installed and configured correctly.

1
2
3
4
5
6
7
[✓] Flutter (Channel stable, 3.19.5, on Ubuntu 22.04.4 LTS 5.15.146.1-microsoft-standard-WSL2, locale C.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✗] Chrome - develop for the web (Cannot find Chrome executable at google-chrome) <SNIP>
[!] Android Studio (not installed)
[✓] Connected device (1 available)
[✓] Network resources

For our purposes, we don’t need Chrome or Android Studio. But you can install them if you need them.

Conclusion

That’s it! We have now prepare our Linux based self hosted Azure DevOps Agent that is ready to build, test and publish Flutter apps to the Google Play Store.

See the link below for the official Microsoft documentation on how to setup a self-hosted Linux agent.

References

Here’s the offical Microsoft documentation on self-hosted Linux agents.

This post is licensed under CC BY 4.0 by the author.