#!/bin/bash bindir="$(dirname $0)" basedir="$(dirname $bindir)" # Check if GRADLE_VERSION_DIR/CACHEDIR is set from environment if [ -z "$GRADLE_VERSION_DIR" ]; then gradle_version_dir="${basedir}/versions" else gradle_version_dir="$GRADLE_VERSION_DIR" fi BUILDSERVER_CACHEDIR=/vagrant/cache if [ -n "$CACHEDIR" ]; then cachedir="$CACHEDIR" elif [ -d $BUILDSERVER_CACHEDIR ]; then cachedir=$BUILDSERVER_CACHEDIR fi args=("$@") checksums_url="https://gitlab.com/fdroid/gradle-transparency-log/-/raw/master/checksums.json" checksum_cache="$(mktemp -d)/gradle_checksums.json" curl -Lo $checksum_cache $checksums_url run_gradle() { if [ ! -d "${gradle_version_dir}/${v_found}" ]; then download_gradle ${v_found} fi # shellcheck disable=SC2145 echo "Running ${gradle_version_dir}/${v_found}/bin/gradle ${args[@]}" "${gradle_version_dir}/${v_found}/bin/gradle" "${args[@]}" exit $? } download_gradle() { URL="https://downloads.gradle.org/distributions/gradle-${1}-bin.zip" shasum=$(get_sha $1) if [ $? != 0 ]; then echo "No hash for gradle version $1! Exiting..." exit 1 fi if [ -n "${cachedir}" ] && [ -e "${cachedir}/gradle-$1-bin.zip" ]; then echo "Using cached ${cachedir}/gradle-$1-bin.zip ..." gradle_zip="${cachedir}/gradle-$1-bin.zip" else echo "Downloading missing gradle version $1" echo cachedir $cachedir if [[ -n "${cachedir}" && ! -d "${cachedir}" ]]; then mkdir -p "${cachedir}" fi if [[ -n "${cachedir}" && -d "${cachedir}" && -w "${cachedir}" ]]; then tmpdir="${cachedir}" else tmpdir=$(mktemp -d) fi curl -o "${tmpdir}/gradle-$1-bin.zip" --silent --fail --show-error --location --retry 3 --retry-all-errors "${URL}" gradle_zip="${tmpdir}/gradle-$1-bin.zip" fi echo "${shasum} ${gradle_zip}" | sha256sum -c - if [ $? != 0 ]; then echo "gradle download checksum mismatch! Exiting..." exit 1 fi mkdir -p "${gradle_version_dir}/" unzip -q -d "${gradle_version_dir}" "${gradle_zip}" mv "${gradle_version_dir}/gradle-$1" "${gradle_version_dir}/${v_found}" } get_sha() { sha=$(sed -n "/gradle-${1}-bin/,+2p" $checksum_cache | sed -n 's/.*"sha256": "\(.*\)"/\1/p') [[ -z $sha ]] && exit 1 echo $sha } contains() { local e for e in $2; do [[ $e == $1 ]] && return 0; done return 1 } # key-value pairs of what gradle version (value) each gradle plugin version # (key) should accept. plugin versions are actually prefixes and catch sub- # versions as well. Pairs are taken from: # https://developer.android.com/studio/releases/gradle-plugin#updating-gradle d_gradle_plugin_ver_k=(8.4 8.3 8.2 8.1 8.0 7.4 7.3 7.2.0 7.1 7.0 4.2 4.1 4.0 3.6 3.5 3.4 3.3 3.2 3.1 3.0 2.3 2.2 2.1.3 2.1 2.0) d_plugin_min_gradle_v=(8.6 8.4 8.2 8.0 8.0 7.5 7.4 7.3.3 7.2 7.0.2 6.7.1 6.5 6.1.1 5.6.4 5.4.1 5.1.1 4.10.1 4.6 4.4 4.1 3.3 2.14.1 2.14.1 2.12 2.12 2.4 2.4 2.3 2.2.1 2.2.1 2.1 2.1 1.12 1.12 1.12 1.11 1.10 1.9 1.8 1.6 1.6 1.4 1.4) # All gradle versions we know about plugin_v=(sed -n -E 's/.*gradle-([0-9.]+)-bin.*/\1/p' $checksum_cache) v_all=${plugin_v[@]} # Earliest file takes priority # Last key takes priority if there are duplicates (matching java.util.Properties) for f in {.,..}/gradle/wrapper/gradle-wrapper.properties; do [[ -f $f ]] || continue while IFS='' read -r line || [ -n "$line" ]; do line=$(printf "$line" | tr -d '\r') # strip Windows linefeeds if [[ $line == 'distributionUrl='* ]]; then wrapper_ver=${line#*/gradle-} wrapper_ver=${wrapper_ver%-*.zip} fi done < $f [[ -n $wrapper_ver ]] && break done if [[ -n $wrapper_ver ]]; then v_found=$wrapper_ver echo "Found $v_found via distributionUrl" run_gradle fi # Earliest takes priority for f in {.,..}/build.gradle{,.kts}; do [[ -f $f ]] || continue while IFS='' read -r line || [ -n "$line" ]; do line=$(printf "$line" | tr -d '\r') # strip Windows linefeeds if [[ -z "$plugin_pver" && $line == *'com.android.tools.build:gradle:'* ]]; then plugin_pver=${line#*[\'\"]com.android.tools.build:gradle:} plugin_pver=${plugin_pver%[\'\"]*} elif [[ -z "$wrapper_ver" && $line == *'gradleVersion = '* ]]; then wrapper_ver=${line#*gradleVersion*=*[\'\"]} wrapper_ver=${wrapper_ver%[\'\"]*} fi done < $f done if [[ -n $wrapper_ver ]]; then v_found=$wrapper_ver echo "Found $v_found via gradleVersion" run_gradle fi if [[ -n $plugin_pver ]]; then i=0 match=false for k in "${d_gradle_plugin_ver_k[@]}"; do if [[ $plugin_pver == ${k}* ]]; then plugin_ver=${d_plugin_min_gradle_v[$i]} match=true break fi let i++ done if $match; then v_found=$plugin_ver echo "Found $v_found via gradle plugin version $k" fi fi # Find the highest version available for v in ${plugin_v[*]}; do if contains $v "${v_all[*]}"; then v_def=$v break fi done if [[ -z $v_found ]]; then echo "No suitable gradle version found - defaulting to $v_def" v_found=$v_def fi run_gradle