How did I automate the generation of Release APK

Naveen T P
Nov 24, 2018 · 4 min read
Image for post
Image for post

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.

Image for post
Image for post
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.

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

Image for post
Image for post
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.

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

Image for post
Image for post
Signing config properties file.

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.

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.

Image for post
Image for post
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.

Image for post
Image for post
  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.

Our community publishes stories worth reading on Android…

Naveen T P

Written by

Android / iOS developer | Open source enthusiast | https://naveentp.github.io/

MindOrks

Our community publishes stories worth reading on Android Development

Naveen T P

Written by

Android / iOS developer | Open source enthusiast | https://naveentp.github.io/

MindOrks

Our community publishes stories worth reading on Android Development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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