@@ -4,6 +4,16 @@ set -euo pipefail
44
55ba_log () { echo " [build-android-app] $1 " ; }
66
7+ run_with_timeout () {
8+ local duration=" $1 "
9+ shift
10+ if command -v timeout > /dev/null 2>&1 ; then
11+ timeout " $duration " " $@ "
12+ else
13+ " $@ "
14+ fi
15+ }
16+
717SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
818REPO_ROOT=" $( cd " $SCRIPT_DIR /.." && pwd) "
919cd " $REPO_ROOT "
@@ -907,6 +917,57 @@ wait_for_api_level() {
907917 return 1
908918}
909919
920+ ensure_framework_ready () {
921+ local serial=" $1 "
922+ " $ADB_BIN " -s " $serial " wait-for-device > /dev/null 2>&1 || return 1
923+
924+ local total_timeout=" ${FRAMEWORK_READY_TIMEOUT_SECONDS:- 300} "
925+ local per_try=" ${FRAMEWORK_READY_PER_TRY_TIMEOUT_SECONDS:- 5} "
926+ if ! [[ " $total_timeout " =~ ^[0-9]+$ ]] || [ " $total_timeout " -le 0 ]; then
927+ total_timeout=300
928+ fi
929+ if ! [[ " $per_try " =~ ^[0-9]+$ ]] || [ " $per_try " -le 0 ]; then
930+ per_try=5
931+ fi
932+
933+ local deadline=$(( SECONDS + total_timeout))
934+ local last_log=$SECONDS
935+
936+ while [ $SECONDS -lt $deadline ]; do
937+ if run_with_timeout " $per_try " " $ADB_BIN " -s " $serial " shell pm path android > /dev/null 2>&1 \
938+ || run_with_timeout " $per_try " " $ADB_BIN " -s " $serial " shell pm list packages > /dev/null 2>&1 \
939+ || run_with_timeout " $per_try " " $ADB_BIN " -s " $serial " shell dumpsys package > /dev/null 2>&1 ; then
940+ return 0
941+ fi
942+
943+ if [ $(( SECONDS - last_log)) -ge 10 ]; then
944+ local boot_ok=" $( $ADB_BIN -s " $serial " shell getprop sys.boot_completed 2> /dev/null | tr -d ' \r' ) "
945+ local ce_ok=" $( $ADB_BIN -s " $serial " shell getprop sys.user.0.ce_available 2> /dev/null | tr -d ' \r' ) "
946+ ba_log " Waiting for framework readiness on $serial (boot_ok=${boot_ok:- ?} ce_ok=${ce_ok:- ?} )"
947+ last_log=$SECONDS
948+ fi
949+ sleep 2
950+ done
951+
952+ ba_log " Framework appears stalled on $serial ; restarting services" >&2
953+ " $ADB_BIN " -s " $serial " shell stop > /dev/null 2>&1 || true
954+ sleep 2
955+ " $ADB_BIN " -s " $serial " shell start > /dev/null 2>&1 || true
956+
957+ local restart_deadline=$(( SECONDS + 240 ))
958+ while [ $SECONDS -lt $restart_deadline ]; do
959+ if run_with_timeout " $per_try " " $ADB_BIN " -s " $serial " shell pm path android > /dev/null 2>&1 \
960+ || run_with_timeout " $per_try " " $ADB_BIN " -s " $serial " shell pm list packages > /dev/null 2>&1 \
961+ || run_with_timeout " $per_try " " $ADB_BIN " -s " $serial " shell dumpsys package > /dev/null 2>&1 ; then
962+ return 0
963+ fi
964+ sleep 2
965+ done
966+
967+ ba_log " ERROR: Package Manager not available on $serial after restart" >&2
968+ return 1
969+ }
970+
910971dump_emulator_diagnostics () {
911972 " $ADB_BIN " -s " $EMULATOR_SERIAL " shell getprop | sed ' s/^/[build-android-app] getprop: /' || true
912973 " $ADB_BIN " -s " $EMULATOR_SERIAL " shell logcat -d -t 2000 \
@@ -1194,6 +1255,10 @@ adb_install_file_path() {
11941255 local serial=" $1 " apk=" $2 "
11951256 local remote_tmp=" /data/local/tmp/$( basename " $apk " ) "
11961257
1258+ if ! ensure_framework_ready " $serial " ; then
1259+ return 1
1260+ fi
1261+
11971262 " $ADB_BIN " -s " $serial " shell rm -f " $remote_tmp " > /dev/null 2>&1 || true
11981263 if ! " $ADB_BIN " -s " $serial " push " $apk " " $remote_tmp " > /dev/null 2>&1 ; then
11991264 ba_log " Failed to push $( basename " $apk " ) to $remote_tmp " >&2
@@ -1324,14 +1389,20 @@ TEST_RUNTIME_PACKAGE="${RUNTIME_PACKAGE}.test"
13241389RUNTIME_STUB_FQCN=" ${RUNTIME_PACKAGE} .${MAIN_NAME} Stub"
13251390
13261391ba_log " Preparing device for APK installation"
1327- " $ADB_BIN " -s " $EMULATOR_SERIAL " shell getprop sys.boot_completed > /dev/null 2>&1 || true
1392+ if ! ensure_framework_ready " $EMULATOR_SERIAL " ; then
1393+ dump_emulator_diagnostics
1394+ stop_emulator
1395+ exit 1
1396+ fi
13281397
1329- SESSION_IDS=" $( $ADB_BIN -s " $EMULATOR_SERIAL " shell cmd package list sessions 2> /dev/null | awk ' {print $1}' | sed ' s/sessionId=//g' || true) "
1330- if [ -n " $SESSION_IDS " ]; then
1331- while IFS= read -r sid; do
1332- [ -z " $sid " ] && continue
1333- " $ADB_BIN " -s " $EMULATOR_SERIAL " shell cmd package abort-session " $sid " > /dev/null 2>&1 || true
1334- done <<< " $SESSION_IDS"
1398+ if " $ADB_BIN " -s " $EMULATOR_SERIAL " shell cmd package list sessions > /dev/null 2>&1 ; then
1399+ SESSION_IDS=" $( $ADB_BIN -s " $EMULATOR_SERIAL " shell cmd package list sessions 2> /dev/null | awk ' {print $1}' | sed ' s/sessionId=//g' || true) "
1400+ if [ -n " $SESSION_IDS " ]; then
1401+ while IFS= read -r sid; do
1402+ [ -z " $sid " ] && continue
1403+ " $ADB_BIN " -s " $EMULATOR_SERIAL " shell cmd package abort-session " $sid " > /dev/null 2>&1 || true
1404+ done <<< " $SESSION_IDS"
1405+ fi
13351406fi
13361407
13371408" $ADB_BIN " -s " $EMULATOR_SERIAL " shell pm uninstall " $RUNTIME_PACKAGE " > /dev/null 2>&1 || true
@@ -1353,6 +1424,12 @@ if ! adb_install_file_path "$EMULATOR_SERIAL" "$TEST_APK"; then
13531424 exit 1
13541425fi
13551426
1427+ if ! ensure_framework_ready " $EMULATOR_SERIAL " ; then
1428+ dump_emulator_diagnostics
1429+ stop_emulator
1430+ exit 1
1431+ fi
1432+
13561433" $ADB_BIN " -s " $EMULATOR_SERIAL " shell pm list instrumentation | sed " s/^/[build-android-app] instrumentation: /" || true
13571434" $ADB_BIN " -s " $EMULATOR_SERIAL " shell pm list packages | grep -E " ${RUNTIME_PACKAGE// ./ \. } |${RUNTIME_PACKAGE// ./ \. } \\ .test" | sed " s/^/[build-android-app] package: /" || true
13581435" $ADB_BIN " -s " $EMULATOR_SERIAL " shell cmd package resolve-activity --brief " $RUNTIME_PACKAGE /$RUNTIME_STUB_FQCN " | sed " s/^/[build-android-app] resolve-stub (pre-test): /" || true
0 commit comments