Skip to content

Commit 68a8c9a

Browse files
committed
Improve emulator boot wait diagnostics
1 parent d5afe70 commit 68a8c9a

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

scripts/build-android-app.sh

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -367,22 +367,64 @@ wait_for_emulator() {
367367
local serial="$1"
368368
"$ADB_BIN" start-server >/dev/null
369369
"$ADB_BIN" -s "$serial" wait-for-device
370-
local booted="0"
371-
for _ in $(seq 1 120); do
372-
booted="$($ADB_BIN -s "$serial" shell getprop sys.boot_completed 2>/dev/null | tr -d '\r')"
373-
if [ "$booted" = "1" ]; then
370+
371+
local boot_timeout="${EMULATOR_BOOT_TIMEOUT_SECONDS:-600}"
372+
if ! [[ "$boot_timeout" =~ ^[0-9]+$ ]] || [ "$boot_timeout" -le 0 ]; then
373+
ba_log "Invalid EMULATOR_BOOT_TIMEOUT_SECONDS=$boot_timeout provided; falling back to 600"
374+
boot_timeout=600
375+
fi
376+
local poll_interval="${EMULATOR_BOOT_POLL_INTERVAL_SECONDS:-5}"
377+
if ! [[ "$poll_interval" =~ ^[0-9]+$ ]] || [ "$poll_interval" -le 0 ]; then
378+
poll_interval=5
379+
fi
380+
local status_log_interval="${EMULATOR_BOOT_STATUS_LOG_INTERVAL_SECONDS:-30}"
381+
if ! [[ "$status_log_interval" =~ ^[0-9]+$ ]] || [ "$status_log_interval" -le 0 ]; then
382+
status_log_interval=30
383+
fi
384+
385+
local deadline=$((SECONDS + boot_timeout))
386+
local last_log=$SECONDS
387+
local boot_completed="0"
388+
local dev_boot_completed="0"
389+
local bootanim=""
390+
local device_state=""
391+
392+
while [ $SECONDS -lt $deadline ]; do
393+
device_state="$($ADB_BIN -s "$serial" get-state 2>/dev/null | tr -d '\r')"
394+
if [ "$device_state" != "device" ]; then
395+
if [ $((SECONDS - last_log)) -ge $status_log_interval ]; then
396+
ba_log "Waiting for emulator $serial to become ready (state=$device_state)"
397+
last_log=$SECONDS
398+
fi
399+
sleep "$poll_interval"
400+
continue
401+
fi
402+
403+
boot_completed="$($ADB_BIN -s "$serial" shell getprop sys.boot_completed 2>/dev/null | tr -d '\r')"
404+
dev_boot_completed="$($ADB_BIN -s "$serial" shell getprop dev.bootcomplete 2>/dev/null | tr -d '\r')"
405+
bootanim="$($ADB_BIN -s "$serial" shell getprop init.svc.bootanim 2>/dev/null | tr -d '\r')"
406+
407+
if [ "$boot_completed" = "1" ] && [ "$dev_boot_completed" = "1" ]; then
374408
break
375409
fi
376-
sleep 2
410+
411+
if [ $((SECONDS - last_log)) -ge $status_log_interval ]; then
412+
ba_log "Waiting for emulator $serial to boot (sys.boot_completed=${boot_completed:-<unset>} dev.bootcomplete=${dev_boot_completed:-<unset>} bootanim=${bootanim:-<unset>})"
413+
last_log=$SECONDS
414+
fi
415+
sleep "$poll_interval"
377416
done
378-
if [ "$booted" != "1" ]; then
379-
ba_log "Emulator $serial failed to boot within expected time" >&2
417+
418+
if [ "$boot_completed" != "1" ] || [ "$dev_boot_completed" != "1" ]; then
419+
ba_log "Emulator $serial failed to boot within ${boot_timeout}s (sys.boot_completed=${boot_completed:-<unset>} dev.bootcomplete=${dev_boot_completed:-<unset>} bootanim=${bootanim:-<unset>} state=${device_state:-<unset>})" >&2
380420
return 1
381421
fi
422+
382423
"$ADB_BIN" -s "$serial" shell settings put global window_animation_scale 0 >/dev/null 2>&1 || true
383424
"$ADB_BIN" -s "$serial" shell settings put global transition_animation_scale 0 >/dev/null 2>&1 || true
384425
"$ADB_BIN" -s "$serial" shell settings put global animator_duration_scale 0 >/dev/null 2>&1 || true
385426
"$ADB_BIN" -s "$serial" shell input keyevent 82 >/dev/null 2>&1 || true
427+
"$ADB_BIN" -s "$serial" shell wm dismiss-keyguard >/dev/null 2>&1 || true
386428
return 0
387429
}
388430

0 commit comments

Comments
 (0)