if(NOT "$ENV{KEYNAME}" STREQUAL ""
   AND NOT "$ENV{KEYPASS}" STREQUAL ""
   AND NOT "$ENV{STOREPASS}" STREQUAL "")
  execute_process(
    COMMAND
      "@ANDROIDDEPLOYQT_EXECUTABLE@"
      --sign @CMAKE_SOURCE_DIR@/keystore.p12 "$ENV{KEYNAME}"
      --storepass "$ENV{STOREPASS}"
      --keypass "$ENV{KEYPASS}"
      --input @CMAKE_BINARY_DIR@/src/app/android-qfield-deployment-settings.json
      --output @CMAKE_BINARY_DIR@/src/app/android-build
      --deployment bundled
      --android-platform android-@ANDROID_TARGET_PLATFORM@
      --gradle
      --release
      --aab
    WORKING_DIRECTORY @CMAKE_BINARY_DIR@ COMMAND_ECHO STDERR
    COMMAND_ERROR_IS_FATAL ANY)
  execute_process(
    COMMAND
      "@ANDROID_SDK_ROOT@/build-tools/@ANDROID_BUILD_TOOLS_VERSION@/apksigner" sign
      --v2-signing-enabled true
      --ks @CMAKE_SOURCE_DIR@/keystore.p12 --ks-key-alias "$ENV{KEYNAME}"
      --ks-pass "pass:$ENV{STOREPASS}"
      --key-pass "pass:$ENV{KEYPASS}"
        @CMAKE_BINARY_DIR@/src/app/android-build/build/outputs/apk/release/android-build-release-signed.apk
    WORKING_DIRECTORY @CMAKE_BINARY_DIR@ COMMAND_ECHO STDERR
    COMMAND_ERROR_IS_FATAL ANY)
else()
  execute_process(
    COMMAND
      "@ANDROIDDEPLOYQT_EXECUTABLE@"
      --input @CMAKE_BINARY_DIR@/src/app/android-qfield-deployment-settings.json
      --output @CMAKE_BINARY_DIR@/src/app/android-build
      --deployment bundled
      --android-platform android-@ANDROID_TARGET_PLATFORM@
      --gradle
    WORKING_DIRECTORY @CMAKE_BINARY_DIR@ COMMAND_ECHO STDERR
    COMMAND_ERROR_IS_FATAL ANY)
endif()
