Skip to content

Commit 23a32b9

Browse files
Improve Android SDK build configuration
- Change default build command to use 'swift build' instead of 'swift build --build-system native' (the 4/27 snapshot contains the requisite changes for Android) - Use the correct host toolchain for Android build/test - Add mention of the specific host SDK being used to build For Wasm, we want to wait until swiftlang/swift-build#1348 lands before unpinning. Co-authored-by: Marc Prud'hommeaux <mwp1@cornell.edu>
1 parent a2de0e0 commit 23a32b9

3 files changed

Lines changed: 58 additions & 16 deletions

File tree

.github/workflows/scripts/android/android-emulator-tests.sh

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,18 @@ ANDROID_PROFILE="Nexus 10"
2121
ANDROID_EMULATOR_TIMEOUT=300
2222

2323
SWIFTPM_HOME="${XDG_CONFIG_HOME}"/swiftpm
24+
# Prefer the bundle name exported by install-and-build-with-sdk.sh so that we
25+
# pick the SDK matching the requested swift_version (release, snapshot, or main),
26+
# rather than whichever happens to sort last on disk.
2427
# e.g., "${SWIFTPM_HOME}"/swift-sdks/swift-DEVELOPMENT-SNAPSHOT-2025-12-11-a_android.artifactbundle/
25-
SWIFT_ANDROID_SDK_HOME=$(find "${SWIFTPM_HOME}"/swift-sdks -maxdepth 1 -name 'swift-*android.artifactbundle' | tail -n 1)
28+
if [[ -n "${SWIFT_ANDROID_SDK_BUNDLE:-}" ]]; then
29+
SWIFT_ANDROID_SDK_HOME="${SWIFTPM_HOME}/swift-sdks/${SWIFT_ANDROID_SDK_BUNDLE}"
30+
if [[ ! -d "${SWIFT_ANDROID_SDK_HOME}" ]]; then
31+
fatal "Android Swift SDK bundle not found at: ${SWIFT_ANDROID_SDK_HOME}"
32+
fi
33+
else
34+
SWIFT_ANDROID_SDK_HOME=$(find "${SWIFTPM_HOME}"/swift-sdks -maxdepth 1 -name 'swift-*android.artifactbundle' | tail -n 1)
35+
fi
2636

2737
ANDROID_SDK_TRIPLE="x86_64-unknown-linux-android28"
2838

@@ -122,10 +132,12 @@ STAGING_DIR="swift-android-test"
122132
rm -rf "${STAGING_DIR}"
123133
mkdir "${STAGING_DIR}"
124134

125-
BUILD_DIR=.build/"${ANDROID_SDK_TRIPLE}"/debug
135+
export PATH="$(dirname "$SWIFT_EXECUTABLE_FOR_ANDROID_SDK"):$PATH"
126136

127-
find "${BUILD_DIR}" -name '*.xctest' -exec cp -av {} "${STAGING_DIR}" \;
128-
find "${BUILD_DIR}" -name '*.resources' -exec cp -av {} "${STAGING_DIR}" \;
137+
SWIFT_SDK_ID=$(basename "${SWIFT_ANDROID_SDK_HOME}" .artifactbundle)
138+
BUILD_DIR=$(swift build --show-bin-path --swift-sdk "${SWIFT_SDK_ID}" --triple "${ANDROID_SDK_TRIPLE}")
139+
140+
find "${BUILD_DIR}" \( -name '*.xctest' -o -name '*.resources' -o -name '*-test-runner' -o -name '*.so' \) -exec cp -av {} "${STAGING_DIR}" \;
129141

