# packaging pipeline for onnxruntime-extensions Android AAR package

jobs:
  - job: AndroidPackaging
    pool:
      vmImage: "macOS-12"
    timeoutInMinutes: 120
    variables:
      buildConfig: Release
    steps:
      - task: UsePythonVersion@0
        inputs:
          versionSpec: "3.9"
          addToPath: true
          architecture: "x64"
        displayName: "Use Python 3.9"

      - task: JavaToolInstaller@0
        displayName: Use jdk 11
        inputs:
          versionSpec: "11"
          jdkArchitectureOption: "x64"
          jdkSourceOption: "PreInstalled"

      - script: brew install coreutils ninja
        displayName: Install coreutils and ninja

      - script: |
          python ./tools/gen_selectedops.py ./tools/android/package_ops.config
        displayName: "Generate selected ops CMake file"

      - template: templates/run-with-android-emulator-steps.yml
        parameters:
          steps:

          - bash: |
              set -e -x

              python ./tools/android/build_aar.py \
                --output-dir $(Build.BinariesDirectory)/android_aar \
                --config $(buildConfig) \
                --cmake-extra-defines OCOS_ENABLE_SELECTED_OPLIST=ON

              VERSION=$(cat ./version.txt)
              AAR_PATH="$(Build.BinariesDirectory)/android_aar/aar_out/$(buildConfig)/com/microsoft/onnxruntime/onnxruntime-extensions-android/${VERSION}/onnxruntime-extensions-android-${VERSION}.aar"

              echo "##vso[task.setvariable variable=ORT_EXTENSIONS_AAR_PATH]${AAR_PATH}"
            displayName: Build onnxruntime-extensions AAR package

          - bash: |
              set -e -x

              cp -r $(Build.SourcesDirectory)/java/src/test/android $(Build.BinariesDirectory)/android_test

              cd $(Build.BinariesDirectory)/android_test

              ./gradlew connectedDebugAndroidTest --no-daemon -DortExtensionsAarLocalPath="${ORT_EXTENSIONS_AAR_PATH}"
            displayName: Build and run onnxruntime-extensions Android test with Android Emulator

      - script: |
          set -e -x

          appcenter test run espresso \
            --app "AI-Frameworks/Ort-Ext-Mobile-Android-Testapp" \
            --devices $(app_center_android_test_devices) \
            --app-path $(Build.BinariesDirectory)/android_test/app/build/outputs/apk/debug/app-debug.apk \
            --test-series "master" \
            --locale "en_US" \
            --build-dir $(Build.BinariesDirectory)/android_test/app/build/outputs/apk/androidTest/debug \
            --token $(app_center_api_token)
        displayName: Run onnxruntime-extensions Android test using App Center

      - bash: |
          set -e -x

          AAR_DIR=$(dirname "${ORT_EXTENSIONS_AAR_PATH}")
          ARTIFACTS_STAGING_DIR="$(Build.ArtifactStagingDirectory)"

          cp ${ORT_EXTENSIONS_AAR_PATH} ${ARTIFACTS_STAGING_DIR}
          cp ${AAR_DIR}/*-javadoc.jar   ${ARTIFACTS_STAGING_DIR}
          cp ${AAR_DIR}/*-sources.jar   ${ARTIFACTS_STAGING_DIR}
          cp ${AAR_DIR}/*.pom           ${ARTIFACTS_STAGING_DIR}

        displayName: "Assemble artifacts"

      - publish: "$(Build.ArtifactStagingDirectory)"
        artifact: onnxruntime-extensions-android-aar
        displayName: "Publish artifacts"
