Setup a Flutter CI/CD with GitLab CI — Part 2
Building iOS & Android Flutter apps
In the previous post, we learnt how to setup our GitLab runner on our machine. In part 2, we will install all dependencies needed to build Flutter apps for Android & iOS.
Installing Flutter SDK
Navigate to the Flutter website, download and install Flutter (https://flutter.dev/docs/get-started/install/macos)
In this example, we will install the Flutter SDK at the home directory level (~/flutter). You are free to install at any desired location.
export PATH="$PATH:$HOME/flutter/bin"
Add the above export statement to ~/.bash_profile which will ensure Flutter is available for any future sessions.
Tip: In order to run a Flutter command, you can reload your bash environment by running the following command line:
$ source ~/.bash_profile
When your bash_profile is reloaded. Run the command “flutter doctor” to ensure it works correctly.
$ flutter doctor
Below is an example resulting from “flutter doctor” command line
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.5.4-hotfix.2, on Mac OS X 10.14.4 18E226, locale en-GB)
[✗] Android toolchain - develop for Android devices
✗ Unable to locate Android SDK.
Install Android Studio from: https://developer.android.com/studio/index.html
On first launch it will assist you in installing the Android SDK components.
(or visit https://flutter.dev/setup/#android-setup for detailed instructions).
If the Android SDK has been installed to a custom location, set ANDROID_HOME to that location.
You may also want to add it to your PATH environment variable.
[✗] iOS toolchain - develop for iOS devices
✗ Xcode installation is incomplete; a full installation is necessary for iOS development.
Download at: https://developer.apple.com/xcode/download/
Or install Xcode via the App Store.
Once installed, run:
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
✗ libimobiledevice and ideviceinstaller are not installed. To install with Brew, run:
brew update
brew install --HEAD usbmuxd
brew link usbmuxd
brew install --HEAD libimobiledevice
brew install ideviceinstaller
✗ ios-deploy not installed. To install:
brew install ios-deploy
✗ CocoaPods not installed.
CocoaPods is used to retrieve the iOS platform side's plugin code that responds to your plugin usage on the Dart side.
Without resolving iOS dependencies with CocoaPods, plugins will not work on iOS.
For more info, see https://flutter.dev/platform-plugins
To install:
brew install cocoapods
pod setup
[!] Android Studio (not installed)
[!] Connected device
! No devices available
! Doctor found issues in 4 categories.
Installing Android SDK
The Android SDK is required to build Android Apps.
There are many ways to install the Android SDK, manually or via Homebrew. In this example, we will use Homebrew as it’s easier to install and to manage dependencies.
Run the following command line to install the Android SDK.
$ brew cask install android-sdk
The next step is to install the Java Development Kit (JDK) version 8 which is required to run Android SDK because, at this present time, Android SDK does not support versions higher than 8.
Due to the fact, JDK 8 is presently inaccessible for public use on Oracle’s website, we will install AdoptOpenJDK from a community which builds free JDK binaries.
Run the following command line to install AdoptOpenJDK version 8.
$ brew cask install adoptopenjdk8
Next step, we need to update the bash_profile which enables Flutter to build Android apps by running the command shown below.
$ echo 'export ANDROID_HOME="/usr/local/share/android-sdk"' >> ~/.bash_profile
Next step, we need to install the latest Android toolchain with Android 9 (Pie). Run the following command shown below.
$ sdkmanager “platforms;android-28” “platform-tools” “build-tools;28.0.3” # Android Pie
Then, to build an Android app, you will need to accept the licenses agreements with Android by running the following command shown below.
$ flutter doctor --android-licenses
At this stage, run flutter doctor
to verify that the android toolchain works by running the following command shown below.
$ flutter doctor
You should see a summary without any issues, an example is shown below.
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.5.4-hotfix.2, on Mac OS X 10.14.4 18E226, locale en-GB)
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[✗] iOS toolchain - develop for iOS devices
✗ Xcode installation is incomplete; a full installation is necessary for iOS development.
Download at: https://developer.apple.com/xcode/download/
Or install Xcode via the App Store.
Once installed, run:
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
✗ libimobiledevice and ideviceinstaller are not installed. To install with Brew, run:
brew update
brew install --HEAD usbmuxd
brew link usbmuxd
brew install --HEAD libimobiledevice
brew install ideviceinstaller
✗ ios-deploy not installed. To install:
brew install ios-deploy
✗ CocoaPods not installed.
CocoaPods is used to retrieve the iOS platform side's plugin code that responds to your plugin usage on the Dart side.
Without resolving iOS dependencies with CocoaPods, plugins will not work on iOS.
For more info, see https://flutter.dev/platform-plugins
To install:
brew install cocoapods
pod setup
[!] Android Studio (not installed)
[!] Connected device
! No devices available
! Doctor found issues in 3 categories.
The next step is to install all the dependencies needed for iOS apps.
Note: if you have no write permissions for the folder, you can run the command line shown below to change the owner of the folder to the current shell user.
sudo chown -R $(whoami) [DIRECTORY_OF_FOLDER]
Installing Xcode (Two ways)
There are two ways to install the Xcode, via the Mac AppStore or manually.
In this example, we will manually install Xcode via the xcode-install
program by running the following command shown below.
$ gem install xcode-install
Next step, is to install the desired Xcode version by running the following commands.
$ xcversion list # Get the list of available Xcode version from Apple Developer portal$ xcversion install 10.2.1 # Latest version available on May 2018 but feel free to use a more recent version
Installing Cocoapods
CocoaPods is one of most popular dependencies manager for Objective-C and Swift project. In this section, we will install Cocoapods by running the command line below.
$ brew install cocoapods
Then run flutter doctor
command line.
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.5.4-hotfix.2, on Mac OS X 10.14.4 18E226, locale
en-GB)
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[!] iOS toolchain - develop for iOS devices (Xcode 10.2.1)
✗ libimobiledevice and ideviceinstaller are not installed. To install with Brew,
run:
brew update
brew install --HEAD usbmuxd
brew link usbmuxd
brew install --HEAD libimobiledevice
brew install ideviceinstaller
✗ ios-deploy not installed. To install:
brew install ios-deploy
✗ CocoaPods installed but not initialized.
CocoaPods is used to retrieve the iOS platform side's plugin code that
responds to your plugin usage on the Dart side.
Without resolving iOS dependencies with CocoaPods, plugins will not work on
iOS.
For more info, see https://flutter.dev/platform-plugins
To initialize CocoaPods, run:
pod setup
once to finalize CocoaPods' installation.
[!] Android Studio (not installed)
[!] Connected device
! No devices available! Doctor found issues in 3 categories.
From the resulting of flutter doctor summary, you notice that there are warnings for libmobobiledevice
and ideviceinstaller
. Ignore this as are not necessary for a CI machine.
Also notice, Cocoapods is installed but not initialized. You can ignore this warning as we have not yet created and ran a Flutter project. From a CI perspective, dependencies will be automatically downloaded for the iOS project.
You’ve just set up a Flutter environment on your CI machine! 🎉
On the next post, we will learn how to set up GitLab CI to test and build Android & iOS apps.