130142
# copy over the required library dependencies
131143
cp -av "${SWIFT_ANDROID_SDK_HOME}"/swift-android/swift-resources/usr/lib/swift-"${ANDROID_EMULATOR_ARCH_TRIPLE}"/android/*.so "${STAGING_DIR}"
@@ -146,7 +158,10 @@ log "Copy Swift test package to emulator"
146158
ANDROID_TMP_FOLDER="/data/local/tmp/${STAGING_DIR}"
147159
adb push "${STAGING_DIR}" "${ANDROID_TMP_FOLDER}"
148160

149-
TEST_CMD="./*.xctest"
161+
TEST_CMD="./*-test-runner"
162+
if ! find "${STAGING_DIR}" -name '*-test-runner' -maxdepth 1 | grep -q .; then
163+
TEST_CMD="./*.xctest"
164+
fi
150165
TEST_SHELL="cd ${ANDROID_TMP_FOLDER}"
151166
TEST_SHELL="${TEST_SHELL} && ${TEST_CMD} --testing-library xctest"
152167

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,17 @@ if [[ "$INSTALL_ANDROID" == true ]]; then
621621
exit 0
622622
fi
623623
fi
624+
625+
# Export the resolved Android SDK tag so subsequent workflow steps
626+
# (e.g. android-emulator-tests.sh) pick the same SDK we just installed
627+
# rather than guessing via 'find ... | tail -n 1'
628+
if [[ -n "${GITHUB_ENV:-}" ]]; then
629+
{
630+
echo "SWIFT_EXECUTABLE_FOR_ANDROID_SDK=${SWIFT_EXECUTABLE_FOR_ANDROID_SDK}"
631+
echo "SWIFT_ANDROID_SDK_TAG=${ANDROID_SDK_TAG}"
632+
echo "SWIFT_ANDROID_SDK_BUNDLE=${ANDROID_SDK_TAG}_android.artifactbundle"
633+
} >> "$GITHUB_ENV"
634+
fi
624635
fi
625636

626637
if [[ "$INSTALL_STATIC_LINUX" == true ]]; then
@@ -687,8 +698,14 @@ install_android_sdk() {
687698

688699
rm -f "${sdk_url}"
689700

690-
# now setup the link to the local ANDROID_NDK_HOME
691-
swift sdk configure --show-configuration "$(swift sdk list | grep android | tail -n 1)"
701+
# now setup the link to the local ANDROID_NDK_HOME, using the matched
702+
# toolchain so its sdk subcommand and config files agree with what we just installed
703+
local installed_sdk_name
704+
installed_sdk_name=$("$SWIFT_EXECUTABLE_FOR_ANDROID_SDK" sdk list | grep "^${ANDROID_SDK_TAG}.*android" | tail -n 1)
705+
if [[ -z "$installed_sdk_name" ]]; then
706+
fatal "Could not find newly installed Android Swift SDK matching tag ${ANDROID_SDK_TAG}"
707+
fi
708+
"$SWIFT_EXECUTABLE_FOR_ANDROID_SDK" sdk configure --show-configuration "$installed_sdk_name"
692709

693710
# guess some common places where the swift-sdks file lives
694711
cd ~/Library/org.swift.swiftpm || cd ~/.config/swiftpm || cd ~/.local/swiftpm || cd ~/.swiftpm || cd /root/.swiftpm
@@ -717,7 +734,11 @@ install_android_sdk() {
717734
fi
718735
fi
719736

720-
./swift-sdks/"${android_sdk_bundle_name}"/swift-android/scripts/setup-android-sdk.sh
737+
# run the bundled setup script with the matched toolchain in front of PATH
738+
# so any 'swift' it invokes resolves to the version that matches the SDK
739+
local toolchain_bin_dir
740+
toolchain_bin_dir="$(dirname "$SWIFT_EXECUTABLE_FOR_ANDROID_SDK")"
741+
PATH="${toolchain_bin_dir}:${PATH}" ./swift-sdks/"${android_sdk_bundle_name}"/swift-android/scripts/setup-android-sdk.sh
721742
cd -
722743
}
723744

@@ -783,20 +804,24 @@ build() {
783804

784805
if [[ "$INSTALL_ANDROID" == true ]]; then
785806
log "Running Swift build with Android Swift SDK"
807+
log "Host toolchain for Android build: ${SWIFT_EXECUTABLE_FOR_ANDROID_SDK}"
808+
"$SWIFT_EXECUTABLE_FOR_ANDROID_SDK" --version || true
786809

787-
local sdk_name="${ANDROID_SDK_TAG}${ANDROID_SDK_PATH_SEP}android"
810+
local sdk_name="${ANDROID_SDK_TAG}_android"
788811

789812
alias swift='$SWIFT_EXECUTABLE_FOR_ANDROID_SDK'
790813

791814
# This can become a single invocation in the future when `swift build` supports multiple Android triples at once
792815
for android_sdk_triple in "${ANDROID_SDK_TRIPLES[@]}" ; do
793-
local build_command="$SWIFT_BUILD_COMMAND --swift-sdk ${android_sdk_triple}"
816+
local build_command="$SWIFT_BUILD_COMMAND --swift-sdk ${sdk_name} --triple ${android_sdk_triple}"
794817
if [[ -n "$SWIFT_BUILD_FLAGS" ]]; then
795818
build_command="$build_command $SWIFT_BUILD_FLAGS"
796819
fi
797820

798821
log "Running: $build_command"
799822

823+
export PATH="$(dirname "$SWIFT_EXECUTABLE_FOR_ANDROID_SDK"):$PATH"
824+
800825
if eval "$build_command"; then
801826
log "✅ Swift build with Android Swift SDK completed successfully"
802827
else

.github/workflows/swift_package_test.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,7 @@ on:
171171
android_sdk_build_command:
172172
type: string
173173
description: "Command to use when building the package with the Swift SDK for Android"
174-
# Temporarily use native build system on Android due to https://github.com/swiftlang/swift/issues/88282
175-
default: "swift build --build-system native"
174+
default: "swift build"
176175
android_sdk_triples:
177176
type: string
178177
description: "The triples to use when building with the Swift SDK for Android. The final triple in the list will be used for the emulator testing and should match the host architecture."
@@ -673,10 +672,13 @@ jobs:
673672
exclude:
674673
- ${{ fromJson(inputs.android_exclude_swift_versions) }}
675674
steps:
676-
- name: Swift version
677-
run: swift --version
678-
- name: Clang version
679-
run: clang --version
675+
# https://github.com/actions/runner-images/blob/main/images/ubuntu/scripts/build/install-swift.sh
676+
- name: Remove Swift
677+
run: |
678+
sudo rm /usr/local/bin/swift
679+
sudo rm /usr/local/bin/swiftc
680+
sudo rm /usr/local/lib/libsourcekitdInProc.so
681+
sudo rm -r /usr/share/swift
680682
- name: Checkout repository
681683
uses: actions/checkout@v4
682684
- name: Checkout swiftlang/github-workflows repository

0 commit comments

Comments
 (0)