Skip to content

Instantly share code, notes, and snippets.

@pratamawijaya
Forked from shahzadansari/build.gradle.kts
Created September 5, 2023 06:08
Show Gist options
  • Save pratamawijaya/78933f6e0e4af017160822438f8fed48 to your computer and use it in GitHub Desktop.
Save pratamawijaya/78933f6e0e4af017160822438f8fed48 to your computer and use it in GitHub Desktop.
Custom gradle task which copies app-debug.apk to Desktop and renames it to Git's current checked out branch
// 1. Add following snippet in your build.gradle.kts(:app)
// Run this command in terminal -> ./gradlew copyDebugApkToDesktop
tasks.register("copyDebugApkToDesktop") {
dependsOn("assembleDebug")
doLast {
val sourceDir = "build/outputs/apk/debug/app-debug.apk"
val desktopDir = System.getProperty("user.home") + "/Desktop/"
val gitBranch = try {
val process = Runtime.getRuntime().exec("git rev-parse --abbrev-ref HEAD")
process.inputStream.bufferedReader().readText().trim()
} catch (e: Exception) {
"unknown_branch" // Default to a generic name if Git command fails
}
project.copy {
from(sourceDir)
into(desktopDir)
rename { "${gitBranch}.apk" }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment