-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart_emulator.sh
executable file
·114 lines (91 loc) · 3.75 KB
/
start_emulator.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash
RESET="\033[0m"
F_BOLD="\033[1m"
F_BOLD_RESET="\033[22m"
C_RED="\033[31m"
TIMEOUT_IN_SEC=360
function log_info() { printf -- "%s\n" "$*"; }
function log_success() { printf -- "\n${F_BOLD}${C_GREEN}%s${RESET}\n" "$*"; }
function log_error() { printf -- "\n${F_BOLD}${C_RED}ERROR:${F_BOLD_RESET} %s${RESET}\n" "$*"; }
function fail_with_msg() {
printf -- "\n${F_BOLD}${C_RED}ERROR:${F_BOLD_RESET} %s${RESET}\n" "$*";
exit 1
}
function check_kvm() {
cpu_support_hardware_acceleration=$(grep -cw ".*\(vmx\|svm\).*" /proc/cpuinfo)
if [ "$cpu_support_hardware_acceleration" != 0 ]; then
echo 1
else
echo 0
fi
}
function configure_emulator() {
log_info "Let's configure emulator via adb.."
adb shell "settings put global window_animation_scale 0.0"
adb shell "settings put global transition_animation_scale 0.0"
adb shell "settings put global animator_duration_scale 0.0"
adb shell "settings put secure spell_checker_enabled 0"
adb shell "settings put secure show_ime_with_hard_keyboard 1"
# This is not applied to system images with API level < 26
# as there is not a reliable boot complete signal communicated back to the host for those system images.
adb shell "settings put system screen_off_timeout 2147483647"
adb shell "settings put secure long_press_timeout 1500"
# Hidden APIs
# https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces#how_can_i_enable_access_to_non-sdk_interfaces
# Android 9
adb shell "settings put global hidden_api_policy_pre_p_apps 1"
adb shell "settings put global hidden_api_policy_p_apps 1"
# Android 10+
adb shell "settings put global hidden_api_policy 1"
adb shell "am broadcast -a com.android.intent.action.SET_LOCALE --es com.android.intent.extra.LOCALE EN"
adb shell input keyevent 82
}
function wait_emulator() {
log_info "Wait device..."
timeout $TIMEOUT_IN_SEC adb wait-for-device && log_success "Device connected!" || \
fail_with_msg "Timeout ($TIMEOUT_IN_SEC seconds) reached - failed to wait the device"
}
function wait_emulator_boot_completed() {
failcounter=0
log_info "Wait when boot is fully completed..."
while [ true ]; do
status=$(adb shell getprop sys.boot_completed | tr -d '\r')
if [ "$status" == "1" ]; then
log_success "Boot completed!"
break
else
let "failcounter += 1"
if [[ $failcounter -gt TIMEOUT_IN_SEC ]]; then
fail_with_msg "Timeout ($TIMEOUT_IN_SEC seconds) reached - failed to start emulator"
fi
sleep 1
log_info "try #$failcounter/$TIMEOUT_IN_SEC, boot in progress, still waiting..."
fi
done
}
function run_emulator() {
adb devices | grep emulator | cut -f1 | while read line; do adb -s $line emu kill; done
if [ "${SNAPSHOT_ENABLED}" == "true" ]; then
log_info "Snapshots: Emulator will be run with loading snapshot (name: ${DEFAULT_SNAPSHOT})"
snapshot_arguments+=(-snapshot ${DEFAULT_SNAPSHOT} -no-snapshot-save)
else
log_info "Snapshots: Emulator will be run without snapshot feature"
snapshot_arguments+=(-no-snapshot)
fi
${ANDROID_HOME}/emulator/emulator -avd ${EMULATOR_NAME} -verbose -no-boot-anim -wipe-data ${snapshot_arguments} -no-audio -no-window &
wait_emulator
wait_emulator_boot_completed
}
function start_emulator_if_possible() {
log_info "Let's try to run the emulator"
check_kvm=$(check_kvm)
if [ "$check_kvm" != "1" ]; then
log_error "Run emulator failed, nested virtualization is not supported"
exit 1
else
run_emulator
sleep 1
configure_emulator
fi
}
start_emulator_if_possible