Skip to content
Open
Show file tree
Hide file tree
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
25 changes: 19 additions & 6 deletions .github/workflows/maestro-android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ jobs:
distribution: 'temurin'
java-version: '17'

- name: Setup Android SDK
uses: android-actions/setup-android@v3

- name: Create AVD
run: |
chmod +x scripts/create-avd.sh
scripts/create-avd.sh

- name: Download APK
uses: actions/download-artifact@v4
with:
Expand All @@ -46,17 +54,22 @@ jobs:
- name: Make Maestro script executable
run: chmod +x .github/scripts/run-maestro.sh

- name: Prepare Emulator Config
run: |
chmod +x scripts/create-avd.sh
scripts/create-avd.sh

- name: Start Android Emulator and Run Maestro Tests
uses: reactivecircus/android-emulator-runner@v2
timeout-minutes: 120
timeout-minutes: 60
with:
api-level: 34
disk-size: 4096M
arch: x86_64
target: google_apis
profile: pixel_7_pro
api-level: ${{ env.API_LEVEL }}
arch: ${{ env.ABI }}
target: ${{ env.TARGET }}
profile: ${{ env.PROFILE }}
cores: 4
ram-size: 4096M
disk-size: 4096M
force-avd-creation: false
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
disable-animations: true
Expand Down
78 changes: 70 additions & 8 deletions scripts/create-avd.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,76 @@
#!/usr/bin/env bash
set -e

$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --install emulator
if [ -z "$ANDROID_HOME" ]; then
export ANDROID_HOME="$ANDROID_SDK_ROOT"
fi

$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager "system-images;android-31;default;arm64-v8a"
$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --licenses
if [ -z "$ANDROID_HOME" ]; then
export ANDROID_HOME="$HOME/Android/Sdk"
fi

$ANDROID_HOME/cmdline-tools/latest/bin/avdmanager create avd -n Pixel_API_31_AOSP -d pixel --package "system-images;android-31;default;arm64-v8a"
export PATH=$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/emulator:$PATH

echo "hw.lcd.density = 440" >> ~/.android/avd/Pixel_API_31_AOSP.avd/config.ini
echo "hw.lcd.height = 2280" >> ~/.android/avd/Pixel_API_31_AOSP.avd/config.ini
echo "hw.lcd.width = 1080" >> ~/.android/avd/Pixel_API_31_AOSP.avd/config.ini
API_LEVEL=34
PROFILE="pixel_7_pro"
TARGET="google_apis"

echo "Pixel_API_31_AOSP created"
HOST_ARCH=$(uname -m)

if [[ "$HOST_ARCH" == "arm64" || "$HOST_ARCH" == "aarch64" ]]; then
ABI="arm64-v8a"
else
ABI="x86_64"
fi

AVD_NAME="${PROFILE}-api-${API_LEVEL}-${TARGET}-${ABI}"

export API_LEVEL
export PROFILE
export TARGET
export ABI
export AVD_NAME

if [ -n "$GITHUB_ENV" ]; then
echo "API_LEVEL=$API_LEVEL" >> "$GITHUB_ENV"
echo "PROFILE=$PROFILE" >> "$GITHUB_ENV"
echo "TARGET=$TARGET" >> "$GITHUB_ENV"
echo "ABI=$ABI" >> "$GITHUB_ENV"
echo "AVD_NAME=$AVD_NAME" >> "$GITHUB_ENV"
fi

# ✅ STOP HERE when running in GitHub Actions
if [ "$GITHUB_ACTIONS" = "true" ]; then
echo "Running in GitHub Actions — skipping AVD creation"
exit 0
fi

HOST_ARCH=$(uname -m)
echo "Host architecture: $HOST_ARCH"
echo "Using emulator ABI: $ABI"

IMAGE="system-images;android-${API_LEVEL};${TARGET};${ABI}"

echo "Installing emulator + system image"

sdkmanager --install emulator
sdkmanager "$IMAGE"
yes | sdkmanager --licenses

echo "Creating AVD"

echo "no" | avdmanager create avd \
-n "$AVD_NAME" \
-d "$PROFILE" \
--package "$IMAGE"
Comment on lines +62 to +65
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

cat -n scripts/create-avd.sh

Repository: RocketChat/Rocket.Chat.ReactNative

Length of output: 2338


🏁 Script executed:

# Check avdmanager documentation/common usage patterns
rg -i 'avdmanager|set -e' scripts/create-avd.sh

Repository: RocketChat/Rocket.Chat.ReactNative

Length of output: 120


🌐 Web query:

avdmanager create avd --force idempotent options

💡 Result:

To make avdmanager create avd idempotent in scripts/CI, you basically have two choices:

1) “Ensure this AVD exists (and matches my desired config)” → overwrite deterministically

Use force overwrite plus non-interactive input:

echo "no" | avdmanager create avd \
  --name my_avd \
  --package "system-images;android-34;google_apis;x86_64" \
  --force
  • -f/--force overwrites an existing AVD with the same name (this is the closest built-in “idempotent” behavior). [1]
  • echo "no" | ... avoids the interactive prompt (commonly used in CI). [2]

