Files

82 lines
2.4 KiB
Bash

#!/bin/bash
set -e
echo "=========================================="
echo " ChunYu Android Emulator Container"
echo "=========================================="
# Configuration
BACKEND_HOST="${BACKEND_HOST:-backend}"
BACKEND_PORT="${BACKEND_PORT:-8000}"
APK_PATH="${APK_PATH:-/app/apks/app-debug.apk}"
LAUNCH_ACTIVITY="${LAUNCH_ACTIVITY:-com.chunyu.app/.MainActivity}"
echo "[1/5] Starting Redroid Android container..."
# Start redroid in background
redroid --host=0.0.0.0 --port=5555 &
REDROID_PID=$!
echo "[2/5] Waiting for Android emulator to be ready..."
# Wait for boot completion
timeout=120
counter=0
while [ $counter -lt $timeout ]; do
if adb devices | grep -q "emulator.*device"; then
echo " ✓ Android emulator is ready!"
break
fi
sleep 2
counter=$((counter + 2))
echo " ...waiting for emulator (${counter}s/${timeout}s)"
done
if [ $counter -ge $timeout ]; then
echo " ✗ Timeout waiting for emulator to boot"
exit 1
fi
# Wait for boot completion
adb wait-for-device
echo " Waiting for boot to complete..."
adb shell 'while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done; echo "Boot completed"'
echo " ✓ Android boot completed!"
echo "[3/5] Configuring network..."
# Get the IP that the Android emulator can use to reach the backend
# In Docker bridge network, we can use the service name
echo " Backend host: ${BACKEND_HOST}:${BACKEND_PORT}"
# Push configuration to Android
# Create a config file that the app can read
adb shell "mkdir -p /data/local/tmp/chunyu"
adb shell "echo '${BACKEND_HOST}' > /data/local/tmp/chunyu/backend_host"
adb shell "echo '${BACKEND_PORT}' > /data/local/tmp/chunyu/backend_port"
echo "[4/5] Installing APK..."
if [ -f "$APK_PATH" ]; then
echo " Installing: $APK_PATH"
adb install -r -t "$APK_PATH"
echo " ✓ APK installed successfully!"
else
echo " ⚠ APK not found at $APK_PATH"
echo " Please mount the APK to /app/apks/app-debug.apk"
fi
echo "[5/5] Launching application..."
adb shell am start -n "$LAUNCH_ACTIVITY"
echo " ✓ Application launched!"
echo ""
echo "=========================================="
echo " Android Emulator Ready!"
echo "=========================================="
echo " ADB Port: 5555"
echo " Backend: ${BACKEND_HOST}:${BACKEND_PORT}"
echo ""
echo " To connect from host:"
echo " adb connect localhost:5555"
echo "=========================================="
# Keep container running
wait $REDROID_PID