Skip to content

Commit 555b802

Browse files
authored
Check if the locally installed Android NDK matches the wanted NDK version (#235)
* Check if the locally installed Android NDK matches the configured NDK version We were never actually checking `android_ndk_versions` if ANDROID_NDK_HOME was set, as that environment variable always will be on the github runners. Also, since GitHub often has other NDKs installed too, check if a matching one is installed and use it, plus add some log output so we can verify all this easily from now on. * [CI] Add back Android flag needed to build it, and build against NDK 28c instead
1 parent c6fcbaf commit 555b802

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

.github/workflows/pull_request.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ jobs:
5252
# Android
5353
android_sdk_pre_build_command: |
5454
cd tests/TestPackage
55+
enable_android_sdk_build: true
5556
enable_android_sdk_checks: true
57+
android_ndk_versions: "[\"r28c\"]"
5658
# Windows
5759
windows_build_command: |
5860
cd tests/TestPackage

.github/workflows/scripts/install-and-build-with-sdk.sh

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,16 @@ ANDROID_SDK_DOWNLOAD_ROOT="${SWIFT_DOWNLOAD_ROOT}/${SWIFT_VERSION_BRANCH}/androi
627627
STATIC_LINUX_SDK_DOWNLOAD_ROOT="${SWIFT_DOWNLOAD_ROOT}/${SWIFT_VERSION_BRANCH}/static-sdk"
628628
WASM_SDK_DOWNLOAD_ROOT="${SWIFT_DOWNLOAD_ROOT}/${SWIFT_VERSION_BRANCH}/wasm-sdk"
629629

630+
install_android_ndk() {
631+
local ndk_version="$1"
632+
log "Installing Android NDK: $ndk_version"
633+
curl_with_retry -fsSL -o ndk.zip https://dl.google.com/android/repository/android-ndk-"${ndk_version}"-"$(uname -s)".zip
634+
command -v unzip >/dev/null || install_package unzip
635+
unzip -q ndk.zip
636+
rm ndk.zip
637+
export ANDROID_NDK_HOME="${PWD}"/android-ndk-"${ndk_version}"
638+
}
639+
630640
install_android_sdk() {
631641
# Check if the Android Swift SDK is already installed
632642
if "$SWIFT_EXECUTABLE_FOR_ANDROID_SDK" sdk list 2>/dev/null | grep -q "^${ANDROID_SDK_TAG}_android"; then
@@ -653,18 +663,28 @@ install_android_sdk() {
653663
# guess some common places where the swift-sdks file lives
654664
cd ~/Library/org.swift.swiftpm || cd ~/.config/swiftpm || cd ~/.local/swiftpm || cd ~/.swiftpm || cd /root/.swiftpm
655665

666+
# permit the "--android-ndk" flag to override the default
667+
local android_ndk_version="${ANDROID_NDK_VERSION:-r27d}"
668+
log "Checking for Android NDK $android_ndk_version at $ANDROID_NDK_HOME"
669+
656670
# Download and install the Android NDK.
657671
# Note that we could use the system package manager, but it is
658672
# named different things for different distributions
659673
# (e.g., "google-android-ndk-r26-installer" on Debian)
660674
if [[ ! -d "${ANDROID_NDK_HOME:-}" ]]; then
661-
# permit the "--android-ndk" flag to override the default
662-
local android_ndk_version="${ANDROID_NDK_VERSION:-r27d}"
663-
curl_with_retry -fsSL -o ndk.zip https://dl.google.com/android/repository/android-ndk-"${android_ndk_version}"-"$(uname -s)".zip
664-
command -v unzip >/dev/null || install_package unzip
665-
unzip -q ndk.zip
666-
rm ndk.zip
667-
export ANDROID_NDK_HOME="${PWD}"/android-ndk-"${android_ndk_version}"
675+
install_android_ndk "$android_ndk_version"
676+
elif ! grep -q "Pkg.ReleaseName = ${android_ndk_version}" "${ANDROID_NDK_HOME}/source.properties"; then
677+
log "Android NDK $android_ndk_version did not match $(grep "Pkg.ReleaseName" "$ANDROID_NDK_HOME/source.properties")"
678+
# Check if the correct NDK is already cached in the same directory, as
679+
# it often is on GitHub runners, and use it if so
680+
ndk_release=$(echo "$android_ndk_version" | tr -d "[:alpha:]")
681+
try_ndk_path=$(find "$(dirname "${ANDROID_NDK_HOME}")" -name "${ndk_release}*" -maxdepth 1)
682+
if [[ -d "${try_ndk_path}" ]] && grep -q "Pkg.ReleaseName = ${android_ndk_version}" "${try_ndk_path}/source.properties"; then
683+
log "Found a matching Android NDK $android_ndk_version at $try_ndk_path instead"
684+
export ANDROID_NDK_HOME="$try_ndk_path"
685+
else
686+
install_android_ndk "$android_ndk_version"
687+
fi
668688
fi
669689

670690
./swift-sdks/"${android_sdk_bundle_name}"/swift-android/scripts/setup-android-sdk.sh

0 commit comments

Comments
 (0)