Fix Android emulator app launch

Three issues prevented `run_android_emulator.sh` from launching the app:

- gradlew's shebang was a hardcoded Nix-store bash path (copied verbatim
  from the nixpkgs Flutter SDK template) that breaks once that store path
  is garbage-collected, surfacing as a misleading "ProcessException: No
  such file or directory". The dev-shell now normalizes the gitignored
  android/gradlew shebang to the portable "#!/usr/bin/env sh" on entry.
- `flutter run -d android` never matched: -d resolves a device by id/name,
  not platform. Resolve the concrete id (e.g. emulator-5554) from
  `flutter devices --machine` instead.
- Install raced the boot ("device is still booting"); wait for
  sys.boot_completed=1 before launching.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
rzuasti
2026-06-04 09:35:46 -04:00
co-authored by Claude Opus 4.8
parent 080222f121
commit 2817fc7cab
2 changed files with 50 additions and 3 deletions
+38 -3
View File
@@ -15,15 +15,50 @@ if ! command -v flutter >/dev/null 2>&1; then
exit 1
fi
# Print the id of the first connected Android device, or nothing if none is up.
# `flutter run -d <x>` matches a device by id/name, never by platform, so the
# literal "android" is not a usable target; we must resolve the concrete id
# (e.g. "emulator-5554") from flutter's own machine-readable device list.
android_device_id() {
flutter devices --machine 2>/dev/null | python3 -c '
import json, sys
try:
devices = json.load(sys.stdin)
except ValueError:
devices = []
for d in devices:
if str(d.get("targetPlatform", "")).startswith("android"):
print(d["id"])
break
'
}
# Block until the Android OS on the given device has finished booting. The
# device shows up in `flutter devices` as soon as adb connects, but installing
# before boot completes fails with "device is still booting", so we poll the
# sys.boot_completed property (adb shell emits CRLF, hence the trailing-\r trim).
wait_for_boot() {
local serial="$1"
adb -s "$serial" wait-for-device
until [ "$(adb -s "$serial" shell getprop sys.boot_completed 2>/dev/null | tr -d '\r')" = "1" ]; do
sleep 2
done
}
# Boot the emulator in the background if no Android device is connected yet.
if ! flutter devices 2>/dev/null | grep -qi "android"; then
if [ -z "$(android_device_id)" ]; then
echo "No Android device detected, booting the emulator..."
"$SCRIPT_DIR/run_android.sh" -no-snapshot-save &
echo "Waiting for the emulator to come online..."
until flutter devices 2>/dev/null | grep -qi "android"; do
until [ -n "$(android_device_id)" ]; do
sleep 2
done
fi
exec flutter run -d android "$@"
device_id="$(android_device_id)"
echo "Waiting for '$device_id' to finish booting..."
wait_for_boot "$device_id"
echo "Launching OOTT on '$device_id'..."
exec flutter run -d "$device_id" "$@"