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.
Now, I want to,
- Generate debug apks for all 3 flavours.
- Generate signed apks for all 3 flavours (Let’s consider, all 3 flavours uses same keystore and signing configs)
- 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.
Step 2:
Let’s write some gradle script to read the above file and use those signing credentials to generate Production signed apk.
|
|
|
|
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.
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.
Advantages:
- Productivity — Saves build time using terminal
- Confidential — Keeping properties file out of the version control(git, svn etc.,) and you still be able to sign the apk using the script
- 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.
- 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!