Skip to content

Instantly share code, notes, and snippets.

@eyal-lezmy
Last active February 18, 2016 14:40
Show Gist options
  • Save eyal-lezmy/2786ecbf6bde0ae93a84 to your computer and use it in GitHub Desktop.
Save eyal-lezmy/2786ecbf6bde0ae93a84 to your computer and use it in GitHub Desktop.
Check connected devices before adding genymotion configuration
/**
This Gist shows how to check the presence of connected devices before applying another .gradle file.
It has been tested with Android Gradle Plugin from 1.2.0 to 1.5.0.
*/
apply plugin: 'com.android.application'
android {
Your app config...
}
/**
* This method returns whether a device is plugged or not to adb.
* It also prints the list of connected devices when there is.
* @param project the current Gradle project
* @return returns true is there is at least one device plugged or false otherwise
*/
boolean hasDevicesPlugged(Project project) {
//Partially extracted from: https://android.googlesource.com/platform/tools/build/+/925c0b5cf8730105dd5aa8c851141d5688d07789/gradle/src/main/groovy/com/android/build/gradle/internal/tasks/TestFlavorTask.groovy#156
AndroidDebugBridge.initIfNeeded(false /*clientSupport*/)
AndroidDebugBridge bridge = AndroidDebugBridge.createBridge(project.android.getAdbExe().absolutePath, false /*forceNewBridge*/)
long timeOut = 30000 // 30 sec
int sleepTime = 1000
while (!bridge.hasInitialDeviceList() && timeOut > 0) {
sleep(sleepTime)
timeOut -= sleepTime
}
if (timeOut <= 0 && !bridge.hasInitialDeviceList()) {
throw new BuildException("Timeout getting device list.", null)
}
if (bridge.devices.size() < 1) {
return false
}
println "Devices found:"
for (IDevice device : bridge.devices) {
println device.name
}
return true
}
if(hasDevicesPlugged(project)) {
// import your gradle script according to your needs
apply from: "genymotion.gradle"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment