Installing the Swift Android SDK
First install the matching Swift 6.2 toolchain from https://swift.org/download/#releases and add it to your PATH, or by using swiftly:
swiftly install 6.2
Then install the Swift Android SDK by running the command:
swift sdk install https://github.com/skiptools/swift-android-toolchain/releases/download/6.2/swift-6.2-RELEASE-android-0.1.artifactbundle.tar.gz --checksum 52cfebf048b8fe732403c98474908a3f237913cbba9e3fcef3f90ee732bf2cec
Installing the Android NDK
The Swift Android SDK requires the Android Native Development Toolkit (NDK) to function, which must be installed separately. Download and unzip the r27c LTS release and set the ANDROID_NDK_HOME environment variable to the local NDK installation and run the setup script.
macOS configuration command for android-ndk-r27c-darwin.zip:
ANDROID_NDK_HOME=~/Downloads/android-ndk-r27c ~/Library/org.swift.swiftpm/swift-sdks/swift-6.2-RELEASE-android-0.1.artifactbundle/swift-android/scripts/setup-android-sdk.sh
Linux configuration command for android-ndk-r27c-linux.zip:
ANDROID_NDK_HOME=~/android-ndk-r27c ~/.swiftpm/swift-sdks/swift-6.2-RELEASE-android-0.1.artifactbundle/swift-android/scripts/setup-android-sdk.sh
GitHub Actions:
~/.swiftpm/swift-sdks/swift-6.2-RELEASE-android-0.1.artifactbundle/swift-android/scripts/setup-android-sdk.sh
Note
GitHub Actions already includes the Android NDK and defines ANDROID_NDK_HOME, so there is no need to install the NDK separately. Alternatively, you can use the swift-android-action to build Swift packages and run Android tests from a GitHub workflow.
Building Swift Packages for Android
Now you can compile a Swift package for Android with:
$ git clone https://github.com/apple/swift-algorithms.git
$ cd swift-algorithms/
$ swiftly run swift build --swift-sdk aarch64-unknown-linux-android28 +6.2
Running Swift Executables on Android
If you have a connected Android device with USB debugging enabled, or are running an Android emulator, you can create and run a Swift executable with the following commands:
$ mkdir ExecutableDemo
$ cd ExecutableDemo
$ swift package init --type executable
$ swiftly run swift build --static-swift-stdlib --swift-sdk aarch64-unknown-linux-android28 +6.2
$ adb push .build/debug/ExecutableDemo /data/local/tmp/
$ adb push ${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/*/sysroot/usr/lib/aarch64-linux-android/libc++_shared.so /data/local/tmp/
$ adb shell /data/local/tmp/ExecutableDemo