Skip to content

Instantly share code, notes, and snippets.

@OleksandrKucherenko
Created December 3, 2018 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OleksandrKucherenko/664f107383d2b44c09a1f4bc81b79b12 to your computer and use it in GitHub Desktop.
Save OleksandrKucherenko/664f107383d2b44c09a1f4bc81b79b12 to your computer and use it in GitHub Desktop.
Script force all 'installDebug' task wait 10 seconds for device attaching.
static isAnyDeviceOnline() {
def start = System.currentTimeMillis()
def firstTime = true
while (System.currentTimeMillis() - start < 10000) {
def out = new StringBuilder()
def process = 'adb devices -l'.execute()
process.consumeProcessOutput(out, null)
process.waitForOrKill(1000)
if (out.toString().contains("device ")) return
if (firstTime) {
firstTime = false
println 'INFO: Waiting 10s for any ADB device'
println "${out.toString()}"
}
Thread.sleep(1000)
}
throw new GradleException('Timeout. Cannot detect any connected online device')
}
gradle.projectsEvaluated {
// Grab all build types and product flavors
def buildTypes = android.buildTypes.collect { type -> type.name }
def productFlavors = android.productFlavors.collect { flavor -> flavor.name }
// When no product flavors defined, use empty
if (!productFlavors) productFlavors.add("")
productFlavors.each { flavor ->
buildTypes.each { buildt ->
def task = project.tasks.findByName("install${flavor.capitalize()}${buildt.capitalize()}")
if (null != task) task.doFirst { isAnyDeviceOnline() }
}
}
}
@OleksandrKucherenko
Copy link
Author

Usage: just copy and paste code into end of the app/build.gradle file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment