Modernizes the android.sh script.
This commit is contained in:
153
.rc.d/android.sh
153
.rc.d/android.sh
@@ -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:"*) ;;
|
||||||
if [ -n "${ANDROID_HOME}" ]; then
|
*) PATH="$1:$PATH"; export PATH ;;
|
||||||
path=(${ANDROID_HOME}/cmdline-tools/latest/bin $path)
|
esac
|
||||||
# TODO: Find a way to get these versions as the latest versions in the directories.
|
|
||||||
path=(${ANDROID_HOME}/build-tools/34.0.0 $path)
|
|
||||||
path=(${ANDROID_HOME}/ndk/26.1.0909125 $path)
|
|
||||||
path=(${ANDROID_HOME}/platform-tools $path)
|
|
||||||
path=(${ANDROID_HOME}/tools $path)
|
|
||||||
path=(${ANDROID_HOME}/tools:/bin $path)
|
|
||||||
fi
|
|
||||||
|
|
||||||
# "extras;google;simulators" "ndk;27.0.11718014" "platform-tools" "platforms;android-34" "system-images;android-34;default;arm64-v8a"
|
|
||||||
|
|
||||||
function anon() {
|
|
||||||
local android_studio_sdk_home_on_mac="$HOME/Library/Android/sdk"
|
|
||||||
local android_home=
|
|
||||||
if [ -d "$android_studio_sdk_home_on_mac" ]; then
|
|
||||||
android_home="$android_studio_sdk_home_on_mac"
|
|
||||||
elif [ -d "$android_cmdline_tools_on_mac" ]; then
|
|
||||||
android_home="$android_cmdline_tools_on_mac"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -n "$android_home" ]; then
|
|
||||||
export ANDROID_HOME="$android_home"
|
|
||||||
if [ -d "$android_home/cmdline-tools/latest/bin" ]; then
|
|
||||||
path=("$android_home/cmdline-tools/latest/bin" $path)
|
|
||||||
fi
|
|
||||||
# TODO: Get the appropriate directories based on what is available.
|
|
||||||
path=(${ANDROID_HOME}/build-tools/34.0.0 $path)
|
|
||||||
path=(${ANDROID_HOME}/ndk/26.1.0909125 $path)
|
|
||||||
path=(${ANDROID_HOME}/platform-tools $path)
|
|
||||||
path=(${ANDROID_HOME}/tools $path)
|
|
||||||
path=(${ANDROID_HOME}/tools:/bin $path)
|
|
||||||
fi
|
|
||||||
|
|
||||||
# "extras;google;simulators" "ndk;27.0.11718014" "platform-tools" "platforms;android-34" "system-images;android-34;default;arm64-v8a"
|
|
||||||
|
|
||||||
unset -f anon
|
|
||||||
}
|
}
|
||||||
anon
|
|
||||||
|
# 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
|
||||||
|
|||||||
Reference in New Issue
Block a user