language: android

jdk: oraclejdk8

sudo: required

dist: trusty

env:
  global:
    # switch glibc to a memory conserving mode
    - MALLOC_ARENA_MAX=2
    - ANDROID_TARGET=android-19
    - ANDROID_ABI=armeabi-v7a

if: type != push OR tag IS present

before_install:
  - openssl aes-256-cbc -K $encrypted_82adfa9c3806_key -iv $encrypted_82adfa9c3806_iv -in secrets.tar.enc -out secrets.tar -d
  - tar xvf secrets.tar

install:
  - pip install --user 'requests[security]'
  - wget -r -nH -nd -np -R index.html* robots.txt* http://download.kiwix.org/dev/android/api/licenses/ -e robots=off -P $ANDROID_HOME/licenses || true

addons:
  apt:
    packages:
      - lynx

before_cache:
  - rm -f  $HOME/.gradle/caches/modules-2/modules-2.lock
  - rm -fr $HOME/.gradle/caches/*/plugin-resolution/

cache:
  directories:
    - "$HOME/.gradle/caches/"
    - "$HOME/.gradle/wrapper/"
    - "$HOME/.android/build-cache"

android:
  components:
    - build-tools-28.0.3
    - android-28
    - extra-android-m2repository
    - $ANDROID_TARGET
    - sys-img-${ANDROID_ABI}-${ANDROID_TARGET}

licenses:
  - ".+"

before_script:
  - chmod +x gradlew
  - echo no | android create avd --force -n test -t $ANDROID_TARGET --abi $ANDROID_ABI -c 100M
  - emulator -avd test -no-audio -no-window &
  - android-wait-for-emulator
  - adb shell input keyevent 82 &

script:
  - ./gradlew createKiwixDebugCoverageReport lintKiwixDebug jacocoTestKiwixDebugUnitTestReport assemble

after_success:
  - bash <(curl -s https://codecov.io/bash)
  - ./gradlew kiwixtestUploadKiwix

after_failure:
  - export REPORT_DIR=${TRAVIS_HOME}/build/kiwix/kiwix-android/app/build/outputs/reports/
  - export LOG_DIR=${REPORT_DIR}androidTests/connected/flavors/KIWIX/
  - lynx -dump ${LOG_DIR}index.html
  - lynx -dump ${REPORT_DIR}lint-results-kiwixDebug.html


before_deploy:
  #  - export APP_CHANGELOG=$(cat app/src/kiwix/play/release-notes/en-US/default.txt)
  - export DATE=$(date +%Y-%m-%d)
  - export OUTPUT_DIR=app/build/outputs/apk/kiwix
  - export UNIVERSAL_RELEASE_APK=$OUTPUT_DIR/release/*universal*.apk
  - export UNIVERSAL_DEBUG_APK=$OUTPUT_DIR/debug/*universal*.apk
  - export SSH_KEY=travisci_builder_id_key
  - chmod 600 $SSH_KEY

deploy:

  #publish on github releases
  - provider: releases
    api_key: "$GITHUB_TOKEN"
    file: $UNIVERSAL_APK
    file_glob: true
    skip_cleanup: true
    overwrite: true
    #body: "$APP_CHANGELOG" broken because travis can't escape newlines https://github.com/travis-ci/dpl/issues/155
    draft: true
    on:
      tags: true

  #publish on play store
  - provider: script
    skip_cleanup: true
    script: ./gradlew publishKiwixRelease
    on:
      tags: true

  #publish release on download.kiwix.org
  - provider: script
    skip_cleanup: true
    script: mkdir $TRAVIS_TAG && cp $UNIVERSAL_RELEASE_APK $TRAVIS_TAG && scp -vrp -i ${SSH_KEY} -o StrictHostKeyChecking=no $TRAVIS_TAG ci@download.kiwix.org:/data/download/release/kiwix-android/
    on:
      tags: true

  #publish debug nightly on download.kiwix.org
  - provider: script
    skip_cleanup: true
    script: mkdir $DATE && cp $UNIVERSAL_DEBUG_APK $DATE && scp -vrp -i ${SSH_KEY} -o StrictHostKeyChecking=no $DATE ci@download.kiwix.org:/data/download/nightly/
    on:
      branch: develop
