Wait for android emulator to be up and running properly

Pick-to: 6.3 6.2
Fixes: QTQAINFRA-4681
Change-Id: Icfa75b6982964970172726379e5a2a2bb640f8bf
Reviewed-by: Daniel Smith <Daniel.Smith@qt.io>
This commit is contained in:
Dimitrios Apostolou 2022-01-04 17:41:47 +01:00
parent 17eb0f2d8a
commit 0aaf514042

View File

@ -31,7 +31,6 @@
set -ex set -ex
EMULATOR_TIMEOUT=30
EMULATOR_MAX_RETRIES=5 EMULATOR_MAX_RETRIES=5
EMULATOR_EXEC="$ANDROID_SDK_ROOT/emulator/emulator" EMULATOR_EXEC="$ANDROID_SDK_ROOT/emulator/emulator"
ADB_EXEC="$ANDROID_SDK_ROOT/platform-tools/adb" ADB_EXEC="$ANDROID_SDK_ROOT/platform-tools/adb"
@ -40,35 +39,66 @@ if [[ -z "${ANDROID_EMULATOR}" ]]; then
else else
EMULATOR_NAME="$ANDROID_EMULATOR" EMULATOR_NAME="$ANDROID_EMULATOR"
fi fi
RESULT=0
function check_for_android_device
{
$ADB_EXEC devices \
| awk 'NR==2{print $2}' | grep -qE '^(online|device)$'
}
function check_if_fully_booted
{
# The "getprop" command separates lines with \r\n so we trim them
bootanim=` $ADB_EXEC shell getprop init.svc.bootanim | tr -d '\r\n'`
boot_completed=`$ADB_EXEC shell getprop sys.boot_completed | tr -d '\r\n'`
[ "$bootanim" = stopped ] && [ "$boot_completed" = 1 ]
}
for counter in `seq 1 ${EMULATOR_MAX_RETRIES}`; do for counter in `seq 1 ${EMULATOR_MAX_RETRIES}`; do
$ADB_EXEC start-server $ADB_EXEC start-server
if check_for_android_device
then
echo "Emulator is already running but it shouldn't be. Aborting\!"
exit 3
fi
echo "Starting emulator, try ${counter}/${EMULATOR_MAX_RETRIES}" echo "Starting emulator, try ${counter}/${EMULATOR_MAX_RETRIES}"
$EMULATOR_EXEC $EMULATOR_NAME -gpu swiftshader_indirect -no-audio -partition-size 4096 -cores 4 -memory 3500 -no-snapshot-load -no-snapshot-save &>/dev/null & $EMULATOR_EXEC $EMULATOR_NAME -gpu swiftshader_indirect -no-audio -partition-size 4096 -cores 4 -memory 3500 -no-snapshot-load -no-snapshot-save &>/dev/null &
emulator_pid=$! emulator_pid=$!
# Give emulator time to start $ADB_EXEC wait-for-device
sleep $EMULATOR_TIMEOUT
emulator_status=`$ADB_EXEC devices | tail -n -2 | awk '{print $2}'` # Wait about one minute for the emulator to come up
emulator_status=down
for i in `seq 60`
do
sleep 1
if check_for_android_device && check_if_fully_booted
then
emulator_status=up
break
fi
done
# If emulator status is still offline after timeout period, # If emulator status is still offline after timeout period,
# we can assume it's stuck, and we must restart it # we can assume it's stuck, and we must restart it
if [[ $emulator_status == 'online' || $emulator_status == 'device' ]]; then if [ $emulator_status = up ]
then
echo "Emulator started successfully" echo "Emulator started successfully"
break break
else else
if [ $counter -lt $EMULATOR_MAX_RETRIES ]; then if [ $counter -lt $EMULATOR_MAX_RETRIES ]; then
echo "Emulator failed to start, forcefully killing current instance" echo "Emulator failed to start, forcefully killing current instance and re-starting emulator"
kill $emulator_pid || true kill $emulator_pid || true
sleep 5 sleep 5
elif [ $counter -eq $EMULATOR_MAX_RETRIES ]; then elif [ $counter -eq $EMULATOR_MAX_RETRIES ]; then
echo "Emulator failed to start, reached maximum number of retries" echo "Emulator failed to start, reached maximum number of retries. Aborting\!"
RESULT=-1 exit 2
break
fi fi
fi fi
done done
exit $RESULT exit 0