stages:
  - build

variables:
  ANDROID_SDK_ROOT: "/root/Android/Sdk"
  ANDROID_HOME: "/root/Android/Sdk"
  GRADLE_USER_HOME: "$CI_PROJECT_DIR/.gradle"

image: ubuntu:20.04

before_script:
  # Installer Java, SDK tools et dépendances SANS
  - apt-get update -qq && apt-get install -qq -y openjdk-17-jdk wget unzip
  - wget https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip -O android-commandlinetools.zip
  - mkdir -p $ANDROID_SDK_ROOT/cmdline-tools
  - unzip android-commandlinetools.zip -d $ANDROID_SDK_ROOT/cmdline-tools
  - mv $ANDROID_SDK_ROOT/cmdline-tools/cmdline-tools $ANDROID_SDK_ROOT/cmdline-tools/tools
  - mkdir -p $ANDROID_SDK_ROOT/licenses
  # Accepter toutes les licences sans erreur bloquante
  - yes | $ANDROID_SDK_ROOT/cmdline-tools/tools/bin/sdkmanager --licenses || true
  - $ANDROID_SDK_ROOT/cmdline-tools/tools/bin/sdkmanager "platforms;android-30" "build-tools;30.0.3" "platforms;android-34" "build-tools;34.0.0"

build-apk:
  stage: build
  script:
    - echo "Téléchargement des dépendances..."
    - sed -i 's/\r$//' gradlew
    - chmod +x ./gradlew
    - ./gradlew dependencies
    - echo "Construction de l'APK..."
    - ./gradlew assembleDebug  # ou assembleRelease pour un APK de production
  artifacts:
    paths:
      - app/build/outputs/apk/debug/*.apk
  rules:
    - if: '$CI_COMMIT_BRANCH == "master" && $CI_COMMIT_MESSAGE =~ /build/'
