-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLaunchAndroidVirtualDevice.sh
executable file
·107 lines (91 loc) · 2.9 KB
/
LaunchAndroidVirtualDevice.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
#!/usr/bin/env bash
# Programatically launching an emulated android device without the need for booting up Android Studio.
# Assumes you have the Android SDK/platform tools installed alongside Android Studio.
# This has only been tested on Mac OS.
# Usage:
# -h, --help Show this help message and exit.
# -l, --list-avds List available Android Virtual Devices.
# [AVD_NAME] Launch specified Android Virtual Device.
set -Eeuo pipefail
# Default SDK path
android_SDK_directory="${ANDROID_SDK_ROOT:-/Users/$(whoami)/Library/Android/sdk}"
function print_usage() {
echo "Usage: $0 [options] [AVD_NAME]"
echo "Options:"
echo " -h Display this help menu."
echo " -l List available AVDs."
echo " -i Install Android SDK platform tools if not installed."
echo " -s Setup an AVD (requires manual input)."
}
function install_sdk() {
if ! command -v brew &>/dev/null; then
echo "Homebrew is not installed. Please install Homebrew first."
exit 1
fi
echo "Installing Android SDK platform tools..."
brew install --cask android-platform-tools
echo "Android SDK platform tools installed."
}
function setup_avd() {
echo "Setting up Android Virtual Device..."
echo "Please follow the instructions here: https://developer.android.com/studio/run/managing-avds"
open "https://developer.android.com/studio/run/managing-avds"
}
function list_avds() {
"$android_SDK_directory/emulator/emulator" -list-avds | grep -v 'INFO'
exit 0
}
function launch_avd() {
if [[ -z $1 ]]; then
echo "Error: AVD name is required if not listing AVDs."
print_usage
exit 1
fi
"$android_SDK_directory/emulator/emulator" -avd "$1" -netdelay none -netspeed full
exit 0
}
function check_dependencies() {
if [[ ! -x "$(command -v grep)" ]]; then
echo "Error: grep is not installed."
exit 1
fi
if [[ ! -d "$android_SDK_directory" || ! -x "$android_SDK_directory/emulator/emulator" ]]; then
echo "Error: Android SDK or emulator not correctly set at $android_SDK_directory."
exit 1
fi
}
OPTIND=1
while getopts "hlis" opt; do
case "$opt" in
h)
print_usage
exit 0
;;
l)
list_avds
;;
i)
install_sdk
exit 0
;;
s)
setup_avd
exit 0
;;
\?)
print_usage
exit 1
;;
esac
done
shift $((OPTIND -1))
check_dependencies
# Check if an AVD name is provided as argument or environment variable
if [[ -z "${1:-}" && -z "${AVD_DEVICE_NAME:-}" ]]; then
echo "Error: No AVD name provided."
print_usage
exit 1
fi
# Use command line argument as AVD name or fallback to environment variable
AVD_NAME="${1:-$AVD_DEVICE_NAME}"
launch_avd "$AVD_NAME"