How did I automate the generation of Release APK

Here is a riddle to solve: I spend most of my time looking at Gradle scripts to run, but never loses hope. Who am I?
That’s right! I am an Android developer.

Android Gradle build times are really slow. Especially when you have a huge code base, less RAM and using Android studio to run the scripts😪. But fortunately, we have terminal. Running gradle scripts using terminal saves lots of time.

Source: How I reduced my Android build times by 89% by Nishant

So, let’s see how we can use the terminal and Gradle script to automate the process of building and generating apk.

Problem Statement:

I have an android project with three different product flavours Staging , QA , Production . All three flavours has its own end points, and other configurations.

Three flavours of a sample app.

Now, I want to,

  1. Generate debug apks for all 3 flavours.
  2. Generate signed apks for all 3 flavours (Let’s consider, all 3 flavours uses same keystore and signing configs)
  3. Generate Production (Signed) apk for release.

In these 3 scenarios, Using AS, If I want to build an apk for a particular flavour, I would go to Build → Select Build variant → Choose module → select build variant. This process would be a bit cumbersome task as I need to redo the process three times in case I want to build for all three flavours. Also, I cannot count on gradle build which is triggered through AS.


Groovy + terminal = ⚡️ 🚀️

Gradle provides a domain specific language, or DSL, for describing builds. This build language is based on Groovy. In our solution will use some gradle scripts to automate the generation of apks using terminal command.

Step 1:

Let us store signing keystore information like path_to_keystore_file, alias, passwords in a properties file out of the version control system.

Signing config properties file.

Step 2:

Let’s write some gradle script to read the above file and use those signing credentials to generate Production signed apk.

A script that automates the generation of Signed apk
Necessary changes to the module build.gradle

Place the above script at the end of your module level build.gradle file. The above script finds the property file, and sets the config provided. Also notice that I have added lintOptions which ignores errors at build time. This is needed for our automate script to work.

Step 3:

Now, Let’s ditch our good old Android studio way of building an application. Let’s use terminal instead for all our build purposes. Once you complete editing the build.gradle and after the sync completes, Gradle creates the following build variants.

Product flavours

If we go back and relook at our problem statement, we can now achieve all three cases in terminal like below. It’s as simple as u run any other terminal command.


  1. Productivity — Saves build time using terminal
  2. Confidential — Keeping properties file out of the version control(git, svn etc.,) and you still be able to sign the apk using the script
  3. CI & CD — When using Jenkins or any other tool for continuous integration, terminal commands and scripts come in handy to generate multiple apks for targeted testings.
  4. Signed APK — Generates signed apk without providing the signing configs each and every time.

That’s all folks!!

If you liked the article, show some love by clicking on the 👏 button(Did you know you can 👏 up till 50?). It will motivate me to write more articles like this.

Read my other blog posts here.

Let’s become friends on Twitter, Linkedin, Github.