Building Flutter APKs and IPAs on Travis
APK and IPA are application packaging formats for Android and iOS respectively. When you are done building (and testing!) your Flutter app and ready to publish it to the Play Store and the App Store, you need to build an APK and an IPA. You can setup a continuous build of APKs and IPAs on Travis.
Officially Travis only supports the Android SDK on Linux. We need to set
android, tell Travis to accept Android licenses and install a sufficient set of components. Assuming your Flutter app is at the root of the git repository, the
.travis.yml file below has everything you need:
The following line installs Flutter itself:
git clone https://github.com/flutter/flutter.git -b alpha --depth 1
Finally this line builds the APK:
./flutter/bin/flutter -v build apk
IPAs can only be built on macOS and you need Xcode command-line tools installed. Luckily, you don’t need to know how Xcode command-line tools work. Flutter wraps all the xcode commands in a nice CLI. That said, if you are proficient with Xcode, a Flutter project is still a regular Xcode project, which you can build directly using the Xcode UI and command-line tools.
Now, let’s tell Travis to give us a macOS (still known to Travis as
osx) environment with Xcode and other Flutter dependencies. Again, assuming your Flutter project is at the root of the git repository, the following
.travis.yml file is all you need:
In this example, we are building an “unsigned” IPA file. This means that you need to sign it before you can run it on a physical iOS device or publish to Apple’s App Store. Code signing is a “fascinating” topic that’s out of scope of this post, but you can learn about it here.
Building both using a build matrix
“But wait,” you say, “isn’t Flutter all about building both for Android and iOS?”
Why yes it is! So, if we’re building simultaneously for both platforms, can we build IPAs and APKs simultaneously too? Of course! The issue though, is that one builds on Linux, and the other one builds on Mac. For that, Travis has a feature called build matrix. A build matrix can run multiple build jobs as part of a single build. So we can merge the above two
.travis.yml files into one file with a build matrix, like so:
Making builds fast
The approach above works. However, there’s a little problem. All this Android and Xcode SDK installation and building takes time. If your project uses a pull request system, you might want pull requests to be checked for errors quickly and allow it to be merged. In my previous post I talked about Flutter’s super-fast UI testing framework. Running a Travis build that runs Flutter tests only takes a minute or two, which includes installing Flutter itself. APK/IPA building can add more than 10 minutes to the process. That can be annoying. What can we do about it?
I’ll show you one way to speed up your build - create a third job in the build matrix, which only runs tests. You set the
fast_finish flag on your build, and mark all jobs except that one test job as optional using the
allow_failures option. Travis will still run all the jobs and build APKs and IPAs. However, it won’t wait for APK/IPA jobs to finish. Once the test job succeeds or fails, it will immediately paint your pull request in the respective color. Here’s the full
A downside to this approach is that your build is still considered successful even when an IPA or an APK fails to build. You can use custom shell scripts and check for specific Travis environment variables to get around this restriction. In the interests of keeping this post short, I’ll have to omit the details.
I hope this information helps you setup a very productive development cycle for your Flutter projects.