Files
mtgc/scripts/android-env.sh
Garrett Mills 17c927afa3 Add a fish version of the Android env helper
Only the env helper needs a per-shell variant, since it has to mutate the
calling shell; android-setup.sh is executed rather than sourced and already
works from fish.

The fish version finds NDK/JDK candidates via `find` rather than globs because
fish raises an error on a wildcard matching nothing, and "SDK with no ndk/
directory yet" is a normal state the helper has to report cleanly.

Also switch the bash helper's NDK selection to `find | sort -V` so the two
agree: glob order is lexicographic, which would rank 9.x above 10.x. Verified
both pick the same NDK and JDK on this machine and against a fixture with
9.0.111 / 10.0.222 / 28.2.333 / 30.0.999-beta2 installed.
2026-08-02 13:22:36 -05:00

71 lines
2.3 KiB
Bash
Executable File

#!/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/AGP reject very new JDKs; prefer Studio's bundled JBR 21 ---
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 \
/usr/lib/jvm/java-21-openjdk /usr/lib/jvm/java-21-openjdk-* \
/usr/lib/jvm/java-17-openjdk /usr/lib/jvm/java-17-openjdk-*
do
if [ -x "$d/bin/javac" ]; then jdk="$d"; break; fi
done
if [ -z "$jdk" ]; then
echo "android-env: no JDK 21/17 found (Gradle rejects the system default)" >&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
unset -f _mtgc_android_env