name: CI (android)

on:
  workflow_dispatch: # allows manual triggering
  push:
    branches:
      - master
    paths: ['.github/workflows/build-android.yml',
            '**/CMakeLists.txt',
            '**/*.h',
            '**/*.hpp',
            '**/*.c',
            '**/*.cpp',
            '**/*.java']

  pull_request:
    types: [opened, synchronize, reopened]
    paths-ignore:
      - 'bindings/ruby/**'        # handled by bindings-ruby.yml
      - 'bindings/go/**'          # handled by bindings-go.yml
      - 'examples/addon.node/**'  # handled by examples.yml

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref && github.ref || github.run_id }}
  cancel-in-progress: true

jobs:
  android:
    runs-on: ubuntu-22.04

    steps:
      - name: Clone
        uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6
        with:
          path: whisper

      - name: Install Java
        uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5
        with:
          distribution: zulu
          java-version: 21

      - name: Setup Android SDK
        uses: android-actions/setup-android@40fd30fb8d7440372e1316f5d1809ec01dcd3699 # v4.0.1

      - name: Build
        run: |
          cd whisper/examples/whisper.android
          ./gradlew assembleRelease --no-daemon

      - name: Build with external ggml
        run: |
          export PATH_TO_GGML=$PWD/ggml
          cd whisper/examples/whisper.android
          ./gradlew assembleRelease --no-daemon

  android_java:
    runs-on: ubuntu-22.04

    steps:
      - name: Clone
        uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6

      - name: set up JDK 11
        uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5
        with:
          java-version: '11'
          distribution: 'temurin'
          cache: gradle

      - name: Setup Android SDK
        uses: android-actions/setup-android@40fd30fb8d7440372e1316f5d1809ec01dcd3699 # v4.0.1
        with:
          cmdline-tools-version: 9.0

      - name: Build
        run: |
          cd examples/whisper.android.java
          chmod +x ./gradlew
          ./gradlew assembleRelease
