#!/bin/bash set -e # Configuration ANDROID_COMPILE_SDK="34" ANDROID_BUILD_TOOLS="34.0.0" CMDLINE_TOOLS_URL="https://dl.google.com/android/repository/commandlinetools-linux-10406996_latest.zip" CIQ_SDK_URL="https://developer.garmin.com/downloads/connect-iq/sdks/connectiq-sdk-linux-6.4.1-2023-12-13-176868848.zip" CIQ_DEVICE="fenix7spro" # Default device for build PROJECT_ROOT=$(pwd) TOOLS_DIR="$PROJECT_ROOT/_build_tools" ANDROID_SDK_ROOT="$TOOLS_DIR/android-sdk" CIQ_SDK_ROOT="$TOOLS_DIR/ciq-sdk" mkdir -p "$TOOLS_DIR" echo "=== FotoCompanion Build Setup ===" # 1. Check Java if ! command -v java &> /dev/null; then echo "Error: Java (JDK) not found. Please install JDK 17." exit 1 fi # 2. Setup Android SDK if [ ! -d "$ANDROID_SDK_ROOT/cmdline-tools" ]; then echo "Downloading Android Command Line Tools..." wget -q -O "$TOOLS_DIR/cmdline-tools.zip" "$CMDLINE_TOOLS_URL" mkdir -p "$ANDROID_SDK_ROOT/cmdline-tools/latest" unzip -q "$TOOLS_DIR/cmdline-tools.zip" -d "$TOOLS_DIR/cmdline-tools-temp" mv "$TOOLS_DIR/cmdline-tools-temp/cmdline-tools/"* "$ANDROID_SDK_ROOT/cmdline-tools/latest/" rm -rf "$TOOLS_DIR/cmdline-tools-temp" "$TOOLS_DIR/cmdline-tools.zip" fi export ANDROID_HOME="$ANDROID_SDK_ROOT" export PATH="$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$PATH" echo "Accepting Android Licenses..." yes | sdkmanager --licenses > /dev/null 2>&1 || true echo "Installing Android Platform Tools & SDK..." sdkmanager "platform-tools" "platforms;android-$ANDROID_COMPILE_SDK" "build-tools;$ANDROID_BUILD_TOOLS" > /dev/null # 3. Setup Garmin Connect IQ SDK if [ ! -d "$CIQ_SDK_ROOT" ]; then echo "Downloading Garmin Connect IQ SDK..." wget -q -O "$TOOLS_DIR/ciq-sdk.zip" "$CIQ_SDK_URL" mkdir -p "$CIQ_SDK_ROOT" unzip -q "$TOOLS_DIR/ciq-sdk.zip" -d "$CIQ_SDK_ROOT" rm "$TOOLS_DIR/ciq-sdk.zip" fi # 4. Copy CIQ Lib to Android Project # Note: The SDK download might not contain the mobile lib jar directly in root. # It's usually a separate download "Connect IQ Mobile SDK". # For automation, we might skip this if the user hasn't provided it, OR we download the mobile sdk too. # Let's try to download the Mobile SDK specifically for the JAR. MOBILE_SDK_URL="https://github.com/garmin/connectiq-mobile-sdk-android/releases/download/v1.5/connectiq-mobile-sdk-android-1.5.zip" # Note: GitHub URL is hypothetical/example. Garmin hosts them on their site behind login sometimes or direct links. # Actually, the JAR is often inside the main SDK or separately. # The user instruction in README says "Download manually". We will warn if missing. if [ ! -f "android-app/app/libs/connectiq.jar" ]; then echo "WARNING: connectiq.jar not found in android-app/app/libs/." echo "Please download the Connect IQ Mobile SDK for Android and copy connectiq.jar there." echo "Skipping Android Build." else echo "Building Android App..." cd android-app chmod +x gradlew ./gradlew assembleDebug cd .. echo "Android APK built: android-app/app/build/outputs/apk/debug/app-debug.apk" fi # 5. Build Garmin App echo "Building Garmin App..." MONKEYC="$CIQ_SDK_ROOT/bin/monkeyc" MONKEYDO="$CIQ_SDK_ROOT/bin/monkeydo" # Generate Developer Key if missing DEV_KEY="$TOOLS_DIR/developer_key.der" if [ ! -f "$DEV_KEY" ]; then echo "Generating Developer Key..." openssl genrsa -out "$TOOLS_DIR/developer_key.pem" 4096 openssl pkcs8 -topk8 -inform PEM -outform DER -in "$TOOLS_DIR/developer_key.pem" -out "$DEV_KEY" -nocrypt fi # Build "$MONKEYC" \ -o "garmin-app/bin/foto_companion.prg" \ -f "garmin-app/monkey.jungle" \ -y "$DEV_KEY" \ -d "$CIQ_DEVICE" \ -w echo "Garmin App built: garmin-app/bin/foto_companion.prg" echo "=== Done ==="