Modernizes the android.sh script.

This commit is contained in:
2026-09-07 17:27:56 -07:00
parent 279e5f8aa3
commit ad5036564e

View File

@@ -1,49 +1,108 @@
# Android sdk # Setup Android SDK environment and tools
# ~/Library/Android/sdk/ _setup_android() {
if [ -d "${HOME}/Library/Android/sdk" ]; then [ -n "$ZSH_VERSION" ] && setopt localoptions nonomatch nullglob
export ANDROID_HOME="${HOME}/Library/Android/sdk"
fi
if [ -d "${HOME}/Applications/Android/cmdline-tools" ]; then _add_to_path() {
export ANDROID_HOME="${HOME}/Applications/Android/cmdline-tools" [ -d "$1" ] || return 0
fi case ":$PATH:" in
*":$1:"*) ;;
*) PATH="$1:$PATH"; export PATH ;;
esac
}
if [ -n "${ANDROID_HOME}" ]; then # Retrieve the latest versioned subdirectory using version sorting
path=(${ANDROID_HOME}/cmdline-tools/latest/bin $path) _get_latest_subdir() {
# TODO: Find a way to get these versions as the latest versions in the directories. [ -d "$1" ] || return 1
path=(${ANDROID_HOME}/build-tools/34.0.0 $path) _latest=$(
path=(${ANDROID_HOME}/ndk/26.1.0909125 $path) for _d in "$1"/*; do
path=(${ANDROID_HOME}/platform-tools $path) [ -d "$_d" ] && basename "$_d"
path=(${ANDROID_HOME}/tools $path) done | { sort -V 2>/dev/null || sort -n 2>/dev/null || sort; } | tail -n 1
path=(${ANDROID_HOME}/tools:/bin $path) )
fi if [ -n "$_latest" ] && [ -d "$1/$_latest" ]; then
echo "$1/$_latest"
return 0
fi
return 1
}
# "extras;google;simulators" "ndk;27.0.11718014" "platform-tools" "platforms;android-34" "system-images;android-34;default;arm64-v8a" # Common SDK search locations in priority order (macOS, Linux, WSL)
# The first directory found will be selected and configured.
_sdk_candidates=(
"${ANDROID_HOME:-}"
"${ANDROID_SDK_ROOT:-}"
"$HOME/Library/Android/sdk" # macOS Android Studio default
"$HOME/Android/Sdk" # Linux / WSL Android Studio default
"$HOME/android-sdk" # Common user manual install
"$HOME/.android/sdk"
"$HOME/Applications/Android/sdk"
"$HOME/Applications/Android/cmdline-tools"
"/usr/lib/android-sdk" # Linux distribution package (e.g. apt)
"/opt/android-sdk" # Linux system manual install
"/opt/android/sdk"
"/var/lib/android-sdk"
"/opt/homebrew/share/android-commandlinetools" # macOS Apple Silicon Homebrew
"/opt/homebrew/share/android-sdk"
"/usr/local/share/android-commandlinetools" # macOS Intel Homebrew
"/usr/local/share/android-sdk"
"/Library/Android/sdk" # macOS system-wide install
)
function anon() { _found_sdk=""
local android_studio_sdk_home_on_mac="$HOME/Library/Android/sdk" for _dir in "${_sdk_candidates[@]}"; do
local android_home= if [ -n "$_dir" ] && [ -d "$_dir" ]; then
if [ -d "$android_studio_sdk_home_on_mac" ]; then _found_sdk="$_dir"
android_home="$android_studio_sdk_home_on_mac" break
elif [ -d "$android_cmdline_tools_on_mac" ]; then fi
android_home="$android_cmdline_tools_on_mac" done
if [ -z "$_found_sdk" ]; then
return 0
fi fi
if [ -n "$android_home" ]; then # Set SDK environment variables
export ANDROID_HOME="$android_home" export ANDROID_HOME="$_found_sdk"
if [ -d "$android_home/cmdline-tools/latest/bin" ]; then export ANDROID_SDK_ROOT="$_found_sdk"
path=("$android_home/cmdline-tools/latest/bin" $path)
fi # Find the most recent build-tools
# TODO: Get the appropriate directories based on what is available. _build_tools_dir=""
path=(${ANDROID_HOME}/build-tools/34.0.0 $path) if [ -d "$_found_sdk/build-tools" ]; then
path=(${ANDROID_HOME}/ndk/26.1.0909125 $path) _build_tools_dir=$(_get_latest_subdir "$_found_sdk/build-tools")
path=(${ANDROID_HOME}/platform-tools $path)
path=(${ANDROID_HOME}/tools $path)
path=(${ANDROID_HOME}/tools:/bin $path)
fi fi
# "extras;google;simulators" "ndk;27.0.11718014" "platform-tools" "platforms;android-34" "system-images;android-34;default;arm64-v8a" # Find the most recent NDK
_ndk_dir=""
if [ -d "$_found_sdk/ndk" ]; then
_ndk_dir=$(_get_latest_subdir "$_found_sdk/ndk")
elif [ -d "$_found_sdk/ndk-bundle" ]; then
_ndk_dir="$_found_sdk/ndk-bundle"
fi
unset -f anon if [ -n "$_ndk_dir" ] && [ -d "$_ndk_dir" ]; then
export NDK_HOME="$_ndk_dir"
export ANDROID_NDK_HOME="$_ndk_dir"
fi
# Find cmdline-tools (prefer 'latest', then highest version, then root bin)
_cmdline_tools_bin=""
if [ -d "$_found_sdk/cmdline-tools/latest/bin" ]; then
_cmdline_tools_bin="$_found_sdk/cmdline-tools/latest/bin"
elif [ -d "$_found_sdk/cmdline-tools" ]; then
_latest_cmdline=$(_get_latest_subdir "$_found_sdk/cmdline-tools")
if [ -n "$_latest_cmdline" ] && [ -d "$_latest_cmdline/bin" ]; then
_cmdline_tools_bin="$_latest_cmdline/bin"
fi
elif [ -d "$_found_sdk/bin" ]; then
_cmdline_tools_bin="$_found_sdk/bin"
fi
# Prepend tools to PATH in reverse priority so most critical are at front
_add_to_path "$_found_sdk/tools"
_add_to_path "$_found_sdk/tools/bin"
[ -n "$_ndk_dir" ] && _add_to_path "$_ndk_dir"
[ -n "$_build_tools_dir" ] && _add_to_path "$_build_tools_dir"
_add_to_path "$_found_sdk/emulator"
_add_to_path "$_found_sdk/platform-tools"
[ -n "$_cmdline_tools_bin" ] && _add_to_path "$_cmdline_tools_bin"
} }
anon _setup_android
unset -f _setup_android _add_to_path _get_latest_subdir