Files
mtgc/scripts/android-env.sh
Garrett Mills 1641d9aa47 Android: build on JDK 25 with Gradle 9.6.1, AGP 9.4.1, Kotlin 2.2.10
Android Studio now bundles JBR 25, which Gradle 8.14's embedded Kotlin
can't parse. Follow Tauri's current template: Gradle 9.6.1, Kotlin 2.2.10,
the builtInKotlin/newDsl/failOnMissingFiles compatibility flags, the
compilerOptions DSL, and ExecOperations in BuildTask (project.exec is gone
in Gradle 9). AGP goes to 9.4.1 rather than the template's 9.3.1.

Release lint vital is turned off: AGP 9.3/9.4's lint crashes analysing the
.gradle.kts scripts (findFirCompiledSymbol), failing every release build.

The env scripts now accept JDK 17 through 25, preferring Studio's JBR.
2026-10-16 22:39:36 -05:00

77 lines
2.6 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
#
# android-env.sh — export the environment the Android build needs.
#
# SOURCE this, don't execute it:
#
# source scripts/android-env.sh
# cargo tauri android dev --open
#
# Everything is auto-detected, so there are no machine-specific paths to keep in
# sync. Run ./scripts/android-setup.sh first to install the pieces.
_mtgc_android_env() {
local sdk ndk jdk
# --- SDK ---
sdk="${ANDROID_HOME:-$HOME/Android/Sdk}"
if [ ! -d "$sdk" ]; then
echo "android-env: no Android SDK at $sdk (run ./scripts/android-setup.sh)" >&2
return 1
fi
# --- NDK: newest *stable* installed (skip beta/rc/canary) ---
# `sort -V` so 9.x doesn't outrank 10.x the way plain lexicographic would.
local best=""
while IFS= read -r d; do
[ -n "$d" ] || continue
case "$(sed -n 's/^Pkg.Revision *= *//p' "$d/source.properties" 2>/dev/null)" in
*beta*|*rc*|*canary*) continue ;;
esac
best="$d"
done < <(find "$sdk/ndk" -mindepth 1 -maxdepth 1 -type d 2>/dev/null | sort -V)
ndk="$best"
if [ -z "$ndk" ]; then
echo "android-env: no stable NDK under $sdk/ndk (run ./scripts/android-setup.sh)" >&2
return 1
fi
# --- JDK: Gradle 9.6 / AGP 9 run on 17 through 25 (25 is the newest tested).
# Prefer Studio's bundled JBR, then the newest system JDK in that range.
for d in \
"$HOME/.local/share/JetBrains/Toolbox/apps/android-studio/jbr" \
"$HOME/.local/share/JetBrains/Toolbox/apps/android-studio"/*/jbr \
"$HOME/android-studio/jbr" \
/opt/android-studio/jbr \
$(ls -d /usr/lib/jvm/* 2>/dev/null | sort -rV)
do
[ -x "$d/bin/javac" ] || continue
local v
v="$(sed -n 's/^JAVA_VERSION="\([0-9]*\).*/\1/p' "$d/release" 2>/dev/null)"
if [ -n "$v" ] && [ "$v" -ge 17 ] && [ "$v" -le 25 ]; then jdk="$d"; break; fi
done
if [ -z "$jdk" ]; then
echo "android-env: no JDK 17–25 found (sudo dnf install java-25-openjdk-devel)" >&2
return 1
fi
export ANDROID_HOME="$sdk"
export NDK_HOME="$ndk"
export JAVA_HOME="$jdk"
# adb/emulator live here; harmless if already present.
case ":$PATH:" in
*":$sdk/platform-tools:"*) ;;
*) export PATH="$sdk/platform-tools:$PATH" ;;
esac
echo "ANDROID_HOME=$ANDROID_HOME"
echo "NDK_HOME=$NDK_HOME"
echo "JAVA_HOME=$JAVA_HOME"
}
_mtgc_android_env
_mtgc_rc=$?
unset -f _mtgc_android_env
# Pass the failure on so `set -e` scripts that source this stop here.
return "$_mtgc_rc" 2>/dev/null || exit "$_mtgc_rc"