emulator.sh 1.3 KB

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