Skip to content

Commit a3aac61

Browse files
Improve Android SDK build configuration (#269)
- 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 a3aac61

3 files changed

Lines changed: 71 additions & 17 deletions

File tree

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

Lines changed: 22 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,14 @@ STAGING_DIR="swift-android-test"
122132
rm -rf "${STAGING_DIR}"
123133
mkdir "${STAGING_DIR}"
124134

125-
BUILD_DIR=.build/"${ANDROID_SDK_TRIPLE}"/debug
135+
_swift_bin_dir="$(dirname "$SWIFT_EXECUTABLE_FOR_ANDROID_SDK")"
136+
export PATH="$_swift_bin_dir:$PATH"
137+
unset _swift_bin_dir
126138

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

130144
# copy over the required library dependencies
131145
cp -av "${SWIFT_ANDROID_SDK_HOME}"/swift-android/swift-resources/usr/lib/swift-"${ANDROID_EMULATOR_ARCH_TRIPLE}"/android/*.so "${STAGING_DIR}"
@@ -146,7 +160,10 @@ log "Copy Swift test package to emulator"
146160
ANDROID_TMP_FOLDER="/data/local/tmp/${STAGING_DIR}"
147161
adb push "${STAGING_DIR}" "${ANDROID_TMP_FOLDER}"
148162

149-
TEST_CMD="./*.xctest"
163+
TEST_CMD="./*-test-runner"
164+
if ! find "${STAGING_DIR}" -name '*-test-runner' -maxdepth 1 | grep -q .; then
165+
TEST_CMD="./*.xctest"
166+
fi
150167
TEST_SHELL="cd ${ANDROID_TMP_FOLDER}"
151168
TEST_SHELL="${TEST_SHELL} && ${TEST_CMD} --testing-library xctest"
152169

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

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,6 @@ find_latest_sdk_snapshot() {
350350
SWIFT_VERSION_BRANCH=""
351351
ANDROID_SDK_TAG=""
352352
ANDROID_SDK_CHECKSUM=""
353-
ANDROID_SDK_PATH_SEP="-"
354353

355354
STATIC_LINUX_SDK_TAG=""
356355
STATIC_LINUX_SDK_CHECKSUM=""
@@ -621,6 +620,17 @@ if [[ "$INSTALL_ANDROID" == true ]]; then
621620
exit 0
622621
fi
623622
fi
623+
624+
# Export the resolved Android SDK tag so subsequent workflow steps
625+
# (e.g. android-emulator-tests.sh) pick the same SDK we just installed
626+
# rather than guessing via 'find ... | tail -n 1'
627+
if [[ -n "${GITHUB_ENV:-}" ]]; then
628+
{
629+
echo "SWIFT_EXECUTABLE_FOR_ANDROID_SDK=${SWIFT_EXECUTABLE_FOR_ANDROID_SDK}"
630+
echo "SWIFT_ANDROID_SDK_TAG=${ANDROID_SDK_TAG}"
631+
echo "SWIFT_ANDROID_SDK_BUNDLE=${ANDROID_SDK_TAG}_android.artifactbundle"
632+
} >> "$GITHUB_ENV"
633+
fi
624634
fi
625635

626636
if [[ "$INSTALL_STATIC_LINUX" == true ]]; then
@@ -687,8 +697,14 @@ install_android_sdk() {
687697

688698
rm -f "${sdk_url}"
689699

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

693709
# guess some common places where the swift-sdks file lives
694710
cd ~/Library/org.swift.swiftpm || cd ~/.config/swiftpm || cd ~/.local/swiftpm || cd ~/.swiftpm || cd /root/.swiftpm
@@ -717,7 +733,16 @@ install_android_sdk() {
717733
fi
718734
fi
719735

720-
./swift-sdks/"${android_sdk_bundle_name}"/swift-android/scripts/setup-android-sdk.sh
736+
# run the bundled setup script with the matched toolchain in front of PATH
737+
# so any 'swift' it invokes resolves to the version that matches the SDK
738+
local toolchain_bin_dir
739+
toolchain_bin_dir="$(dirname "$SWIFT_EXECUTABLE_FOR_ANDROID_SDK")"
740+
local setup_script="./swift-sdks/${android_sdk_bundle_name}/swift-android/scripts/setup-android-sdk.sh"
741+
if [[ -x "$setup_script" ]]; then
742+
PATH="${toolchain_bin_dir}:${PATH}" "$setup_script"
743+
else
744+
log "Skipping Android SDK setup script (not found): $setup_script"
745+
fi
721746
cd -
722747
}
723748

@@ -783,20 +808,30 @@ build() {
783808

784809
if [[ "$INSTALL_ANDROID" == true ]]; then
785810
log "Running Swift build with Android Swift SDK"
811+
log "Host toolchain for Android build: ${SWIFT_EXECUTABLE_FOR_ANDROID_SDK}"
812+
"$SWIFT_EXECUTABLE_FOR_ANDROID_SDK" --version || true
786813

787-
local sdk_name="${ANDROID_SDK_TAG}${ANDROID_SDK_PATH_SEP}android"
814+
local sdk_name="${ANDROID_SDK_TAG}_android"
788815

789816
alias swift='$SWIFT_EXECUTABLE_FOR_ANDROID_SDK'
790817

791818
# This can become a single invocation in the future when `swift build` supports multiple Android triples at once
792819
for android_sdk_triple in "${ANDROID_SDK_TRIPLES[@]}" ; do
793-
local build_command="$SWIFT_BUILD_COMMAND --swift-sdk ${android_sdk_triple}"
820+
if [[ "$SWIFT_VERSION_INPUT" == "6.3" || "$SWIFT_VERSION_INPUT" == "nightly-6.3" ]]; then
821+
local build_command="$SWIFT_BUILD_COMMAND --swift-sdk ${android_sdk_triple}"
822+
else
823+
local build_command="$SWIFT_BUILD_COMMAND --swift-sdk ${sdk_name} --triple ${android_sdk_triple}"
824+
fi
794825
if [[ -n "$SWIFT_BUILD_FLAGS" ]]; then
795826
build_command="$build_command $SWIFT_BUILD_FLAGS"
796827
fi
797828

798829
log "Running: $build_command"
799830

831+
local _swift_bin_dir
832+
_swift_bin_dir="$(dirname "$SWIFT_EXECUTABLE_FOR_ANDROID_SDK")"
833+
export PATH="$_swift_bin_dir:$PATH"
834+
800835
if eval "$build_command"; then
801836
log "✅ Swift build with Android Swift SDK completed successfully"
802837
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)