#!/usr/bin/env bash set -e AVD_NAME="${AVD_NAME:-inbe-test}" # Check for Android SDK location if [ -z "$ANDROID_SDK_ROOT" ] && [ -z "$ANDROID_HOME" ]; then echo "❌ Error: Neither ANDROID_SDK_ROOT nor ANDROID_HOME is set" exit 1 fi # Set up writable SDK location PERSISTENT_SDK_ROOT="$HOME/.android-sdk-writable" ORIGINAL_SDK_ROOT="${ANDROID_SDK_ROOT:-$ANDROID_HOME}" mkdir -p "$PERSISTENT_SDK_ROOT" for component in build-tools cmake cmdline-tools emulator licenses ndk platforms platform-tools tools; do if [ -n "$ORIGINAL_SDK_ROOT" ] && [ -e "$ORIGINAL_SDK_ROOT/$component" ] && [ ! -e "$PERSISTENT_SDK_ROOT/$component" ]; then ln -sf "$ORIGINAL_SDK_ROOT/$component" "$PERSISTENT_SDK_ROOT/$component" fi done if [ -d "$PERSISTENT_SDK_ROOT" ] && [ -d "$PERSISTENT_SDK_ROOT/avd/$AVD_NAME.avd" ]; then export ANDROID_SDK_ROOT="$PERSISTENT_SDK_ROOT" export ANDROID_SDK_HOME="$PERSISTENT_SDK_ROOT" export ANDROID_AVD_HOME="$PERSISTENT_SDK_ROOT/avd" else echo "ℹ️ AVD not found. Creating new Pixel 8 Pro AVD..." bash scripts/create-avd.sh "$AVD_NAME" # Re-export after creation export ANDROID_SDK_ROOT="$PERSISTENT_SDK_ROOT" export ANDROID_SDK_HOME="$PERSISTENT_SDK_ROOT" export ANDROID_AVD_HOME="$PERSISTENT_SDK_ROOT/avd" fi ADB_CMD="${ANDROID_SDK_ROOT}/platform-tools/adb" if [ ! -x "$ADB_CMD" ]; then ADB_CMD="$(command -v adb)" fi EMULATOR_CMD="${ANDROID_SDK_ROOT}/emulator/emulator" if [ ! -x "$EMULATOR_CMD" ]; then EMULATOR_CMD="$(command -v emulator)" fi EMULATOR_DIR="$(cd "$(dirname "$EMULATOR_CMD")" && pwd)" EMULATOR_LD_LIBRARY_PATH="${ANDROID_EMULATOR_LD_LIBRARY_PATH:-$EMULATOR_DIR/lib64:$EMULATOR_DIR/lib64/qt/lib}" # Check if emulator is already running if "$ADB_CMD" devices | grep -q '^emulator-[0-9][0-9]*[[:space:]]*device'; then echo "✅ Emulator already running" "$ADB_CMD" devices exit 0 fi if LD_LIBRARY_PATH="$EMULATOR_LD_LIBRARY_PATH${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" ldd "$EMULATOR_CMD" 2>/dev/null | grep -q "not found"; then echo "❌ Emulator runtime libraries are missing." echo " Re-enter the repo flake shell so the updated emulator LD_LIBRARY_PATH is active:" echo " nix develop --impure" exit 1 fi echo "🚀 Launching Pixel 8 Pro emulator (Android 15 API 35)..." # Set environment for emulator unset ANDROID_HOME export ANDROID_SDK_ROOT="$ANDROID_SDK_ROOT" # Launch emulator in background with KVM acceleration LD_LIBRARY_PATH="$EMULATOR_LD_LIBRARY_PATH${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" "$EMULATOR_CMD" @"$AVD_NAME" \ -gpu host \ -skin 1440x2960 \ -no-snapshot-load \ -no-boot-anim \ -verbose \ -qemu -enable-kvm > /tmp/emulator.log 2>&1 & echo "⏳ Waiting for boot (this may take a while)..." timeout_seconds=120 elapsed=0 timeout 20 "$ADB_CMD" -e wait-for-device >/dev/null 2>&1 || true while ! "$ADB_CMD" -e shell getprop sys.boot_completed 2>/dev/null | grep -q "1"; do if [ $elapsed -ge $timeout_seconds ]; then echo "❌ Timeout waiting for boot. Check /tmp/emulator.log" exit 1 fi sleep 5 elapsed=$((elapsed + 5)) if ! pgrep -f "emulator.*$AVD_NAME" > /dev/null; then echo "❌ Emulator died. Check /tmp/emulator.log" tail -20 /tmp/emulator.log exit 1 fi # Show progress if [ $((elapsed % 15)) -eq 0 ]; then echo " Still booting... (${elapsed}s)" fi done echo "✅ Pixel 8 Pro emulator ready!" echo "🎯 Enabling punch-hole display cutout overlay if available..." if "$ADB_CMD" -e shell cmd overlay list 2>/dev/null | grep -q "com.android.internal.display.cutout.emulation.hole"; then "$ADB_CMD" -e shell cmd overlay enable --user 0 com.android.internal.display.cutout.emulation.hole || true elif "$ADB_CMD" -e shell cmd overlay list 2>/dev/null | grep -q "com.android.internal.display.cutout.emulation.corner"; then "$ADB_CMD" -e shell cmd overlay enable --user 0 com.android.internal.display.cutout.emulation.corner || true elif "$ADB_CMD" -e shell cmd overlay list 2>/dev/null | grep -q "com.android.internal.display.cutout.emulation.tall"; then "$ADB_CMD" -e shell cmd overlay enable --user 0 com.android.internal.display.cutout.emulation.tall || true else echo " No display cutout emulation overlay found on this image" fi echo "" echo "📱 Device info:" "$ADB_CMD" -e shell getprop ro.product.model "$ADB_CMD" -e shell getprop ro.build.version.release "$ADB_CMD" -e shell getprop ro.build.version.sdk echo "" echo " Connect with: $ADB_CMD -e shell"