Useful create avd options (the ones that matter for reproducibility):

  • -n/--name AVD name (required) [1]
  • -k/--package system image package id (required) [1]
  • -c/--sdcard {path|size} set/allocate SD card [1]
  • -p/--path AVD directory location [1]
  • -f/--force overwrite existing AVD [1]
  • (Also commonly available) --device, --tag, --abi depending on cmdline-tools version; these show up in avdmanager create avd --help output in the field. [2]

2) “Create only if missing” → don’t touch existing AVDs

Do an existence check first, then create without --force:

if ! avdmanager list avd -c | grep -q '^my_avd$'; then
  echo "no" | avdmanager create avd -n my_avd -k "system-images;android-34;google_apis;x86_64"
fi

avdmanager itself doesn’t provide a dedicated “create-if-not-exists” flag; scripting with list avd is the typical approach. [1]


🏁 Script executed:

# Search for avdmanager documentation or common usage patterns
rg 'avdmanager\s+create' --type bash -B2 -A2

Repository: RocketChat/Rocket.Chat.ReactNative

Length of output: 106


🏁 Script executed:

# Let me search for the actual output format of avdmanager list avd
# to verify if the proposed grep pattern is correct
rg 'list avd|grep' --type sh -B2 -A2 . 2>/dev/null | head -30

Repository: RocketChat/Rocket.Chat.ReactNative

Length of output: 60


🏁 Script executed:

fd -type f -name '*.sh' | xargs rg -l 'avdmanager' 2>/dev/null

Repository: RocketChat/Rocket.Chat.ReactNative

Length of output: 287


🏁 Script executed:

# Check if there are any CI/workflow files that might show how avdmanager is used
fd -type f -name '*.yml' -o -name '*.yaml' | xargs rg -l 'create-avd|avdmanager' 2>/dev/null | head -5

Repository: RocketChat/Rocket.Chat.ReactNative

Length of output: 287


🌐 Web query:

avdmanager list avd output format "Name:" example

💡 Result:

avdmanager list avd prints a “pretty” multi-line block per AVD that includes lines like Name: (and usually Device:, Path:, Target:, etc.). Example output:

$ avdmanager list avd
Available Android Virtual Devices:
    Name: Pixel_C_Tablet_API_30
  Device: pixel_c (Google)
    Path: /Users/alleng/.android/avd/Pixel_C_Tablet_API_30.avd
  Target: Google APIs (Google Inc.)
          Based on: Android API 30 Tag/ABI: google_apis/x86
    Skin: pixel_c
  Sdcard: 128 MB

[2]

If you need a script-friendly format, avdmanager supports list ... -c for “compact output suitable for scripts” when listing AVDs. [1]

Sources: [1] https://developer.android.com/tools/avdmanager, [2] https://blog.csdn.net/wizardforcel/article/details/141383947


Add idempotency check or use --force flag to handle existing AVDs on reruns.

Line 62 will fail on subsequent runs if the AVD already exists (set -e aborts the script). Additionally, appending to config.ini (lines 67–74) will duplicate settings on reruns.

Use --force flag for simpler idempotent creation:

Suggested fix (simplest)
 echo "no" | avdmanager create avd \
   -n "$AVD_NAME" \
   -d "$PROFILE" \
   --package "$IMAGE" \
+  --force

Alternatively, check and delete the existing AVD first (note: adjust the grep pattern—avdmanager list avd output has leading whitespace before Name:):

Alternative: delete-then-create approach
+if avdmanager list avd | grep -q "[[:space:]]*Name: ${AVD_NAME}$"; then
+  echo "Deleting existing AVD: $AVD_NAME"
+  avdmanager delete avd -n "$AVD_NAME"
+fi
+
 echo "no" | avdmanager create avd \
   -n "$AVD_NAME" \
   -d "$PROFILE" \
   --package "$IMAGE"
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@scripts/create-avd.sh` around lines 62 - 65, The avd creation step using
avdmanager create avd -n "$AVD_NAME" will fail on reruns and appends duplicate
settings to config.ini; make it idempotent by either adding the --force flag to
the avdmanager create avd invocation (to overwrite existing AVD) or by checking
for and removing an existing AVD name (parse avdmanager list avd output
carefully—look for lines like "Name:" that may have leading whitespace—then call
avdmanager delete avd before creating). Also replace the append-to-config.ini
logic with a safe write/replace approach (overwrite or filter existing keys) so
repeated runs do not duplicate entries in config.ini. Ensure you reference
AVD_NAME, avdmanager create avd, avdmanager list avd, avdmanager delete avd, and
the config.ini write block when implementing the fix.


CONFIG="$HOME/.android/avd/${AVD_NAME}.avd/config.ini"

echo "hw.lcd.density=440" >> "$CONFIG"
echo "hw.lcd.height=2280" >> "$CONFIG"
echo "hw.lcd.width=1080" >> "$CONFIG"
echo "hw.gpu.enabled=yes" >> "$CONFIG"
echo "hw.ramSize=4096" >> "$CONFIG"
echo "vm.heapSize=512" >> "$CONFIG"

echo "AVD created: $AVD_NAME ($ABI)"
Loading