emulator.sh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. SDK="${ANDROID_HOME:-/opt/android-sdk}"
  4. ADB="$SDK/platform-tools/adb"
  5. EMULATOR="$SDK/emulator/emulator"
  6. API="${1:-35}"
  7. GPU_MODE="${CARDASH_GPU:-host}"
  8. if [[ "$API" == "stop" ]]; then
  9. if "$ADB" devices | grep -q "emulator-"; then
  10. "$ADB" emu kill >/dev/null 2>&1 || true
  11. echo "Emulator stopped."
  12. else
  13. echo "No emulator is running."
  14. fi
  15. exit 0
  16. fi
  17. if [[ "$API" != "35" && "$API" != "29" ]]; then
  18. echo "Usage: $0 [35|29|stop] [window]" >&2
  19. exit 2
  20. fi
  21. AVD="cardash_tablet_api$API"
  22. if ! "$ADB" devices | grep -q "emulator-"; then
  23. args=(
  24. -avd "$AVD"
  25. -no-audio
  26. -no-boot-anim
  27. -gpu "$GPU_MODE"
  28. -no-snapshot
  29. -no-metrics
  30. -camera-back none
  31. -camera-front none
  32. )
  33. if [[ "${2:-}" != "window" ]]; then
  34. args+=(-no-window)
  35. fi
  36. echo "Booting $AVD (1280x800, landscape)…"
  37. nohup "$EMULATOR" "${args[@]}" > "/tmp/cardash-emulator-api$API.log" 2>&1 &
  38. else
  39. echo "An emulator is already running; reusing it."
  40. fi
  41. "$ADB" wait-for-device
  42. for _ in $(seq 1 90); do
  43. if [[ "$("$ADB" shell getprop sys.boot_completed 2>/dev/null | tr -d '\r')" == "1" ]]; then
  44. echo "Boot completed: $("$ADB" shell wm size | tr -d '\r') @ $("$ADB" shell wm density | tr -d '\r')"
  45. exit 0
  46. fi
  47. sleep 2
  48. done
  49. echo "Emulator did not report boot_completed. See /tmp/cardash-emulator-api$API.log" >&2
  50. exit 1