Easily build APKs using Github Actions for flutter projects

Do you believe in wonders ?

We have been developing flutter apps for quite a time now. But creating and releasing a build has not been so fun for developers. Wouldn’t it be wonderful if we could automate that ?

There are techniques to build the apk outside of your local computer in which you develop the flutter projects. CodeMagic could also be used , but sometimes having the apk per release in your git allows your more flexibility.

Github Actions

Github Actions is the essence of this blog. We will be generating apks with the help of it. It is more simple than it sounds. To make the GitHub build the apk for us we can walk through the following steps.

For developers it’s an easy task. It doesn’t need to be done every time. Once you set it up, you will be free like a bird flying in the sky. And to your surprise it’s just a single file.

YML Files and WorkFlows

On your root repository you need to create a .github folder. Usually when you see a folder with the dot prefix it means the folder is hidden. Inside that folder you need to create a folder named workflow. The folder doesn’t mean anything for your project, only for the GitHub. It might not work if you are using other services like Gitlab.

Place your .yml file inside the workflow folder. There you will write at what git event this action will be triggered.Few Git events to name could be push, pull, pull request.

Here is what our yml file looks like. We are naming it build_android.yml which we are planning to trigger on every tag push only.

Line 1 to Line 4
It specifies on which git event to trigger this workflow. Here we are trying to trigger whenever a new tag starting with letter v is pushed.

Line 5 : jobs
Jobs is what gets triggered on the specified git event. For our example the git event will be tag push.

Now we arrive at the heart building APKs. If you have experience with Docker Images then you might have known these things a little, but if you haven’t , hey don’t worry, it’s very easy.

Visualizing a Container in an easiest way possible

Imagine you have just bought a laptop that has no software installed, but you are determined to build a fluter project on it. In that case what you do is :

1 Install the operating system first. Let’s say latest version of ubuntu.

2 After the installation of the Ubuntu, you will need to install the Java. To run android application installing java becomes a must, so say you install java 8 on your fresh laptop.

3 Then you will install Flutter SDK on after the installation of java. Say you install flutter version 1.20.2.

4 Provided that everything is installed, you will be able to create the flutter project. and start building the application.

5 Get all the packages required in flutter project by running command
flutter pub get.

6 Then you create a build and release it by running command
flutter build apk.

Now same is the case with the Virtual Machine/Container (for simplicity) in the cloud. By the help of the yml file we will tell GitHub what to install and in what order to make our project running. Let’s move forward with the line numbers.

Line 7 : runs-on: ubuntu-latest
Installs the latest version of the ubuntu as an operating system in the Virtual Machine.

Line 8 : steps
Following this line are the steps that needs to be followed by the GitHub while running the job.

Now you will see real GitHub Actions in use. Github actions are the tasks GitHub perform effortlessly for us which otherwise would be an arduous task to do our own. There are already numbers of actions available in use. You can also create your actions but you will almost never need to.

Line 9 : uses: actions/checkout@v1
This is the action we are using to checkout our branch.

Line 11 : uses: actions/setup-java@v1
Sets up Java for android application as you would do in the newly bought laptop, as we described above.

Line 15 : subosito/flutter-action@v1
Sets up the flutter sdks and darts for us. Checkout the offical documentation here.

Line 19 to Line 21
Commands you used to do in your local computer to build the apk

Trigger the Workflow

Add the changes and push a commit. You will not see anything because we have set the workflow to be triggered only when a tag is pushed. To test our workflow create a git tag and push and you should expect to run it without any errors.

Command flutter build apk ran successfully yet you are not able to see the list of apk. Why ? It’s because you haven’t told GitHub to release the apk.

Don’t go away yet. Just adding an action will do the task for us. Ncipollo Release Action is what we need. Add the following steps in your yml file and believe in wonders.

Token ?

Well what about the token ? It easy, just follow the steps below

a. Add a secret on the repository. If you want to know how to add secret to your workflow file, read this article.

b. Add that secret name on .yml file.

Final Workflow File

After the completion of job you can find the apks in the release section as below.

That’s it.

You are good to go now.
Thank you .


Let’s Flutter.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store