build-release.sh 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #!/usr/bin/env bash
  2. #
  3. # A build script for the release version of the Threema Android app.
  4. # _____ _
  5. # |_ _| |_ _ _ ___ ___ _ __ __ _
  6. # | | | ' \| '_/ -_) -_) ' \/ _` |_
  7. # |_| |_||_|_| \___\___|_|_|_\__,_(_)
  8. #
  9. # Threema for Android
  10. # Copyright (c) 2020 Threema GmbH
  11. #
  12. # This program is free software: you can redistribute it and/or modify
  13. # it under the terms of the GNU Affero General Public License, version 3,
  14. # as published by the Free Software Foundation.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU Affero General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Affero General Public License
  22. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. set -euo pipefail
  24. DOCKERIMAGE=threema/android-compile
  25. GREEN="\033[0;32m"
  26. RED="\033[0;31m"
  27. RESET="\033[0m"
  28. function print_usage() {
  29. echo "Usage: $0 -v <variants> [-b] [-k <dir>] [-o <dir>] [--no-image-export] --i-accept-the-android-sdk-license"
  30. echo ""
  31. echo "Options:"
  32. echo " -v <variants> Comma-separated variants to build: googleplay_private, googleplay_work, googleplay_onprem, hms_private, hms_work, threemashop_private, libre_private"
  33. echo " -b,--build (Re)build the Docker image"
  34. echo " --no-cache Clear Docker build cache"
  35. echo " -k,--keystore <dir> Path to the keystore directory"
  36. echo " -o,--outdir <dir> Path to the release output directory, will be created if it doesn't exist"
  37. echo " --no-image-export Skip the docker image export step"
  38. echo " -h,--help Print this help and exit"
  39. }
  40. function log() {
  41. echo -en "$1"
  42. echo -n "$2 $3"
  43. echo -e "$RESET"
  44. }
  45. function log_major() { log "$GREEN" "==>" "$1"; }
  46. function log_minor() { log "$GREEN" "--> " "$1"; }
  47. function log_error() { log "$RED" "!!!" "Error: $1"; }
  48. function fail() {
  49. log_error "$1"
  50. exit 1
  51. }
  52. # Re-implementation of realpath function (hello macOS)
  53. function realpath() {
  54. OURPWD=$PWD
  55. cd "$(dirname "$1")"
  56. LINK=$(readlink "$(basename "$1")")
  57. while [ "$LINK" ]; do
  58. cd "$(dirname "$LINK")"
  59. LINK=$(readlink "$(basename "$1")")
  60. done
  61. REALPATH="$PWD/$(basename "$1")"
  62. cd "$OURPWD"
  63. echo "$REALPATH"
  64. }
  65. # Determine script directory
  66. DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
  67. # If no arguments are passed, print usage
  68. if [ "$#" -lt 1 ]; then print_usage; exit 1; fi
  69. # Parse arguments
  70. license=""
  71. variants=""
  72. build=0
  73. no_cache=""
  74. keystore=""
  75. export_image=1
  76. releasedir="$DIR/../release"
  77. while [[ "$#" -gt 0 ]]; do
  78. case $1 in
  79. -v) variants="$2"; shift ;;
  80. -n) echo "Note: The -n parameter is deprecated and not needed anymore"; shift ;;
  81. -b|--build) build=1 ;;
  82. --no-cache) no_cache="--no-cache" ;;
  83. -k|--keystore) keystore="$2"; shift ;;
  84. -o|--outdir) releasedir="$2"; shift ;;
  85. --i-accept-the-android-sdk-license) license="accepted" ;;
  86. --no-image-export) export_image=0 ;;
  87. -h|--help) print_usage; exit 0 ;;
  88. *) echo "Unknown parameter passed: $1"; print_usage; exit 1 ;;
  89. esac
  90. shift
  91. done
  92. releasedir=$(realpath "$releasedir")
  93. # Process arguments
  94. IFS=', ' read -r -a variant_array <<< "$variants"
  95. for variant in "${variant_array[@]}"; do
  96. case $variant in
  97. googleplay_private | googleplay_work | googleplay_onprem | hms_private | hms_work | threemashop_private | libre_private)
  98. # Valid
  99. ;;
  100. *)
  101. fail "Invalid build variant: $variant"
  102. ;;
  103. esac
  104. done
  105. if [ "$license" != "accepted" ]; then
  106. fail 'Please accept the license with "--i-accept-the-android-sdk-license"'
  107. fi
  108. # Determine build version and full name in the form of: "1.1[-beta1]-1000"
  109. APP_BUILD_GRADLE_FILE="$DIR/../app/build.gradle.kts"
  110. APP_VERSION_CODE=$(grep "^\s*val defaultVersionCode = \d*" "$APP_BUILD_GRADLE_FILE" | sed 's/[^0-9]*//g')
  111. APP_VERSION_NAME_MAIN=$(grep '^val appVersion = "' "$APP_BUILD_GRADLE_FILE" | sed 's/^val appVersion = "\([^"]*\)".*/\1/')
  112. APP_VERSION_NAME_SUFFIX=$(grep '^val betaSuffix = "' "$APP_BUILD_GRADLE_FILE" | sed 's/^val betaSuffix = "\([^"]*\)".*/\1/')
  113. FULL_APP_VERSION_NAME="${APP_VERSION_NAME_MAIN}${APP_VERSION_NAME_SUFFIX}-${APP_VERSION_CODE}"
  114. sdk_version=$(grep "^\s*compileSdk = [0-9]\+" "$APP_BUILD_GRADLE_FILE" | sed 's/[^0-9]*//g')
  115. build_tools_version=$(grep "^\s*buildToolsVersion = \"\([0-9]\+\.\?\)\+\"" "$APP_BUILD_GRADLE_FILE" | sed 's/[^0-9\.]*//g')
  116. rust_version=$(grep channel "$DIR/../domain/libthreema/rust-toolchain.toml" | cut -d'"' -f2)
  117. # Validate target directory
  118. mkdir -p "$releasedir"
  119. name=${FULL_APP_VERSION_NAME//[^0-9\.a-zA-Z\-_]/}
  120. if [[ "$name" == "" || "$name" == .* ]]; then
  121. fail "Could not process app version name ($FULL_APP_VERSION_NAME)"
  122. fi
  123. targetdir="$releasedir/$name"
  124. log_major "Creating target directory $targetdir"
  125. if [[ -d "$targetdir" ]]; then
  126. fail "Output directory $targetdir already exists. Please remove it first."
  127. fi
  128. mkdir "$targetdir"
  129. # Build Docker image
  130. if [ $build -eq 1 ]; then
  131. log_major "Building Docker image with args:"
  132. log_minor "app_version_code=$APP_VERSION_CODE"
  133. log_minor "app_version_name=$FULL_APP_VERSION_NAME"
  134. log_minor "sdk_version=$sdk_version"
  135. log_minor "build_tools_version=$build_tools_version"
  136. log_minor "rust_version=$rust_version"
  137. docker build $no_cache "$DIR/../scripts/" \
  138. --build-arg SDK_VERSION="$sdk_version" \
  139. --build-arg BUILD_TOOLS_VERSION="$build_tools_version" \
  140. --build-arg RUST_VERSION="$rust_version" \
  141. -t "$DOCKERIMAGE:latest" \
  142. -t "$DOCKERIMAGE:$APP_VERSION_CODE"
  143. fi
  144. # Build app variant(s)
  145. for variant in "${variant_array[@]}"; do
  146. # Determine target and path
  147. case $variant in
  148. googleplay_private)
  149. target=assembleStore_googleRelease
  150. variant_dir="store_google"
  151. ;;
  152. googleplay_work)
  153. target=assembleStore_google_workRelease
  154. variant_dir="store_google_work"
  155. ;;
  156. googleplay_onprem)
  157. target=assembleOnpremRelease
  158. variant_dir="onprem"
  159. ;;
  160. hms_private)
  161. target=assembleHmsRelease
  162. variant_dir="hms"
  163. ;;
  164. hms_work)
  165. target=assembleHms_workRelease
  166. variant_dir="hms_work"
  167. ;;
  168. threemashop_private)
  169. target=assembleStore_threemaRelease
  170. variant_dir="store_threema"
  171. ;;
  172. libre_private)
  173. target=assembleLibreRelease
  174. variant_dir="libre"
  175. ;;
  176. *)
  177. fail "Invalid build variant: $variant"
  178. ;;
  179. esac
  180. # Compile
  181. log_major "Building gradle target $target"
  182. run_command="docker run --rm"
  183. if [ "${CI:-}" = "true" ]; then
  184. log_minor "CI detected, running Docker commands non-interactively"
  185. else
  186. run_command+=" -ti"
  187. fi
  188. run_command+=" -u \"$(id -u):$(id -g)\""
  189. run_command+=" -v \"$DIR/..\":/code"
  190. run_command+=" -v /dev/null:/code/local.properties" # Mask local.properties file
  191. run_command+=" -v /code/build/" # Mask root build directory
  192. run_command+=" -v /code/app/.cxx/" # Mask ndk build directory
  193. if [ "$keystore" != "" ]; then
  194. log_minor "Using keystore at $keystore"
  195. keystore_realpath=$(realpath "$keystore")
  196. run_command+=" -v \"$keystore_realpath:/keystore\""
  197. fi
  198. run_command+=" \"$DOCKERIMAGE:$APP_VERSION_CODE\""
  199. run_command+=" /bin/bash -c \"cd /code && ./gradlew clean -PbuildUniversalApk $target\""
  200. eval "$run_command"
  201. # Copy files
  202. log_major "Copying generated files for variant $variant"
  203. mkdir -p "$targetdir/$variant/"{logs,mapping}/
  204. for f in "$DIR"/../app/build/outputs/apk/"$variant_dir"/release/*; do
  205. log_minor "$(basename "$f")"
  206. cp -r "$f" "$targetdir/$variant/"
  207. done
  208. for f in "$DIR"/../app/build/outputs/logs/*"$variant_dir"*; do
  209. log_minor "$(basename "$f")"
  210. cp "$f" "$targetdir/$variant/logs/"
  211. done
  212. for f in "$DIR"/../app/build/outputs/mapping/"$variant_dir"Release/*; do
  213. log_minor "$(basename "$f")"
  214. cp "$f" "$targetdir/$variant/mapping/"
  215. done
  216. for f in "$DIR"/../app/build/outputs/native-debug-symbols/"$variant_dir"Release/native-debug-symbols.zip; do
  217. log_minor "$(basename "$f")"
  218. cp "$f" "$targetdir/$variant/mapping/"
  219. done
  220. for f in "$DIR"/../app/build/outputs/sdk-dependencies/"$variant_dir"Release/sdkDependencies.txt; do
  221. log_minor "$(basename "$f")"
  222. cp "$f" "$targetdir/$variant/"
  223. done
  224. done
  225. # Export image
  226. if [ $export_image -eq 1 ]; then
  227. log_major "Exporting docker image"
  228. docker image save -o "$targetdir/docker-image.tar" "$DOCKERIMAGE:$APP_VERSION_CODE"
  229. log_minor "Compressing docker image"
  230. gzip "${targetdir}/docker-image.tar"
  231. chmod 644 "${targetdir}/docker-image.tar.gz"
  232. fi
  233. log_major "Done! You can find the resulting files in the '$releasedir' directory."