2021-02-18 15:12:35 +00:00
|
|
|
#!/bin/bash
|
2022-05-10 10:06:48 +00:00
|
|
|
# Copyright (C) 2021 The Qt Company Ltd.
|
|
|
|
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2020-12-09 17:15:51 +00:00
|
|
|
# This util launches the Android emulator and ensures it doesn't stuck/freeze
|
|
|
|
# by detecting that and restarting it
|
|
|
|
|
2022-01-06 10:05:21 +00:00
|
|
|
set -e
|
|
|
|
|
2020-12-09 17:15:51 +00:00
|
|
|
|
|
|
|
EMULATOR_MAX_RETRIES=5
|
2021-10-18 09:51:06 +00:00
|
|
|
EMULATOR_EXEC="$ANDROID_SDK_ROOT/emulator/emulator"
|
|
|
|
ADB_EXEC="$ANDROID_SDK_ROOT/platform-tools/adb"
|
2022-01-06 10:05:21 +00:00
|
|
|
if [ -z "${ANDROID_EMULATOR}" ]
|
|
|
|
then
|
2022-04-28 14:00:00 +00:00
|
|
|
EMULATOR_NAME="@emulator_x86_api_23"
|
2021-05-27 10:58:26 +00:00
|
|
|
else
|
|
|
|
EMULATOR_NAME="$ANDROID_EMULATOR"
|
|
|
|
fi
|
2022-01-04 16:41:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
function check_for_android_device
|
|
|
|
{
|
|
|
|
$ADB_EXEC devices \
|
|
|
|
| awk 'NR==2{print $2}' | grep -qE '^(online|device)$'
|
|
|
|
}
|
|
|
|
|
2022-01-06 10:05:21 +00:00
|
|
|
# WARNING: On the very first boot of the emulator it happens that the device
|
|
|
|
# "finishes" booting and getprop shows bootanim=stopped and
|
|
|
|
# boot_completed=1. But sometimes not all packages have been installed (`pm
|
|
|
|
# list packages` shows only 16 packages installed), and after around half a
|
|
|
|
# minute the boot animation starts spinning (bootanim=running) again despite
|
|
|
|
# boot_completed=1 all the time. After some minutes the boot animation stops
|
|
|
|
# again and the list of packages contains 80 packages. Only then the device is
|
|
|
|
# fully booted, and only then is dev.bootcomplete=1.
|
|
|
|
#
|
|
|
|
# To reproduce the emulator booting as the first time, you have to delete the
|
|
|
|
# cached images found inside $HOME/.android especially the
|
|
|
|
# "userdata-qemu.img.qcow2" file.
|
2022-01-04 16:41:47 +00:00
|
|
|
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'`
|
2022-01-06 10:05:21 +00:00
|
|
|
bootcomplete=` $ADB_EXEC shell getprop dev.bootcomplete | tr -d '\r\n'`
|
|
|
|
echo "bootanim=$bootanim boot_completed=$boot_completed bootcomplete=$bootcomplete"
|
|
|
|
[ "$bootanim" = stopped ] && [ "$boot_completed" = 1 ] && [ "$bootcomplete" = 1 ]
|
2022-01-04 16:41:47 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 11:01:50 +00:00
|
|
|
|
2020-12-09 17:15:51 +00:00
|
|
|
|
2022-01-06 10:05:21 +00:00
|
|
|
for counter in `seq ${EMULATOR_MAX_RETRIES}`
|
|
|
|
do
|
2020-12-09 17:15:51 +00:00
|
|
|
$ADB_EXEC start-server
|
2022-01-04 16:41:47 +00:00
|
|
|
|
|
|
|
if check_for_android_device
|
|
|
|
then
|
|
|
|
echo "Emulator is already running but it shouldn't be. Aborting\!"
|
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
|
2020-12-09 17:15:51 +00:00
|
|
|
echo "Starting emulator, try ${counter}/${EMULATOR_MAX_RETRIES}"
|
2022-01-04 17:10:17 +00:00
|
|
|
$EMULATOR_EXEC $EMULATOR_NAME \
|
|
|
|
-gpu swiftshader_indirect -no-audio -partition-size 4096 \
|
2023-02-01 20:20:02 +00:00
|
|
|
-cores 4 -memory 16000 -no-snapshot-load -no-snapshot-save \
|
2022-01-05 11:01:50 +00:00
|
|
|
</dev/null >$HOME/emulator.log 2>&1 &
|
2020-12-09 17:15:51 +00:00
|
|
|
emulator_pid=$!
|
2022-01-05 11:01:50 +00:00
|
|
|
disown $emulator_pid
|
2020-12-09 17:15:51 +00:00
|
|
|
|
2022-01-06 10:05:21 +00:00
|
|
|
echo "Waiting for emulated device to appear..."
|
2022-01-04 16:41:47 +00:00
|
|
|
$ADB_EXEC wait-for-device
|
2020-12-09 17:15:51 +00:00
|
|
|
|
2022-01-06 10:05:21 +00:00
|
|
|
echo "Waiting a few minutes for the emulator to fully boot..."
|
2022-01-04 16:41:47 +00:00
|
|
|
emulator_status=down
|
2022-01-05 11:01:50 +00:00
|
|
|
for i in `seq 300`
|
2022-01-04 16:41:47 +00:00
|
|
|
do
|
|
|
|
sleep 1
|
2022-01-06 10:05:21 +00:00
|
|
|
|
|
|
|
if check_for_android_device && check_if_fully_booted
|
2022-01-04 16:41:47 +00:00
|
|
|
then
|
|
|
|
emulator_status=up
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
2020-12-09 17:15:51 +00:00
|
|
|
|
|
|
|
# If emulator status is still offline after timeout period,
|
|
|
|
# we can assume it's stuck, and we must restart it
|
2022-01-04 16:41:47 +00:00
|
|
|
if [ $emulator_status = up ]
|
|
|
|
then
|
2020-12-09 17:15:51 +00:00
|
|
|
echo "Emulator started successfully"
|
|
|
|
break
|
|
|
|
else
|
2022-01-06 10:05:21 +00:00
|
|
|
if [ $counter -lt $EMULATOR_MAX_RETRIES ]
|
|
|
|
then
|
2022-01-04 16:41:47 +00:00
|
|
|
echo "Emulator failed to start, forcefully killing current instance and re-starting emulator"
|
2020-12-09 17:15:51 +00:00
|
|
|
kill $emulator_pid || true
|
|
|
|
sleep 5
|
2022-01-06 10:05:21 +00:00
|
|
|
elif [ $counter -eq $EMULATOR_MAX_RETRIES ]
|
|
|
|
then
|
2022-01-04 16:41:47 +00:00
|
|
|
echo "Emulator failed to start, reached maximum number of retries. Aborting\!"
|
|
|
|
exit 2
|
2020-12-09 17:15:51 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2022-01-05 11:01:50 +00:00
|
|
|
|
2022-01-04 16:41:47 +00:00
|
|
|
exit 0
|