Skip to content

Commit 3b660e1

Browse files
committed
Improve emulator bring-up for Android UI tests
1 parent 998d8e2 commit 3b660e1

File tree

2 files changed

+60
-24
lines changed

2 files changed

+60
-24
lines changed

scripts/build-android-app.sh

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ install_android_packages() {
344344
"$manager" --install \
345345
"platform-tools" \
346346
"emulator" \
347-
"platforms;android-33" \
348-
"system-images;android-33;google_apis;x86_64" >/dev/null 2>&1 || true
347+
"platforms;android-35" \
348+
"system-images;android-35;default;x86_64" >/dev/null 2>&1 || true
349349
}
350350

351351
create_avd() {
@@ -357,16 +357,26 @@ create_avd() {
357357
ba_log "avdmanager not available; cannot create emulator" >&2
358358
exit 1
359359
fi
360-
rm -rf "$avd_dir"
361360
mkdir -p "$avd_dir"
362-
if ! ANDROID_AVD_HOME="$avd_dir" "$manager" create avd -n "$name" -k "$image" --device "pixel_6" --force >/dev/null <<<'no'
361+
local ini_file="$avd_dir/$name.ini"
362+
local image_dir="$avd_dir/$name.avd"
363+
if [ -f "$ini_file" ] && [ -d "$image_dir" ]; then
364+
if grep -F -q "$image" "$ini_file" 2>/dev/null; then
365+
ba_log "Reusing existing Android Virtual Device $name"
366+
return
367+
fi
368+
ba_log "Existing Android Virtual Device $name uses a different system image; recreating"
369+
rm -f "$ini_file"
370+
rm -rf "$image_dir"
371+
fi
372+
if ! ANDROID_AVD_HOME="$avd_dir" "$manager" create avd -n "$name" -k "$image" --device "2.7in QVGA" --force >/dev/null <<<'no'
363373
then
364374
ba_log "Failed to create Android Virtual Device $name using image $image" >&2
365375
find "$avd_dir" -maxdepth 2 -mindepth 1 -print | sed 's/^/[build-android-app] AVD: /' >&2 || true
366376
exit 1
367377
fi
368-
if [ ! -f "$avd_dir/$name.ini" ]; then
369-
ba_log "AVD $name was created but configuration file $avd_dir/$name.ini is missing" >&2
378+
if [ ! -f "$ini_file" ]; then
379+
ba_log "AVD $name was created but configuration file $ini_file is missing" >&2
370380
find "$avd_dir" -maxdepth 1 -mindepth 1 -print | sed 's/^/[build-android-app] AVD: /' >&2 || true
371381
exit 1
372382
fi
@@ -377,10 +387,10 @@ wait_for_emulator() {
377387
"$ADB_BIN" start-server >/dev/null
378388
"$ADB_BIN" -s "$serial" wait-for-device
379389

380-
local boot_timeout="${EMULATOR_BOOT_TIMEOUT_SECONDS:-600}"
390+
local boot_timeout="${EMULATOR_BOOT_TIMEOUT_SECONDS:-900}"
381391
if ! [[ "$boot_timeout" =~ ^[0-9]+$ ]] || [ "$boot_timeout" -le 0 ]; then
382-
ba_log "Invalid EMULATOR_BOOT_TIMEOUT_SECONDS=$boot_timeout provided; falling back to 600"
383-
boot_timeout=600
392+
ba_log "Invalid EMULATOR_BOOT_TIMEOUT_SECONDS=$boot_timeout provided; falling back to 900"
393+
boot_timeout=900
384394
fi
385395
local poll_interval="${EMULATOR_BOOT_POLL_INTERVAL_SECONDS:-5}"
386396
if ! [[ "$poll_interval" =~ ^[0-9]+$ ]] || [ "$poll_interval" -le 0 ]; then
@@ -451,24 +461,34 @@ wait_for_emulator() {
451461

452462
wait_for_package_service() {
453463
local serial="$1"
454-
local timeout="${PACKAGE_SERVICE_TIMEOUT:-120}"
464+
local configured_timeout="${PACKAGE_SERVICE_TIMEOUT_SECONDS:-${PACKAGE_SERVICE_TIMEOUT:-600}}"
465+
local timeout="$configured_timeout"
455466
if ! [[ "$timeout" =~ ^[0-9]+$ ]] || [ "$timeout" -le 0 ]; then
456-
timeout=120
467+
timeout=600
457468
fi
469+
local poll_interval=5
458470
local deadline=$((SECONDS + timeout))
459471
local last_log=$SECONDS
460472
while [ $SECONDS -lt $deadline ]; do
461-
if "$ADB_BIN" -s "$serial" shell cmd package list packages >/dev/null 2>&1; then
462-
return 0
463-
fi
464-
if "$ADB_BIN" -s "$serial" shell pm list packages >/dev/null 2>&1; then
465-
return 0
473+
local boot_ok
474+
boot_ok="$($ADB_BIN -s "$serial" shell getprop sys.boot_completed 2>/dev/null | tr -d '\r')"
475+
local ce_ok
476+
ce_ok="$($ADB_BIN -s "$serial" shell getprop sys.user.0.ce_available 2>/dev/null | tr -d '\r')"
477+
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
483+
return 0
484+
fi
485+
fi
466486
fi
467-
if [ $((SECONDS - last_log)) -ge 10 ]; then
468-
ba_log "Waiting for package manager service on $serial"
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>})"
469489
last_log=$SECONDS
470490
fi
471-
sleep 2
491+
sleep "$poll_interval"
472492
done
473493
ba_log "Package manager service not ready on $serial after ${timeout}s" >&2
474494
return 1
@@ -507,8 +527,11 @@ if [ ! -x "$EMULATOR_BIN" ]; then
507527
fi
508528

509529
AVD_NAME="cn1UiTestAvd"
510-
SYSTEM_IMAGE="system-images;android-33;google_apis;x86_64"
511-
AVD_HOME="$WORK_DIR/android-avd"
530+
SYSTEM_IMAGE="system-images;android-35;default;x86_64"
531+
AVD_CACHE_ROOT="${AVD_CACHE_ROOT:-${RUNNER_TEMP:-$HOME}/cn1-android-avd}"
532+
mkdir -p "$AVD_CACHE_ROOT"
533+
AVD_HOME="$AVD_CACHE_ROOT"
534+
ba_log "Using AVD home at $AVD_HOME"
512535
create_avd "$AVDMANAGER_BIN" "$AVD_NAME" "$SYSTEM_IMAGE" "$AVD_HOME"
513536

514537
ANDROID_AVD_HOME="$AVD_HOME" "$ADB_BIN" start-server >/dev/null
@@ -526,7 +549,10 @@ EMULATOR_SERIAL="emulator-$EMULATOR_PORT"
526549

527550
EMULATOR_LOG="$GRADLE_PROJECT_DIR/emulator.log"
528551
ba_log "Starting headless Android emulator $AVD_NAME on port $EMULATOR_PORT"
529-
ANDROID_AVD_HOME="$AVD_HOME" "$EMULATOR_BIN" -avd "$AVD_NAME" -port "$EMULATOR_PORT" -no-window -no-snapshot -gpu swiftshader_indirect -no-audio -no-boot-anim -accel off >"$EMULATOR_LOG" 2>&1 &
552+
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 &
530556
EMULATOR_PID=$!
531557
trap stop_emulator EXIT
532558

@@ -581,7 +607,12 @@ if ! wait_for_emulator "$EMULATOR_SERIAL"; then
581607
exit 1
582608
fi
583609

610+
sleep 20
611+
ba_log "Waiting briefly for emulator system services to stabilize"
612+
584613
if ! wait_for_package_service "$EMULATOR_SERIAL"; then
614+
"$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
585616
stop_emulator
586617
exit 1
587618
fi

scripts/update_android_ui_test_gradle.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import re
88
import sys
99

10-
COMPILE_SDK_LINE = " compileSdkVersion 33\n"
10+
COMPILE_SDK_LINE = " compileSdkVersion 35\n"
1111

1212
TEST_OPTIONS_SNIPPET = """ testOptions {\n animationsDisabled = true\n }\n\n"""
1313

@@ -234,7 +234,12 @@ def summary(self) -> str:
234234

235235

236236
def process(path: pathlib.Path) -> None:
237-
editor = GradleFile(path.read_text(encoding="utf-8"))
237+
content = path.read_text(encoding="utf-8")
238+
if "android {" not in content:
239+
raise SystemExit(
240+
"Selected Gradle file doesn't contain an android { } block. Check module path."
241+
)
242+
editor = GradleFile(content)
238243
editor.apply()
239244
path.write_text(editor.content, encoding="utf-8")
240245
print(editor.summary())

0 commit comments

Comments
 (0)