Skip to content

Commit 4ddaf22

Browse files
committed
Improve emulator readiness checks
1 parent 3b660e1 commit 4ddaf22

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

scripts/build-android-app.sh

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -461,35 +461,40 @@ wait_for_emulator() {
461461

462462
wait_for_package_service() {
463463
local serial="$1"
464-
local configured_timeout="${PACKAGE_SERVICE_TIMEOUT_SECONDS:-${PACKAGE_SERVICE_TIMEOUT:-600}}"
465-
local timeout="$configured_timeout"
464+
local timeout="${PACKAGE_SERVICE_TIMEOUT_SECONDS:-${PACKAGE_SERVICE_TIMEOUT:-600}}"
465+
local per_try="${PACKAGE_SERVICE_PER_TRY_TIMEOUT_SECONDS:-${PACKAGE_SERVICE_PER_TRY_TIMEOUT:-5}}"
466466
if ! [[ "$timeout" =~ ^[0-9]+$ ]] || [ "$timeout" -le 0 ]; then
467467
timeout=600
468468
fi
469-
local poll_interval=5
469+
if ! [[ "$per_try" =~ ^[0-9]+$ ]] || [ "$per_try" -le 0 ]; then
470+
per_try=5
471+
fi
472+
470473
local deadline=$((SECONDS + timeout))
471474
local last_log=$SECONDS
475+
472476
while [ $SECONDS -lt $deadline ]; do
473-
local boot_ok
477+
local boot_ok ce_ok
474478
boot_ok="$($ADB_BIN -s "$serial" shell getprop sys.boot_completed 2>/dev/null | tr -d '\r')"
475-
local ce_ok
476479
ce_ok="$($ADB_BIN -s "$serial" shell getprop sys.user.0.ce_available 2>/dev/null | tr -d '\r')"
477480
if [ "$boot_ok" = "1" ] && [ "$ce_ok" = "1" ]; then
478-
local android_id
479-
android_id="$($ADB_BIN -s "$serial" shell settings get secure android_id 2>/dev/null | tr -d '\r')"
480-
if [ -n "$android_id" ] && [ "$android_id" != "null" ]; then
481-
if "$ADB_BIN" -s "$serial" shell cmd package path android >/dev/null 2>&1 \
482-
|| "$ADB_BIN" -s "$serial" shell pm list packages >/dev/null 2>&1; then
481+
if timeout "$per_try" "$ADB_BIN" -s "$serial" shell settings get secure android_id >/dev/null 2>&1; then
482+
if timeout "$per_try" "$ADB_BIN" -s "$serial" shell cmd package path android >/dev/null 2>&1 \
483+
|| timeout "$per_try" "$ADB_BIN" -s "$serial" shell pm path android >/dev/null 2>&1 \
484+
|| timeout "$per_try" "$ADB_BIN" -s "$serial" shell cmd package list packages >/dev/null 2>&1 \
485+
|| timeout "$per_try" "$ADB_BIN" -s "$serial" shell pm list packages >/dev/null 2>&1; then
483486
return 0
484487
fi
485488
fi
486489
fi
487-
if [ $((SECONDS - last_log)) -ge 15 ]; then
488-
ba_log "Waiting for package manager service on $serial (boot_ok=${boot_ok:-<unset>} ce_ok=${ce_ok:-<unset>})"
490+
491+
if [ $((SECONDS - last_log)) -ge 10 ]; then
492+
ba_log "Waiting for package manager service on $serial (boot_ok=${boot_ok:-?} ce_ok=${ce_ok:-?})"
489493
last_log=$SECONDS
490494
fi
491-
sleep "$poll_interval"
495+
sleep 2
492496
done
497+
493498
ba_log "Package manager service not ready on $serial after ${timeout}s" >&2
494499
return 1
495500
}
@@ -550,9 +555,8 @@ EMULATOR_SERIAL="emulator-$EMULATOR_PORT"
550555
EMULATOR_LOG="$GRADLE_PROJECT_DIR/emulator.log"
551556
ba_log "Starting headless Android emulator $AVD_NAME on port $EMULATOR_PORT"
552557
ANDROID_AVD_HOME="$AVD_HOME" "$EMULATOR_BIN" -avd "$AVD_NAME" -port "$EMULATOR_PORT" \
553-
-no-window -no-snapshot -gpu swiftshader_indirect -no-audio -no-boot-anim -accel off \
554-
-camera-back none -camera-front none -no-snapshot-load -no-snapshot-save -skip-adb-auth \
555-
-no-accel -netfast >"$EMULATOR_LOG" 2>&1 &
558+
-no-window -gpu swiftshader_indirect -no-audio -no-boot-anim -accel off \
559+
-camera-back none -camera-front none -skip-adb-auth -no-accel -netfast -memory 2048 >"$EMULATOR_LOG" 2>&1 &
556560
EMULATOR_PID=$!
557561
trap stop_emulator EXIT
558562

@@ -607,12 +611,20 @@ if ! wait_for_emulator "$EMULATOR_SERIAL"; then
607611
exit 1
608612
fi
609613

610-
sleep 20
611-
ba_log "Waiting briefly for emulator system services to stabilize"
614+
POST_BOOT_GRACE="${EMULATOR_POST_BOOT_GRACE_SECONDS:-20}"
615+
if ! [[ "$POST_BOOT_GRACE" =~ ^[0-9]+$ ]] || [ "$POST_BOOT_GRACE" -lt 0 ]; then
616+
POST_BOOT_GRACE=20
617+
fi
618+
if [ "$POST_BOOT_GRACE" -gt 0 ]; then
619+
ba_log "Waiting ${POST_BOOT_GRACE}s for emulator system services to stabilize"
620+
sleep "$POST_BOOT_GRACE"
621+
fi
612622

613623
if ! wait_for_package_service "$EMULATOR_SERIAL"; then
614624
"$ADB_BIN" -s "$EMULATOR_SERIAL" shell getprop | sed 's/^/[build-android-app] getprop: /' || true
615-
"$ADB_BIN" -s "$EMULATOR_SERIAL" shell logcat -d -t 2000 | tail -n 200 | sed 's/^/[build-android-app] logcat: /' || true
625+
"$ADB_BIN" -s "$EMULATOR_SERIAL" shell logcat -d -t 2000 \
626+
| grep -v -E 'com\.android\.bluetooth|BtGd|bluetooth' \
627+
| tail -n 200 | sed 's/^/[build-android-app] logcat: /' || true
616628
stop_emulator
617629
exit 1
618630
fi

0 commit comments

Comments
 (0)