Sitemap

THE GRADLE MAGIC BEHIND EVERY ANDROID BUILD

5 Gradle Scripts I Use in Every Android Project!

These simple Gradle tweaks save me time, keep my builds clean, and help automate the boring stuff.

3 min readJul 27, 2025

--

Press enter or click to view image in full size
Photo by Mohammad Alizade on Unsplash

Not a Medium Member? “Read for Free”

If you’re an Android developer, there’s no escaping Gradle. Whether you love it or merely tolerate it, Gradle is the powerhouse behind your project’s build, dependency management, and automation.

Over the years, I’ve picked up a few Gradle tricks that I now use almost every single day. These aren’t just fancy scripts — they’ve become part of my daily workflow to save time, avoid errors, and ship cleaner builds.

In this article, I’ll share 5 Gradle scripts that have earned a permanent place in my Android toolkit.

1). Custom Build Configs for Different Flavors

Ever had to use different APIs, URLs, or values based on the environment (dev, staging, prod)? Instead of hardcoding or constantly changing things, use build variants.

android {
buildTypes {
debug {
buildConfigField("String", "BASE_URL", '"https://dev.api.com"')
}
release {
buildConfigField("String", "BASE_URL", '"https://prod.api.com"')
minifyEnabled true
proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
}
}
}

I no longer worry about accidentally hitting the production server from a debug build. It’s clean, safe, and automatic.

2). Signing Config Script for Secure Releases

Manually configuring signing keys can be risky. I use a keystore.properties file to keep secrets out of version control.

keystore.properties (ignored by Git):

storePassword=myStorePassword
keyPassword=myKeyPassword
keyAlias=myKeyAlias
storeFile=keystore.jks

build.gradle:

def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(rootProject.file("keystore.properties")))

android {
signingConfigs {
release {
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
}
}
}

Keeps sensitive credentials out of Git and simplifies CI/CD setups.

3). Automatic Cleanup Script Before Every Build

Ever faced a weird issue that only got fixed after a clean build? I’ve been there. So I added a pre-build cleanup step to remove old cache.

Add the below script at the end of the build.gradle(Module:app) file.

gradle.taskGraph.whenReady {
tasks.findByName("preBuild")?.dependsOn("clean")
}

Avoids hard-to-debug build issues and ensures fresh builds when needed.

4). Gradle Script to Rename APK

Place this inside your app/build.gradle(Module:app)

import com.android.build.gradle.internal.api.BaseVariantOutputImpl

android {
...

applicationVariants.all {
outputs.forEach { output ->
if (output is BaseVariantOutputImpl) {
val appName = "MyApp"
val versionName = this.versionName
val buildType = this.buildType.name
val flavorName =
if (this.flavorName?.isNotEmpty() == true) "${this.flavorName}_" else ""
val newApkName = "${appName}_${flavorName}${buildType}_v${versionName}.apk"

output.outputFileName = newApkName
}
}
}
}

This renames your APK to something like:

MyApp_debug_v1.0.apk
MyApp_release_v1.0.apk

5). Gradle Script to Rename AAB File

To rename the AAB file after the bundle task completes in Kotlin DSL (build.gradle.kts(Module:app)), you'll need to hook into the bundleRelease or bundle<Variant> task and move/rename the generated .aab file manually.

android {
....
....
....

afterEvaluate {
tasks.named("bundle").configure {
doLast {
applicationVariants.all {
val outputDir = layout.buildDirectory.get().dir("outputs/bundle/${buildType.name}").asFile
val aabFile = outputDir.listFiles()?.find { it.extension == "aab" }

if (aabFile != null && aabFile.exists()) {
val appName = "MyApp"
val versionName = android.defaultConfig.versionName
val buildType = buildType.name
val date = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())

val renamedFile = File(
outputDir,
"${appName}_${buildType}_v${versionName}_$date.aab"
)

val success = aabFile.renameTo(renamedFile)
if (success) {
println("AAB file renamed to: ${renamedFile.name}")
} else {
println("Failed to rename AAB file.")
}
} else {
println("AAB file not found in: ${outputDir.path}")
}
}
}
}
}

}

This renames your Bundle to something like:

AAB file renamed to: MyApp_debug_v3.0.7_20250727_212259.aab
AAB file renamed to: MyApp_release_v3.0.7_20250727_212259.aab

App Bundle Path:

{path}/app/build/outputs/bundle/debug/MyApp_debug_v3.0.7_20250727_212259.aab

{path}/app/build/outputs/bundle/release/MyApp_release_v3.0.7_20250727_212259.aab

Conclusion

Gradle can be intimidating at first, but once you get the hang of it, it becomes a powerful in your development workflow.

These 5 scripts helped me:

  • Simplify builds across environments
  • Secure sensitive data
  • Automate testing
  • Prevent flaky builds
  • Save time with quick tools

Try adding one or two of these to your project and see the difference. And if you have your own Gradle tricks, I’d love to hear them in the comments!

--

--

Jayant Kumar🇮🇳
Jayant Kumar🇮🇳

Written by Jayant Kumar🇮🇳

Jayant Kumar is a Lead Software Engineer, passionate about building modern Android applications. He shares his expertise in Kotlin, Android, and Compose.

Responses (3)