Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 97 additions & 18 deletions .github/workflows/_android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ jobs:
env:
ANDROID_NDK_VERSION: r28c
API_LEVEL: 34
EMULATOR_DEVICE_ID: emulator-5554
steps:
- name: Setup SSH (Click me for login details)
uses: pytorch/test-infra/.github/actions/setup-ssh@main
Expand All @@ -74,13 +75,28 @@ jobs:
python-version: '3.10'

- name: Install Android dependencies
env:
ANDROID_HOME: /opt/android/sdk
shell: bash
run: |
set -eux

# Reuse the script that install Android on ET Docker image
sudo -E bash .ci/docker/common/install_android.sh

# Move cmdline-tools to the proper structure for SDK manager
if [ -d /opt/cmdline-tools ]; then
sudo mkdir -p $ANDROID_HOME/cmdline-tools
sudo mv /opt/cmdline-tools $ANDROID_HOME/cmdline-tools/latest
else
echo "ERROR: /opt/cmdline-tools not found after installation"
exit 1
fi

# Install emulator and system images
sudo yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --sdk_root="$ANDROID_HOME" "emulator"
sudo yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --sdk_root="$ANDROID_HOME" "system-images;android-${{ env.API_LEVEL }};default;x86_64"

# After https://github.com/ReactiveCircus/android-emulator-runner/releases/tag/v2.33.0 release,
# it seems that we need to chown the Android setup to the current user instead of root to
# avoid permission issue
Expand All @@ -107,23 +123,86 @@ jobs:
~/.android/adb*
key: avd-${{ env.API_LEVEL }}

# NB: It takes about 10m to cold boot the emulator here
- name: Run Android emulator
- name: Create AVD
env:
ANDROID_HOME: /opt/android/sdk
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: ${{ env.API_LEVEL }}
arch: x86_64
script: ./scripts/run_android_emulator.sh
# NB: This is to boot the emulator faster following the instructions on
# https://github.com/ReactiveCircus/android-emulator-runner. The max number
# of cores we can set is 6, any higher number will be reduced to 6.
cores: 6
ram-size: 16384M
heap-size: 12288M
force-avd-creation: false
disable-animations: true
emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
# This is to make sure that the job doesn't fail flakily
emulator-boot-timeout: 900
shell: bash
run: |
set -eux
# Create AVD if it doesn't exist
if [ ! -d "$HOME/.android/avd/test_avd.avd" ]; then
echo "Creating AVD"
echo "no" | $ANDROID_HOME/cmdline-tools/latest/bin/avdmanager create avd \
--force \
--name test_avd \
--package "system-images;android-${{ env.API_LEVEL }};default;x86_64" \
--abi x86_64 \
--device "pixel_5"
else
echo "AVD already exists"
fi

- name: Start Emulator
env:
ANDROID_HOME: /opt/android/sdk
shell: bash
run: |
set -eux
# Start emulator in background with optimized flags
$ANDROID_HOME/emulator/emulator \
-avd test_avd \
-no-snapshot-load \
-no-window \
-gpu swiftshader_indirect \
-no-boot-anim \
-noaudio \
-camera-back none \
-cores 6 \
-memory 16384 \
-no-snapshot-save &

# Start ADB server
$ANDROID_HOME/platform-tools/adb start-server

# Wait for emulator device to be detected and boot (up to 15 minutes)
echo "Waiting for emulator device to be detected..."
for i in {1..90}; do
# First check if the device is detected
if ! $ANDROID_HOME/platform-tools/adb devices | grep -q "${{ env.EMULATOR_DEVICE_ID }}"; then
echo "Device not yet detected... (attempt $i/90)"
sleep 10
continue
fi

# Then check if boot is completed
boot_completed=$($ANDROID_HOME/platform-tools/adb -s ${{ env.EMULATOR_DEVICE_ID }} shell getprop sys.boot_completed 2>/dev/null | tr -d '\r')
if [[ "$boot_completed" == "1" ]]; then
echo "Emulator booted successfully."
break
fi
echo "Device detected but waiting for boot to complete... (attempt $i/90)"
sleep 10
done

# Verify boot completion
boot_status=$($ANDROID_HOME/platform-tools/adb -s ${{ env.EMULATOR_DEVICE_ID }} shell getprop sys.boot_completed 2>/dev/null | tr -d '\r')
if [[ "$boot_status" != "1" ]]; then
echo "ERROR: Emulator failed to boot within timeout"
$ANDROID_HOME/platform-tools/adb devices
exit 1
fi

- name: Check Devices
env:
ANDROID_HOME: /opt/android/sdk
shell: bash
run: |
set -eux
echo "Listing connected devices:"
$ANDROID_HOME/platform-tools/adb devices

- name: Run Android Tests
env:
ANDROID_HOME: /opt/android/sdk
shell: bash
run: ./scripts/run_android_emulator.sh
Loading