返回 Skill 列表
extension
分类: 开发与工程无需 API Key

cleanup-android-emulators

删除由构建安卓应用技能创建的所有安卓模拟器。查找符合{project}_{branch}命名规则的AVD,关闭所有正在运行的,并删除它们。当用户说“清理模拟器”、“删除模拟器”、“移除AVD”或“清理安卓模拟器”时使用。

person作者: jakexiaohubgithub

Cleanup Android Emulators

Remove all AVDs that follow the {project}_{branch} naming convention used by the build-android-app skill.

Step 1: List matching AVDs

AVDs created by the build-android-app skill are named {project}_{branch}, where project comes from the git remote. Non-matching AVDs (e.g. ones created manually in Android Studio) are left untouched.

PROJECT=$(git remote get-url origin 2>/dev/null | sed 's/.*\///' | sed 's/\.git$//')

if [ -z "$PROJECT" ]; then
  echo "ERROR: Not in a git repo. Run from a project directory to derive the project name."
  exit 1
fi

PREFIX="${PROJECT}_"
ALL_AVDS=$($ANDROID_HOME/emulator/emulator -list-avds 2>/dev/null)
MATCHING_AVDS=$(echo "$ALL_AVDS" | grep "^${PREFIX}")

if [ -z "$MATCHING_AVDS" ]; then
  echo "No AVDs matching '${PREFIX}*' found. Nothing to clean up."
  exit 0
fi

echo "AVDs matching '${PREFIX}*':"
echo "$MATCHING_AVDS"
echo ""
echo "AVDs that will NOT be touched:"
echo "$ALL_AVDS" | grep -v "^${PREFIX}" || echo "  (none)"

Show this output to the user and confirm before proceeding.

Step 2: Shut down running emulators

Before deleting, shut down any matching AVDs that are currently running. Never shut down non-matching emulators.

for SERIAL in $(adb devices | grep 'emulator-' | awk '{print $1}'); do
  AVD_ON_SERIAL=$(adb -s "$SERIAL" emu avd name 2>/dev/null | head -1 | tr -d '\r')
  if echo "$MATCHING_AVDS" | grep -qx "$AVD_ON_SERIAL"; then
    echo "Shutting down '$AVD_ON_SERIAL' on $SERIAL..."
    adb -s "$SERIAL" emu kill 2>/dev/null
    sleep 1
  fi
done

Step 3: Delete AVDs

for AVD in $MATCHING_AVDS; do
  echo "Deleting AVD '$AVD'..."
  $ANDROID_HOME/cmdline-tools/latest/bin/avdmanager delete avd -n "$AVD" 2>&1
done

echo ""
echo "Cleanup complete. Remaining AVDs:"
$ANDROID_HOME/emulator/emulator -list-avds 2>/dev/null || echo "  (none)"