name: android

# Native Android (Kotlin/Compose/Media3) — M8 rewrite, now the only client.
# This workflow is testing only — lint + detekt + unit tests on every push
# to dev/main, plus a debug APK artifact for main. The signed-release
# build + asset attach + image-bundling lives in release.yml under a
# `needs:` chain so the docker image cannot ship without the APK.

on:
  push:
    branches: [main, dev]
    paths:
      - 'android/**'
      - '.gitea/workflows/android.yml'

# pull_request trigger intentionally omitted — see test-web.yml for
# the rationale (single-author repo, push covers PR-merge equivalent).
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  # Silences the JDK 22+ "restricted method in java.lang.System has been
  # called" warning that Gradle 9.1's bundled native-platform jar trips
  # at launch (System.load for native primitives). Affects the LAUNCHER
  # JVM, not the daemon — that's why org.gradle.jvmargs in
  # gradle.properties isn't enough. Future-compat: required opt-in once
  # JDK 25 promotes the warning to an error.
  JAVA_TOOL_OPTIONS: "--enable-native-access=ALL-UNNAMED"

jobs:
  build:
    name: Build + lint + test
    # Using flutter-ci runner label because it's the only proven-working
    # label with docker that can pull our container.image. Switch to
    # android-ci once the operator registers that runner label.
    runs-on: flutter-ci
    container:
      image: git.fabledsword.com/bvandeusen/ci-android:36

    defaults:
      run:
        working-directory: android

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Cache Gradle dirs
        # Resolved deps + Gradle distribution + Kotlin daemon caches.
        # Saves ~3 min per CI run after the first warm-up.
        uses: actions/cache@v4
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
            ~/.kotlin
          key: gradle-${{ runner.os }}-${{ hashFiles('android/gradle/wrapper/gradle-wrapper.properties', 'android/gradle/libs.versions.toml', 'android/**/*.gradle.kts') }}
          restore-keys: |
            gradle-${{ runner.os }}-

      - name: Make gradlew executable
        run: chmod +x ./gradlew

      - name: Gradle wrapper validation
        run: ./gradlew --version

      - name: ktlint
        run: ./gradlew ktlintCheck

      - name: detekt
        run: ./gradlew detekt

      - name: Unit tests
        run: ./gradlew testDebugUnitTest

      - name: Assemble debug
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
        run: ./gradlew assembleDebug

      - name: Upload debug APK
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
        # Gitea Actions runs in GHES-emulation mode; @actions/artifact v2+
        # (i.e. upload-artifact@v4+) errors with "GHESNotSupportedError".
        # Pin to @v3 until act_runner or the artifact backend catches up.
        uses: actions/upload-artifact@v3
        with:
          name: minstrel-android-debug-${{ github.sha }}
          path: android/app/build/outputs/apk/debug/app-debug.apk
