Automated scripts to build Swift Android cross compilation toolchain for macOS
Prebuilt toolchains are located on Github Releases
- [IMPORTANT] Swift Android Toolchain uses the macOS Swift toolchain. That's why it will work ONLY with the proper version of the host toolchain. There are 2 options on how to switch the default toolchain to a proper version:
- Install XCode 14.2 and make it default in Command Line
- Or install Swift toolchain 5.7.3 in you current XCode and add
export TOOLCHAINS=swift
to enviroment
- Install NDK and Swift Android Toolchain.
- If you have Android SDK Command-Line Tools installed:
sdkmanager --install "ndk;25.2.9519653"
- otherwise:
curl -O https://dl.google.com/android/repository/android-ndk-r25c-darwin.dmg
hdiutil attach android-ndk-r25c-darwin.dmg
cp -r "/Volumes/Android NDK r25c/AndroidNDK9519653.app/Contents/NDK/" ./android-ndk-r25c
hdiutil detach "/Volumes/Android NDK r25c"
- Install Swift Android Toolchain
curl -L -O https://github.com/readdle/swift-android-toolchain/releases/latest/download/swift-android.zip
unzip swift-android.zip
swift-android/bin/swift-android tools --update
- Setup environment variables by adding:
export ANDROID_NDK_HOME=<PATH_TO_NDK>
export SWIFT_ANDROID_HOME=<PATH_TO_SWIFT_ANDROID>
export PATH=$ANDROID_NDK_HOME:$PATH
export PATH=$SWIFT_ANDROID_HOME/bin:$SWIFT_ANDROID_HOME/build-tools/current:$PATH
Our current swift build system is tiny wrapper over Swift PM. See Swift PM docs for more info.
Command | Description |
---|---|
swift package clean | Clean build folder |
swift package update | Update dependencies |
swift-build | Build all products |
swift-build --build-tests | Build all products and tests |
swift-test | Connect to Android device and run all tests |
swift-build wrapper scripts works as swift build from swift package manager but configured for android. So you can add any extra params like -Xswiftc -DDEBUG , -Xswiftc -suppress-warnings or --configuration release
Example of compilation flags:
Debug
swift-build --configuration debug \
-Xswiftc -DDEBUG \
-Xswiftc -Xfrontend -Xswiftc -experimental-disable-objc-attr
Release
swift-build --configuration release \
-Xswiftc -Xfrontend -Xswiftc -experimental-disable-objc-attr \
-Xswiftc -Xllvm -Xswiftc -sil-disable-pass=array-specialize
This plugin integrates Swift Android Toolchain to Gradle