109 lines
3.9 KiB
Bash
Executable File
109 lines
3.9 KiB
Bash
Executable File
# Setup Android SDK environment and tools
|
|
_setup_android() {
|
|
[ -n "$ZSH_VERSION" ] && setopt localoptions nonomatch nullglob
|
|
|
|
_add_to_path() {
|
|
[ -d "$1" ] || return 0
|
|
case ":$PATH:" in
|
|
*":$1:"*) ;;
|
|
*) PATH="$1:$PATH"; export PATH ;;
|
|
esac
|
|
}
|
|
|
|
# Retrieve the latest versioned subdirectory using version sorting
|
|
_get_latest_subdir() {
|
|
[ -d "$1" ] || return 1
|
|
_latest=$(
|
|
for _d in "$1"/*; do
|
|
[ -d "$_d" ] && basename "$_d"
|
|
done | { sort -V 2>/dev/null || sort -n 2>/dev/null || sort; } | tail -n 1
|
|
)
|
|
if [ -n "$_latest" ] && [ -d "$1/$_latest" ]; then
|
|
echo "$1/$_latest"
|
|
return 0
|
|
fi
|
|
return 1
|
|
}
|
|
|
|
# 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
|
|
)
|
|
|
|
_found_sdk=""
|
|
for _dir in "${_sdk_candidates[@]}"; do
|
|
if [ -n "$_dir" ] && [ -d "$_dir" ]; then
|
|
_found_sdk="$_dir"
|
|
break
|
|
fi
|
|
done
|
|
|
|
if [ -z "$_found_sdk" ]; then
|
|
return 0
|
|
fi
|
|
|
|
# Set SDK environment variables
|
|
export ANDROID_HOME="$_found_sdk"
|
|
export ANDROID_SDK_ROOT="$_found_sdk"
|
|
|
|
# Find the most recent build-tools
|
|
_build_tools_dir=""
|
|
if [ -d "$_found_sdk/build-tools" ]; then
|
|
_build_tools_dir=$(_get_latest_subdir "$_found_sdk/build-tools")
|
|
fi
|
|
|
|
# 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
|
|
|
|
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"
|
|
}
|
|
_setup_android
|
|
unset -f _setup_android _add_to_path _get_latest_subdir
|