Stop Gradle installing on every checkout's emulator at once
The adb wrapper can only aim a call that goes through it, and Gradle's Android tasks do not: installDebug, uninstallDebug and connectedAndroidTest ask the adb server for every attached device and act on all of them. One session ran installDebug with two emulators up and replaced the app on both, which Gradle reported as success and the other session read as its own build never landing. `emu check` answers the question the wrapper already answers for adb -- would a command that reaches every attached device reach a stranger's emulator from here -- and install.sh links a Gradle init script into ~/.gradle/init.d so every build on this machine asks it, including checkouts nobody has adopted it in. It refuses narrowly: a lone emulator, a physical phone and a caller who named a device are all fine. ANDROID_SERIAL naming another checkout's emulator is not, because that is what a serial exported into a long-lived shell decays into once an emulator restarts and another's takes the port.
This commit is contained in:
1 parent
1da445ae32
commit
69a22c54e7
4 files changed
+158
-2
No files matched your search
@@ -0,0 +1,55 @@
|
||||
// Nothing Gradle builds here may land on another checkout's emulator.
|
||||
//
|
||||
// Android's install, uninstall and connected-test tasks talk to the adb
|
||||
// server directly and act on *every* attached device -- the wrapper `adb` in
|
||||
// this repo never sees them, so none of its aiming applies. With two
|
||||
// sessions' emulators up, `./gradlew installDebug` replaces the app on both,
|
||||
// reports success, and says nothing; from the other session that reads as
|
||||
// its own build never landing. It happened on 2026-08-31.
|
||||
//
|
||||
// This is an init script, applied to every build on this machine, rather
|
||||
// than something each project opts into: a rule a checkout has to adopt is
|
||||
// one the next checkout will not have, and the sessions this protects are
|
||||
// working in checkouts nobody has visited yet.
|
||||
//
|
||||
// The decision itself is `emu check` (one rule for "which device does a
|
||||
// command here mean", shared with the adb wrapper), run in the project's own
|
||||
// directory because that is what names the AVD. It refuses only when another
|
||||
// checkout's emulator is attached; EMU_ANY_DEVICE=1 means every device
|
||||
// anyway.
|
||||
|
||||
def emu = new File(System.getProperty("user.home"), ".local/bin/emu").absolutePath
|
||||
|
||||
gradle.allprojects { project ->
|
||||
def workingDir = project.rootDir
|
||||
project.tasks.configureEach { task ->
|
||||
// Matched on where the task class comes from as well as on its name:
|
||||
// a build of any kind may have an `install`, and this must not fail
|
||||
// a project that has nothing to do with a device.
|
||||
if (!task.getClass().name.startsWith('com.android.')) return
|
||||
if (!(task.name =~ /^(install|uninstall|connected)/)) return
|
||||
|
||||
task.doFirst {
|
||||
def said
|
||||
def status
|
||||
try {
|
||||
def process = new ProcessBuilder(emu, 'check')
|
||||
.directory(workingDir)
|
||||
.redirectErrorStream(true)
|
||||
.start()
|
||||
said = process.inputStream.getText('UTF-8')
|
||||
status = process.waitFor()
|
||||
} catch (IOException e) {
|
||||
// Failing closed: this script is installed by
|
||||
// emulator-tools, so `emu` missing means a half-installation
|
||||
// rather than a machine that never had the guard.
|
||||
throw new org.gradle.api.GradleException(
|
||||
"\ncannot check which emulator this build means: ${emu} is not runnable" +
|
||||
"\n run ~/repos/emulator-tools/install.sh, or set EMU_ANY_DEVICE=1")
|
||||
}
|
||||
if (status != 0) {
|
||||
throw new org.gradle.api.GradleException('\n' + said.trim())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user