-
Notifications
You must be signed in to change notification settings - Fork 0
/
android-emulator
106 lines (95 loc) · 2.98 KB
/
android-emulator
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
#!/bin/bash
echo "
------- Android Emulator Shell Srcript -------
A shell script for Unix-based systems (Linux, macOS, etc...) that basically
allows you to run your Android Emulator (has to be already installed) without
the need to open Android Studio everytime you want to launch your emulator.
|----------------------------------------------------------------------|
| Author: Mohammad Shbool <@mohshbool> (https://github.com/mohshbool) |
|----------------------------------------------------------------------|
"
# Helper funcions
getDevices() {
while IFS=' ' read -ra args; do
devices=( )
for index in "${!args[@]}"; do
if [[ "Name:" == "${args[index]}" ]]; then
devices+=("${args[index+1]}")
fi
done
echo ${devices[@]}
done <<< $(sh -c "$listAvd")
}
isDeviceValid() {
devices=$(getDevices)
while IFS=' ' read -ra devices; do
for device in "${devices[@]}"; do
echo $device
if [[ $1 == $device ]]; then
return 0
fi
done
done <<< $(getDevices)
return 1
}
pp_Devices() {
while IFS=' ' read -ra devices; do
for i in "${!devices[@]}"; do
echo "[$i] - ${devices[$i]}"
let devicesLen=$i+1
done
done <<< $(getDevices)
}
getDevice() {
while IFS=' ' read -ra devices; do
for i in "${!devices[@]}"; do
if [[ $1 == $i ]]; then
device="${devices[$i]}"
return 0
fi
done
done <<< $(getDevices)
}
# OS check
if [[ "$OSTYPE" == "linux-gnu" ]]; then
binPath=~/Android/Sdk/tools/bin
# Android Studio Version Check
if [[ ! -d "~/Android/Sdk/tools" ]]; then
# @^3.2
emuPath=~/Android/Sdk/emulator
else
emuPath=~/Android/Sdk/tools
fi
elif [[ "$OSTYPE" == "darwin"* ]]; then
binPath=~/Library/Android/Sdk/tools/bin
emuPath=~/Library/Android/Sdk/tools
else
echo "Script is unable to run, incompatible operating system. Quitting ...."
exit 1
fi
# Constant
listAvd="$binPath/avdmanager list avd"
# Arguments guidance
if [[ $1 == "show" ]]; then
pp_Devices
elif [[ $1 == "run" ]]; then
if isDeviceValid "$2"; then
(cd $emuPath && emulator -avd $2)
else
echo "The device name is invalid. To see the devices you have installed, run android-emulator show"
fi
else
echo "------------------------------------------------------------------------------------"
echo "For future usues, you can use \`android-emulator show\` to show the available devices,"
echo "you can also use \`andorid-emulator run <device_name>\` to launch the device."
echo "------------------------------------------------------------------------------------"
echo "This will show you your available devices and prompt you to choose one by entering it's [number]."
pp_Devices
read -r -p "Your device number: " number
if [[ $number -lt $devicesLen ]] && [[ $number -gt -1 ]]; then
getDevice $number
(cd $emuPath && emulator -avd $device)
else
echo "The number you entered is out of index. Please, try again!"
fi
fi