Gradle task for launching Android simulator
Android build toolchain, albeit being powerful, sometimes lacks some essential functionality required for the modern software development. In particular, running instrumented unit-tests in the CI environment: those tests require a device or a simulator running, however developers need some scripting to make it work.
To simplify the task, I've created a Gradle task that verifies if any Android device or simulator is connected. If not, the task launches a simulator with the first AVD in the list (or fails if no AVDs are found). It's up to you to make the dependencies for the connectedCheck task. The sample output:
> ./gradlew connect --info
...
:AndroidSampleApp:connect (Thread[main,5,main]) started.
:AndroidSampleApp:connect
Executing task ':AndroidSampleApp:connect' (up-to-date check took 0.001 secs) due to:
Task has not declared any outputs.
Detecting devices...
Launching emulator for Nexus_5_API_25 ...
:AndroidSampleApp:connect (Thread[main,5,main]) completed. Took 14.186 secs.
BUILD SUCCESSFUL
The code (just add it into the build.gradle of the module):
To simplify the task, I've created a Gradle task that verifies if any Android device or simulator is connected. If not, the task launches a simulator with the first AVD in the list (or fails if no AVDs are found). It's up to you to make the dependencies for the connectedCheck task. The sample output:
> ./gradlew connect --info
...
:AndroidSampleApp:connect (Thread[main,5,main]) started.
:AndroidSampleApp:connect
Executing task ':AndroidSampleApp:connect' (up-to-date check took 0.001 secs) due to:
Task has not declared any outputs.
Detecting devices...
Launching emulator for Nexus_5_API_25 ...
:AndroidSampleApp:connect (Thread[main,5,main]) completed. Took 14.186 secs.
BUILD SUCCESSFUL
The code (just add it into the build.gradle of the module):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
task connect(type: ConnectDevicesTask) { | |
description 'Verifies that any Android device is connected and runs a simulator if not.' | |
} | |
import com.android.ddmlib.AndroidDebugBridge | |
import com.android.ddmlib.IDevice | |
import org.gradle.tooling.BuildException | |
class ConnectDevicesTask extends DefaultTask { | |
@TaskAction | |
void connect() { | |
project.logger.info "Detecting devices..." | |
AndroidDebugBridge.initIfNeeded false /*clientSupport*/ | |
AndroidDebugBridge bridge = AndroidDebugBridge.createBridge( | |
project.android.adbExe.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) | |
} | |
IDevice[] devices = bridge.devices | |
if (devices.length == 0) { | |
String emulator = project.android.sdkDirectory.absolutePath + "/tools/emulator" | |
String command = "$emulator -list-avds" | |
String avds = command.execute().text | |
if (avds?.trim()) { | |
String avd = avds.split()[0] | |
String runCmd = "$emulator -avd $avd" | |
project.logger.info "Launching emulator for $avd ..." | |
runCmd.execute() | |
timeOut = 30000 // 30 sec | |
while (bridge.devices.length == 0 && timeOut > 0) { | |
sleep sleepTime | |
timeOut -= sleepTime | |
} | |
if (bridge.devices.length == 0 && timeOut <= 0) { | |
throw new BuildException("Timeout launching the emulator.", null) | |
} | |
} else { | |
throw new BuildException("No registered AVDs!", null) | |
} | |
} | |
} | |
} |
Comments
Post a Comment