# The goal of this action is to compile the Android debug build. That should
# tell us automatically if something got broken when a dependency was changed.

name: react-native-android-build-apk
on: [push, pull_request]

jobs:
  AssembleRelease:
    if: github.repository == 'laurent22/joplin'
    runs-on: ubuntu-latest
    steps:
      - name: Install Linux dependencies
        run: |
          sudo apt-get update || true
          sudo apt-get install -y libsecret-1-dev

      - uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '20'
          
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v6
        with:
          node-version: '24'
          cache: 'yarn'

      - name: Install Yarn
        run: |
          corepack enable

      - name: Install
        run:  yarn install
        env:
          SKIP_ONENOTE_CONVERTER_BUILD: 1
      
      - name: Free disk space
        run: |
          sudo rm -rf /usr/share/dotnet || true
          sudo rm -rf /opt/ghc || true

      - name: Assemble Android Release
        run: |
          cd packages/app-mobile/android
          sed -i -- 's/signingConfig signingConfigs.release/signingConfig signingConfigs.debug/' app/build.gradle
          ./gradlew assembleRelease
      
      - name: Verify alignment
        run: |
          cd packages/app-mobile/android/app
          APK_FILE="./build/outputs/apk/release/app-release.apk"
          if test ! -f "$APK_FILE" ; then
            echo "APK file not found."
            exit 1
          else
            echo "APK file found at: $APK_FILE"
          fi

          BUILD_TOOLS_PATH="$ANDROID_HOME/build-tools/"
          if test ! -d "$BUILD_TOOLS_PATH" ; then
            echo "Build tools not found at $BUILD_TOOLS_PATH ($ANDROID_HOME, $BUILD_TOOLS_VERSION)"
            exit 1
          fi
          # The build-tools/ directory contains different subdirectories
          # for each build tools version. As a result, there may be multiple
          # zipalign tools. Select the most recent (biggest two-digit version number):
          ZIPALIGN_PATH="$(find $BUILD_TOOLS_PATH -name "zipalign" -print | sort | tail -n1)"
          if test ! -x "$ZIPALIGN_PATH" ; then
            echo "zipalign not found (searching in $BUILD_TOOLS_PATH, candidate: $ZIPALIGN_PATH)"
            exit 1
          fi
          "$ZIPALIGN_PATH" -c -P 16 -v 4 "$APK_FILE"
