Last active
January 31, 2025 18:53
-
-
Save maikotrindade/e38980d77a78836ea45ef4b4cc852e63 to your computer and use it in GitHub Desktop.
Create an Android Emulator Pixel 2 in a MacOS machine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e # Exit immediately if a command exits with a non-zero status. | |
# Configuration Variables | |
ANDROID_VERSION="31" | |
SYSTEM_IMAGE="google_apis;x86_64" | |
AVD_NAME="Pixel_2_API_31" | |
RAM_SIZE="2048" | |
ANDROID_HOME="$HOME/Library/Android/sdk" | |
CMDLINE_TOOLS_URL="https://dl.google.com/android/repository/commandlinetools-mac-9123335_latest.zip" | |
PLATFORM_TOOLS_URL="https://dl.google.com/android/repository/platform-tools-latest-darwin.zip" | |
AVD_PATH="$HOME/.android/avd/${AVD_NAME}.avd" | |
# Helper Function to Check If Emulator Is Installed | |
is_emulator_installed() { | |
command -v emulator &> /dev/null | |
} | |
# Helper Function to Check If AVD Exists | |
is_avd_created() { | |
[[ -d "$AVD_PATH" ]] | |
} | |
# Install Command-line Tools | |
install_cmdline_tools() { | |
echo "Setting up Android Command-line Tools..." | |
mkdir -p "$ANDROID_HOME/cmdline-tools" | |
if [[ ! -f "$ANDROID_HOME/cmdline-tools/cmdline-tools.zip" ]]; then | |
echo "Downloading Command-line Tools..." | |
curl -L -o "$ANDROID_HOME/cmdline-tools/cmdline-tools.zip" "$CMDLINE_TOOLS_URL" | |
fi | |
if [[ ! -d "$ANDROID_HOME/cmdline-tools/latest" ]]; then | |
echo "Extracting Command-line Tools..." | |
unzip -q "$ANDROID_HOME/cmdline-tools/cmdline-tools.zip" -d "$ANDROID_HOME/cmdline-tools" | |
mv "$ANDROID_HOME/cmdline-tools/cmdline-tools" "$ANDROID_HOME/cmdline-tools/latest" | |
fi | |
} | |
# Install Platform Tools | |
install_platform_tools() { | |
echo "Setting up Android Platform Tools..." | |
PLATFORM_TOOLS_DIR="$ANDROID_HOME/platform-tools" | |
if [[ ! -d "$PLATFORM_TOOLS_DIR" ]]; then | |
echo "Downloading Platform Tools..." | |
curl -L -o "$ANDROID_HOME/platform-tools.zip" "$PLATFORM_TOOLS_URL" | |
echo "Extracting Platform Tools..." | |
unzip -q "$ANDROID_HOME/platform-tools.zip" -d "$ANDROID_HOME" | |
fi | |
} | |
# Accept Licenses and Install SDK Components | |
install_sdk_components() { | |
echo "Accepting Android SDK licenses..." | |
yes | sdkmanager --licenses | |
echo "Installing necessary Android SDK components..." | |
sdkmanager "platform-tools" "platforms;android-${ANDROID_VERSION}" "system-images;android-${ANDROID_VERSION};${SYSTEM_IMAGE}" "emulator" | |
} | |
# Create AVD | |
create_avd() { | |
echo "Creating AVD for ${AVD_NAME}..." | |
echo "no" | avdmanager create avd -n "${AVD_NAME}" -k "system-images;android-${ANDROID_VERSION};${SYSTEM_IMAGE}" -d "pixel_2" | |
echo "Configuring AVD settings..." | |
echo "hw.ramSize=${RAM_SIZE}" >> "${AVD_PATH}/config.ini" | |
} | |
# Run the Emulator | |
run_emulator() { | |
echo "Launching the emulator..." | |
emulator -avd "${AVD_NAME}" & | |
echo "Pixel 2 emulator with API 31 launched successfully!" | |
} | |
# Main Script Logic | |
export ANDROID_HOME="$HOME/Library/Android/sdk" | |
export PATH="$ANDROID_HOME/emulator:$ANDROID_HOME/platform-tools:$ANDROID_HOME/cmdline-tools/latest/bin:$PATH" | |
if is_emulator_installed; then | |
echo "Emulator is already installed." | |
if is_avd_created; then | |
echo "AVD ${AVD_NAME} already exists." | |
run_emulator | |
else | |
echo "AVD ${AVD_NAME} does not exist. Creating it..." | |
create_avd | |
run_emulator | |
fi | |
else | |
echo "Emulator is not installed. Setting up everything..." | |
install_cmdline_tools | |
install_platform_tools | |
install_sdk_components | |
if is_avd_created; then | |
echo "AVD ${AVD_NAME} already exists." | |
else | |
create_avd | |
fi | |
run_emulator | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We'll need an auto-update check for new command line tools versions.
We'll need to make sure that
sdkmanager
checks for updates and runs periodically.If we can get this script to also run as an updater, including command line tools, platform tools, the platform image, update the android emulator from any new platform images.