Skip to content

Instantly share code, notes, and snippets.

@erikghonyan
Last active June 6, 2023 19:07
Show Gist options
  • Save erikghonyan/7159b01dec46166cc61d79bfa553f157 to your computer and use it in GitHub Desktop.
Save erikghonyan/7159b01dec46166cc61d79bfa553f157 to your computer and use it in GitHub Desktop.
Installs and Runs the Android app from Gradle
androidComponents {
val adbPath = sdkComponents.adb.get().asFile.absolutePath
onVariants { variant ->
val capitalizedVariantName = variant.name.replaceFirstChar(Char::titlecase)
val applicationId = variant.applicationId.get()
tasks.register("run${capitalizedVariantName}") {
group = "run"
description = "Installs and Launches the $capitalizedVariantName build"
dependsOn("install${capitalizedVariantName}")
dependsOn("launch${capitalizedVariantName}")
}
tasks.register<Exec>("launch${capitalizedVariantName}") {
group = "run"
description = "Launches the $capitalizedVariantName build"
mustRunAfter("install${capitalizedVariantName}")
doFirst {
println("Launching $applicationId")
println("execute: ${commandLine.joinToString(" ")}")
}
commandLine = listOf(
adbPath,
"shell", "am", "start",
"-p", applicationId,
"-a", "android.intent.action.MAIN",
"-c", "android.intent.category.LAUNCHER",
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment