-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathandroid-emulator.sh
executable file
·149 lines (122 loc) · 2.77 KB
/
android-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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/sh
#
# Starts an Android virtual device with a writeable filesystem.
# If the -hosts option is provided, replaces /etc/hosts on the device with the
# given hosts file.
# If the -return option is given, returns to the caller when the emulator is
# ready, otherwise waits for the emulator process to stop.
# If no emulator -avd option is given, starts the first AVD in the list.
# If no existing AVD is available, creates a new one.
#
# Usage: ./android-emulator.sh [-hosts file] [-return] [-- emulator options]
#
# Copyright 2019, Sebastian Tschan
# https://blueimp.net
#
# Licensed under the MIT license:
# https://opensource.org/licenses/MIT
#
set -e
DEVICE_ID='pixel'
SYSTEM_IMAGE_REGEXP='system-images;android-[0-9]*;google_apis;x86\>'
WRITABLE_SYSTEM_IMAGE='system-images;android-28;google_apis;x86'
SDCARD='512M'
if [ -z "$ANDROID_HOME" ]; then
echo 'Error: ANDROID_HOME is not defined.' >&2
exit 1
fi
adb() {
"$ANDROID_HOME/platform-tools/adb" "$@"
}
emulator() {
"$ANDROID_HOME/emulator/emulator" "$@"
}
avdmanager() {
"$ANDROID_HOME/cmdline-tools/latest/bin/avdmanager" "$@"
}
sdkmanager() {
"$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" "$@"
}
normalize() {
echo "$1" | sed 's/[^a-z A-Z 0-9._-]/-/g'
}
get_avd() {
emulator -list-avds | head -n 1
}
get_image() {
if [ -n "$HOSTS_FILE" ]; then
echo "$WRITABLE_SYSTEM_IMAGE"
else
sdkmanager --list | grep -o "$SYSTEM_IMAGE_REGEXP" | tail -1
fi
}
download_image() {
sdkmanager "$1"
}
create_avd() {
echo 'Downloading system image ...'
download_image "$1"
echo 'System image downloaded.'
echo 'Creating Android Virtual Device ...'
avdmanager create avd \
--name "$(normalize "$DEVICE_ID-${1#*;}")" \
--package "$1" \
--device "$DEVICE_ID" \
--sdcard "$SDCARD"
echo 'Virtual Device created.'
}
has_arg() {
while test $# -gt 0; do
test "$1" = "$ARG" && return 0
shift
done
return 1
}
has_system_prop() {
test "$(adb shell getprop "$1" | tr -d '\r')" = "$2"
}
wait_for_device() {
echo 'Waiting for device to be ready ...'
adb wait-for-device
while ! has_system_prop sys.boot_completed 1; do
sleep 1
done
echo 'Device ready.'
}
update_hosts_file() {
adb root
wait_for_device
adb remount
adb push "$1" /etc/hosts
adb unroot
wait_for_device
}
if [ "$1" = -hosts ]; then
HOSTS_FILE=$2
shift 2
fi
if [ "$1" = -return ]; then
RETURN=true
shift
fi
if [ "$1" = -- ]; then
shift
fi
if ! ARG=-avd has_arg "$@"; then
if [ -z "$(get_avd)" ]; then
create_avd "$(get_image)"
fi
set -- -avd "$(get_avd)" "$@"
fi
if [ -n "$HOSTS_FILE" ]; then
set -- -writable-system "$@"
fi
emulator "$@" & PID=$!
wait_for_device
if [ -n "$HOSTS_FILE" ]; then
update_hosts_file "$HOSTS_FILE"
fi
if [ "$RETURN" = true ]; then
exit
fi
wait "$PID"