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.
I have an android project with three different product flavours
Production . All three flavours has its own end points, and other configurations.
Now, I want to,
debugapks for all 3 flavours.
signedapks for all 3 flavours (Let’s consider, all 3 flavours uses same keystore and signing configs)
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.
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.
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.
- 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.
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.