22set -x
33# Android functions
44
5- # Detect platform and set variables accordingly
5+ # Common configuration
6+ TARGET=" google_apis_playstore"
7+ EMULATOR_DEVICE=" pixel_6"
8+ EMULATOR_BIN=" emulator"
9+
10+ # Platform-specific configuration
611if [[ " $OSTYPE " == " darwin" * ]]; then
7- # Mac settings
12+ # Mac ARM64 settings
813 ARCH=" arm64-v8a"
914 EMULATOR_COUNT=6
1015 API_LEVEL=" 35"
1116 ANDROID_CMD=" commandlinetools-mac-13114758_latest.zip"
12- EMULATOR_BIN=" emulator"
13- TARGET=" google_apis_playstore" # Mac ARM doesn't have playstore variant
1417 ANDROID_SDK_ROOT=${ANDROID_SDK_ROOT:- " $HOME /Android/sdk" }
18+ RAM_SIZE=" 2048"
19+ EMULATOR_PROCESS=" qemu-system-aarch64-headless"
1520else
16- # Linux settings
21+ # Linux x86_64 settings
1722 ARCH=" x86_64"
1823 EMULATOR_COUNT=4
1924 API_LEVEL=" 34"
2025 ANDROID_CMD=" commandlinetools-linux-13114758_latest.zip"
21- EMULATOR_BIN=" emulator"
22- TARGET=" google_apis_playstore" # Linux can use playstore
2326 ANDROID_SDK_ROOT=${ANDROID_SDK_ROOT:- " /opt/android" }
27+ RAM_SIZE=" 4192"
28+ EMULATOR_PROCESS=" qemu-system-x86_64"
2429fi
2530
26-
27- # Derive build tools version from API level
31+ # Derived configuration (uses the variables set above)
2832BUILD_TOOLS=" ${API_LEVEL} .0.0"
29- ANDROID_ARCH=${ANDROID_ARCH_DEFAULT}
3033ANDROID_API_LEVEL=" android-${API_LEVEL} "
31- ANDROID_APIS=" ${TARGET} ;${ARCH} "
32- EMULATOR_PACKAGE=" system-images;${ANDROID_API_LEVEL} ;${ANDROID_APIS} "
34+ EMULATOR_PACKAGE=" system-images;${ANDROID_API_LEVEL} ;${TARGET} ;${ARCH} "
3335PLATFORM_VERSION=" platforms;${ANDROID_API_LEVEL} "
3436BUILD_TOOL=" build-tools;${BUILD_TOOLS} "
35- export ANDROID_SDK_PACKAGES=" ${EMULATOR_PACKAGE} ${PLATFORM_VERSION} ${BUILD_TOOL} platform-tools"
36- export ANDROID_SDK_ROOT
37+ ANDROID_SDK_PACKAGES=" ${EMULATOR_PACKAGE} ${PLATFORM_VERSION} ${BUILD_TOOL} platform-tools emulator"
3738
38- export PATH=" $PATH :$ANDROID_SDK_ROOT /cmdline-tools/tools:$ANDROID_SDK_ROOT /cmdline-tools/tools/bin:$ANDROID_SDK_ROOT /emulator:$ANDROID_SDK_ROOT /tools/bin:$ANDROID_SDK_ROOT /platform-tools:$ANDROID_SDK_ROOT /build-tools/${BUILD_TOOLS} :$ANDROID_SDK_ROOT /platform-tools/"
39- export EMULATOR_DEVICE=" pixel_6" # all emulators are created with the pixel 6 spec for now
39+ # Export everything
40+ export ARCH EMULATOR_BIN EMULATOR_COUNT EMULATOR_PROCESS API_LEVEL ANDROID_CMD TARGET ANDROID_SDK_ROOT RAM_SIZE
41+ export BUILD_TOOLS ANDROID_API_LEVEL EMULATOR_PACKAGE PLATFORM_VERSION BUILD_TOOL ANDROID_SDK_PACKAGES EMULATOR_DEVICE
42+ export PATH=" $ANDROID_SDK_ROOT /cmdline-tools/tools/bin:$ANDROID_SDK_ROOT /emulator:$ANDROID_SDK_ROOT /platform-tools:$ANDROID_SDK_ROOT /build-tools/${BUILD_TOOLS} :$PATH "
4043
4144
4245# this should only be done when we bump the API version or add a worker to the CI that needs emulators to be setup
4346# Once you've run this, you must also start_for_snapshots() and force_save_snapshots() (see details below)
4447function create_emulators() {
45- # Skip apt install on Mac
48+ # Skip apt install on Mac
4649 if [[ " $OSTYPE " != " darwin" * ]]; then
4750 sudo apt update
48- sudo apt install -y ca-certificates curl git vim bash wget unzip tree htop gzip default-jre libnss3 libxcursor1 libqt5gui5 libc++-dev libxcb-cursor0 htop tree tar gzip gh nload
51+ sudo apt install -y ca-certificates curl git vim bash wget unzip tree htop gzip default-jre libnss3 libxcursor1 libqt5gui5 libc++-dev libxcb-cursor0 tar gh nload
4952 fi
5053
5154 sudo rm -rf $ANDROID_SDK_ROOT
52-
5355 sudo mkdir -p $ANDROID_SDK_ROOT
56+
5457 if [[ " $OSTYPE " == " darwin" * ]]; then
5558 sudo chown $USER :staff $ANDROID_SDK_ROOT
5659 else
5760 sudo chown $USER :$USER $ANDROID_SDK_ROOT
5861 fi
59- # Download the SDK tools
60- if [[ " $OSTYPE " == " darwin" * ]]; then
61- curl -L -o /tmp/${ANDROID_CMD} https://dl.google.com/android/repository/${ANDROID_CMD}
62- else
63- wget https://dl.google.com/android/repository/${ANDROID_CMD} -P /tmp
64- fi
6562
66- # Check if download succeeded
67- if [[ ! -f /tmp/${ANDROID_CMD} ]]; then
68- echo " Failed to download Android SDK tools"
63+ # Download SDK tools
64+ local download_url=" https://dl.google.com/android/repository/${ANDROID_CMD} "
65+ echo " Downloading Android SDK tools..."
66+ curl -fL -o " /tmp/${ANDROID_CMD} " " $download_url "
67+
68+ if [[ ! -f " /tmp/${ANDROID_CMD} " ]]; then
69+ echo " Error: Failed to download Android SDK tools" >&2
6970 return 1
7071 fi
7172
@@ -93,28 +94,21 @@ function create_emulators() {
9394 for i in $( seq 1 $EMULATOR_COUNT )
9495 do
9596 echo " no" | avdmanager --verbose create avd --force --name " emulator$i " --device " ${EMULATOR_DEVICE} " --package " ${EMULATOR_PACKAGE} "
96- CONFIG_FILE=" $HOME /.android/avd/emulator$i .avd/config.ini"
9797
98+ # Configure RAM for each AVD
99+ local config_file=" $HOME /.android/avd/emulator$i .avd/config.ini"
98100 if [[ " $OSTYPE " == " darwin" * ]]; then
99- # Mac: 9 emulators @ 2GB each
100- sed -i ' ' ' s/^hw\.ramSize=.*/hw.ramSize=2048/' " $CONFIG_FILE "
101+ sed -i ' ' " s/^hw\.ramSize=.*/hw.ramSize=${RAM_SIZE} /" " $config_file "
101102 else
102- # Linux: Keep original 4GB
103- sed -i ' s/^hw\.ramSize=.*/hw.ramSize=4192/' " $CONFIG_FILE "
103+ sed -i " s/^hw\.ramSize=.*/hw.ramSize=${RAM_SIZE} /" " $config_file "
104104 fi
105105 done
106106}
107107
108108function start_for_snapshots() {
109109 for i in $( seq 1 $EMULATOR_COUNT )
110110 do
111- if [[ " $OSTYPE " == " darwin" * ]]; then
112- # Mac: Not headless
113- $EMULATOR_BIN @emulator$i -no-snapshot-load &
114- else
115- # Linux: Keep as-is with display
116- DISPLAY=:0 $EMULATOR_BIN @emulator$i -gpu host -accel on -no-snapshot-load &
117- fi
111+ $EMULATOR_BIN @emulator$i -gpu host -accel on -no-snapshot-load &
118112 sleep 20
119113 done
120114}
@@ -131,11 +125,7 @@ function force_save_snapshots() {
131125}
132126
133127function killall_emulators() {
134- if [[ " $OSTYPE " == " darwin" * ]]; then
135- killall qemu-system-aarch64-headless 2> /dev/null || true
136- else
137- killall qemu-system-x86_64 2> /dev/null || true
138- fi
128+ killall " $EMULATOR_PROCESS " 2> /dev/null || true
139129}
140130
141131
0 commit comments