#!/usr/bin/env bash # # start-emulator.sh — boot the JTube TV test emulator on the HOST (not inside the # agent container), with GPU acceleration and a window. Use this when the app should # be tested over scrcpy (ATD/system quirks make scrcpy fail against headless # in-container emulators) or when you simply want to watch it. # # The agent container bridges rutherford:5556 -> 127.0.0.1:5601 with socat, so after # this script the emulator is reachable as: # # adb connect rutherford:5556 && scrcpy -s rutherford:5556 # # An RSA authorization popup appears on the emulator window on first connect — # click "Always allow"/OK. set -euo pipefail SDK="${ANDROID_SDK_ROOT:-$HOME/Android/Sdk}" EMU="$SDK/emulator/emulator" ADB="$SDK/platform-tools/adb" AVD="${1:-jtubesc}" PORT=5600 # console port; adbd listens on 5601 (the container bridges 5556 -> 5601) [ -x "$EMU" ] || { echo "emulator not found at $EMU"; exit 1; } [ -f "$HOME/.android/avd/$AVD.ini" ] || { echo "AVD $AVD not found; list: (ls ~/.android/avd)"; exit 1; } # Free stale emulator instances holding our ports (only qemu from OUR SDK path). for pid in $(ps aux | awk '/[q]emu-system-x86_64/ {print $2}' 2>/dev/null); do if tr '\0' ' ' < "/proc/$pid/cmdline" 2>/dev/null | grep -q "\-port $PORT"; then echo "Killing stale emulator pid $pid" kill -9 "$pid" 2>/dev/null || true fi done sleep 1 ACCEL=() if [ -e /dev/kvm ]; then ACCEL+=(-accel kvm); echo "/dev/kvm found — hardware acceleration on" else echo "no /dev/kvm — falling back to software rendering (slow)" fi echo "Booting AVD '$AVD' on console :$PORT (adbd :$((PORT+1))) …" setsid nohup "$EMU" -avd "$AVD" -port "$PORT" -gpu host "${ACCEL[@]:-}" \ -no-snapshot > /tmp/jtube-emu.log 2>&1 < /dev/null & echo "emulator PID $! — log: /tmp/jtube-emu.log" for i in $(seq 1 90); do ok=$("$ADB" -s "emulator-$PORT" shell getprop sys.boot_completed 2>/dev/null | tr -d '\r') [ "$ok" = "1" ] && { echo "BOOTED"; break; } sleep 5 done # Keep the JTube TV app current + running so scrcpy lands on it. APK="$HOME/Projects/jtube-tv/app/build/outputs/apk/debug/app-debug.apk" if [ -f "$APK" ]; then echo "Installing JTube TV…" "$ADB" -s "emulator-$PORT" install -r "$APK" >/dev/null && echo "installed" "$ADB" -s "emulator-$PORT" shell am start -n net.jeena.jtubetv/.MainActivity && echo "app started" fi cat <