Skip to content

Commit d8ce023

Browse files
committed
Add enable_android_sdk_checks option for Android emulator testing
1 parent fe7a90a commit d8ce023

2 files changed

Lines changed: 87 additions & 4 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/bash
2+
##===----------------------------------------------------------------------===##
3+
##
4+
## This source file is part of the Swift.org open source project
5+
##
6+
## Copyright (c) 2025 Apple Inc. and the Swift project authors
7+
## Licensed under Apache License v2.0 with Runtime Library Exception
8+
##
9+
## See https://swift.org/LICENSE.txt for license information
10+
## See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
11+
##
12+
##===----------------------------------------------------------------------===##
13+
14+
set -euo pipefail
15+
16+
log() { printf -- "** %s\n" "$*" >&2; }
17+
error() { printf -- "** ERROR: %s\n" "$*" >&2; }
18+
fatal() { error "$@"; exit 1; }
19+
20+
ANDROID_API=28
21+
ANDROID_EMULATOR_ARCH="x86_64"
22+
EMULATOR_SPEC="system-images;android-${ANDROID_API};default;${ANDROID_EMULATOR_ARCH}"
23+
EMULATOR_NAME="swiftemu"
24+
ANDROID_PROFILE="Pixel 6"
25+
26+
# see if the Android SDK is even installed
27+
echo "CHECKING FOR ANDROID SDK"
28+
find / -name sdkmanager || true
29+
echo "DONE CHECKING FOR ANDROID SDK"
30+
31+
# install and start an Android emulator
32+
sdkmanager --list_installed
33+
yes | sdkmanager --licenses > /dev/null
34+
sdkmanager --install "${EMULATOR_SPEC}" "emulator" "platform-tools" "platforms;android-${ANDROID_API}"
35+
avdmanager create avd -n "${EMULATOR_NAME}" -k "${EMULATOR_SPEC}" --device "${ANDROID_PROFILE}"
36+
emulator -list-avds
37+
# launch the emulator in the background; we will cat the logs at the end
38+
nohup emulator -memory 4096 -avd "${EMULATOR_NAME}" -wipe-data -no-window -no-snapshot -noaudio -no-boot-anim 2>&1 > emulator.log &
39+
adb logcat 2>&1 > logcat.log &
40+
41+
# create a staging folder where we copy the test executable
42+
# and all the dependent libraries to copy over to the emulator
43+
STAGING="android-test-${PACKAGE}"
44+
rm -rf .build/"${STAGING}"
45+
mkdir .build/"${STAGING}"
46+
47+
# for the common case of tests referencing their own files as hardwired resource paths
48+
if [[ -d Tests ]]; then
49+
cp -a Tests .build/"${STAGING}"
50+
fi
51+
52+
cd .build/
53+
cp -a debug/*.xctest "${STAGING}"
54+
cp -a debug/*.resources "${STAGING}" || true
55+
cp -a ${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/*/sysroot/usr/lib/${ANDROID_EMULATOR_ARCH_TRIPLE}-linux-android/libc++_shared.so "${STAGING}"
56+
cp -a ${SWIFT_ANDROID_SDK_HOME}/swift-android/swift-resources/usr/lib/swift-${ANDROID_EMULATOR_ARCH_TRIPLE}/android/*.so "${STAGING}"
57+
58+
adb push ${STAGING} /data/local/tmp/
59+
60+
cd -
61+
62+
TEST_CMD="./${PACKAGE}PackageTests.xctest"
63+
TEST_SHELL="cd /data/local/tmp/${STAGING}"
64+
TEST_SHELL="${TEST_SHELL} && ${TEST_CMD}"
65+
66+
# Run test cases a second time with the Swift Testing library
67+
# We additionally need to handle the special exit code EXIT_NO_TESTS_FOUND (69 on Android),
68+
# which can happen when the tests link to Testing, but no tests are executed
69+
# see: https://github.com/swiftlang/swift-package-manager/blob/1b593469e8ad3daf2cc10e798340bd2de68c402d/Sources/Commands/SwiftTestCommand.swift#L1542
70+
TEST_SHELL="${TEST_SHELL} && ${TEST_CMD} --testing-library swift-testing && [ \$? -eq 0 ] || [ \$? -eq 69 ]"
71+
72+
# run the test executable
73+
adb shell "${TEST_SHELL}"
74+

.github/workflows/swift_package_test.yml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ on:
198198
type: boolean
199199
description: "Boolean to enable building with the Swift SDK for Android. Defaults to false"
200200
default: false
201+
enable_android_sdk_checks:
202+
type: boolean
203+
description: "Boolean to enable testing with the Swift SDK for Android. Defaults to false"
204+
default: false
201205
enable_macos_checks:
202206
type: boolean
203207
description: "Boolean to enable macOS testing. Defaults to false"
@@ -592,9 +596,9 @@ jobs:
592596
os_version: ${{ fromJson(inputs.linux_os_versions) }}
593597
exclude:
594598
- ${{ fromJson(inputs.android_exclude_swift_versions) }}
595-
- ${{ fromJson((!inputs.enable_android_sdk_build && inputs.android_sdk_versions) || '[]') }}
596-
- ${{ fromJson((!inputs.enable_android_sdk_build && inputs.android_ndk_versions) || '[]') }}
597-
- ${{ fromJson((!inputs.enable_android_sdk_build && inputs.linux_os_versions) || '[]') }}
599+
- ${{ fromJson((!(inputs.enable_android_sdk_build || inputs.enable_android_sdk_checks) && inputs.android_sdk_versions) || '[]') }}
600+
- ${{ fromJson((!(inputs.enable_android_sdk_build || inputs.enable_android_sdk_checks) && inputs.android_ndk_versions) || '[]') }}
601+
- ${{ fromJson((!(inputs.enable_android_sdk_build || inputs.enable_android_sdk_checks) && inputs.linux_os_versions) || '[]') }}
598602
container:
599603
image: ${{ (contains(matrix.swift_version, 'nightly') && 'swiftlang/swift') || 'swift' }}:${{ matrix.swift_version }}-${{ matrix.os_version }}
600604
steps:
@@ -642,9 +646,14 @@ jobs:
642646
done
643647
- name: Pre-build
644648
run: ${{ inputs.linux_pre_build_command }}
649+
# TODO: move this after Install Swift SDK for Android and build
650+
- name: Run Android tests on emulator
651+
if: ${{ inputs.enable_android_sdk_checks }}
652+
run: |
653+
${{ steps.script_path.outputs.root }}/.github/workflows/scripts/android/android-emulator-tests.sh --android-sdk-triple=${{ join(fromJson(inputs.android_sdk_triples), ' --android-sdk-triple=') }} --android-ndk-version="${{ matrix.ndk_version }}"
645654
- name: Install Swift SDK for Android and build
646655
env:
647-
BUILD_FLAGS: ${{ (contains(matrix.swift_version, 'nightly') && inputs.swift_nightly_flags) || inputs.swift_flags }}
656+
BUILD_FLAGS: ${{ (contains(matrix.swift_version, 'nightly') && inputs.swift_nightly_flags) || inputs.swift_flags }} ${{ inputs.enable_android_sdk_checks && "--build-tests" }}
648657
run: |
649658
${{ inputs.android_sdk_pre_build_command }}
650659
${{ steps.script_path.outputs.root }}/.github/workflows/scripts/install-and-build-with-sdk.sh --android --flags="$BUILD_FLAGS" --build-command="${{ inputs.android_sdk_build_command }}" --android-sdk-triple=${{ join(fromJson(inputs.android_sdk_triples), ' --android-sdk-triple=') }} --android-ndk-version="${{ matrix.ndk_version }}" ${{ matrix.swift_version }}

0 commit comments

Comments
 (0)