Skip to content

Instantly share code, notes, and snippets.

@shahzadansari
Last active November 26, 2023 09:30
Show Gist options
  • Save shahzadansari/7f23b6e25d75c03b9edec210b7c7d6d7 to your computer and use it in GitHub Desktop.
Save shahzadansari/7f23b6e25d75c03b9edec210b7c7d6d7 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 appended with current time in 12-hour format
// 1. Add following snippet in your build.gradle.kts(:app)
// Run this command in terminal -> ./gradlew copyDebugApkToDesktopWithTime
// Note: It will replace "/" in your branch name with "-" as "/" creates a directory.
// fix/bug becomes fix-bug. You can adjust it as per your needs.
import java.text.SimpleDateFormat
import java.util.Date
tasks.register("copyDebugApkToDesktopWithTime") {
dependsOn("assembleDebug")
doLast {
val sourceDir = "build/outputs/apk/debug/app-debug.apk"
val desktopDir = System.getProperty("user.home") + "/Desktop/"
val currentTime = SimpleDateFormat("hh-mm-a").format(Date())
val gitBranch = try {
val process = Runtime.getRuntime().exec("git rev-parse --abbrev-ref HEAD")
process.inputStream.bufferedReader().readText().trim().replace("/", "-")
} catch (e: Exception) {
"unknown_branch" // Default to a generic name if Git command fails
}
project.copy {
from(sourceDir)
into(desktopDir)
rename { "${gitBranch}_${currentTime}.apk" }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment