This page looks best with JavaScript enabled

How did I automate the generation of Release APK

 ·  ☕ 4 min read  ·  ✍️ Naveen T P
Banner

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.

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
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.

• • •

Solution

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.
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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.nio.file.Paths

/*
* Automates generation of Release APK
* ./gradlew assembleRelease
* */
Properties props = new Properties()
def userHome = Paths.get(System.getProperty('user.home'));
def propFile = file(userHome.resolve('PATH_TO_SIGNING_CONFIG_PROPERTIES_FILE.properties'))
if (propFile.canRead()) {
    props.load(new FileInputStream(propFile))
    if (props != null && props.containsKey('STORE_FILE') && props.containsKey('KEY_STORE_PASSWORD') 
    && props.containsKey('KEY_ALIAS') && props.containsKey('KEY_PASSWORD')) {
        android.signingConfigs.release.storeFile = file(props['STORE_FILE'])
        android.signingConfigs.release.storePassword = props['KEY_STORE_PASSWORD']
        android.signingConfigs.release.keyAlias = props['KEY_ALIAS']
        android.signingConfigs.release.keyPassword = props['KEY_PASSWORD']
    } else {
        println 'signing.properties found but some entries are missing'
        android.buildTypes.release.signingConfig = null
    }
} else {
    println 'signing.properties not found'
    android.buildTypes.release.signingConfig = null
}
A script that automates the generation of Signed apk
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* Module level build.gradle */

android {
  
  defaultConfig {
    ...
    flavorDimensions "default"
  }

  signingConfigs {
    release
  }

  buildTypes {
    release {
     ...
     signingConfig signingConfigs.release
  }
  
  /* 
  * To let gradle automate signing process, lintOptions to be added 
  * */
  lintOptions {
    checkReleaseBuilds false
    abortOnError false
  }
}
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.

Terminal commands
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.

Terminal commands

• • •

Advantages:

  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.


Thats all folks!

Buy me a coffee
If you like this article, you can buy me a coffee. Thanks!

Share on

Naveen T P
WRITTEN BY
Naveen T P
Mobile Developer | #Android | #iOS