name: Example Android check
on:
  pull_request:
    paths:
      - .github/workflows/android-build-test.yml
      - 'android/**'
      - 'src/fabric/**'
      - 'package.json'
      - 'apps/paper-example/android/**'
      - 'apps/paper-example/package.json'
      - 'apps/fabric-example/android/**'
      - 'apps/fabric-example/package.json'
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        working-directory: [paper-example, fabric-example]
      fail-fast: false
    concurrency:
      group: android-${{ matrix.working-directory }}-${{ github.ref }}
      cancel-in-progress: true
    steps:
      - name: Checkout Git repository
        uses: actions/checkout@v4

      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: '17'

      - name: Get react-native-svg node_modules cache
        uses: actions/cache@v4
        with:
          path: node_modules
          key: ${{ runner.os }}-node-modules-svg-${{ hashFiles('yarn.lock') }}
          restore-keys: ${{ runner.os }}-node-modules-svg-

      - name: Install react-native-svg node_modules
        run: yarn install --frozen-lockfile

      - name: Get app node_modules cache
        uses: actions/cache@v4
        with:
          path: apps/${{ matrix.working-directory }}/node_modules
          key: ${{ runner.os }}-node-modules-${{ matrix.working-directory }}-${{ hashFiles(format('{0}/yarn.lock', matrix.working-directory)) }}
          restore-keys: ${{ runner.os }}-node-modules-${{ matrix.working-directory }}-

      - name: Install app node_modules
        working-directory: apps/${{ matrix.working-directory }}
        run: yarn install --frozen-lockfile

      - name: Get build cache
        uses: actions/cache@v4
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-${{matrix.working-directory}}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
          restore-keys: |
            ${{ runner.os }}-gradle-${{matrix.working-directory}}-

      - name: Build app
        working-directory: apps/${{ matrix.working-directory }}/android
        run: ./gradlew assembleDebug --build-cache --console=plain -PreactNativeArchitectures=arm64-v8a
