More: Author   ReportBugs   

Automated scripts to build Swift Android cross compilation toolchain for macOS

Based on:


Prebuilt toolchain can be located here

Prepare environment

  1. Install JDK 8 if needed. Call javac from terminal and macOS will guide you.
  2. Install Swift 4.0.3 toolchain for Xcode https://swift.org/builds/swift-4.0.3-release/xcode/swift-4.0.3-RELEASE/swift-4.0.3-RELEASE-osx.pkg
  3. Install Android Studio 3.3 or higher (optional)
  4. Install brew if needed
  5. Install tools, NDK and Swift Android Toolchain
# install system tools
brew install coreutils cmake wget

cd ~
mkdir android
cd android


# install ndk
wget https://dl.google.com/android/repository/android-ndk-r$NDK-darwin-x86_64.zip
unzip android-ndk-r$NDK-darwin-x86_64.zip
rm -rf android-ndk-r$NDK-darwin-x86_64.zip
unset NDK

# instal swift android toolchain
wget https://dl.bintray.com/readdle/swift-android-toolchain/swift-android-$SWIFT_ANDROID.zip
unzip swift-android-$SWIFT_ANDROID.zip
rm -rf swift-android-$SWIFT_ANDROID.zip
  1. Setup environment variables by putting this to .profile

export JAVA_HOME=$(/usr/libexec/java_home --version 1.8)
# Uncomment if you install Android Studio
# export ANDROID_HOME=$HOME/Library/Android/sdk
export ANDROID_NDK_HOME=$HOME/android/android-ndk-r$NDK
export SWIFT_ANDROID_HOME=$HOME/android/swift-android-$SWIFT_ANDROID
export TOOLCHAINS=swift

export PATH=$SWIFT_ANDROID_HOME/bin:$SWIFT_ANDROID_HOME/build-tools/current:$PATH

unset NDK
  1. Include .profile to your .bashrc or .zshrc if needed by adding this line
source $HOME/.profile
  1. Install Swift Android Build tools(optional). If you want to run swift compiler from command line reload your environment from step 4 and run following command
swift android tools --update

Build and Test swift modules

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-build wrapper scripts works as swift build from swift package manager but configuresd for android. So you can add any extra params like -Xswiftc -DDEBUG , -Xswiftc -suppress-warnings or --configuration release

Example of compilation flags:


swift-build --configuration debug \
    -Xswiftc -DDEBUG \
    -Xswiftc -Xfrontend -Xswiftc -experimental-disable-objc-attr


    swift-build --configuration release \
    -Xswiftc -Xfrontend -Xswiftc -experimental-disable-objc-attr \
    -Xswiftc -Xllvm -Xswiftc -sil-disable-pass=array-specialize

Build swift modules with Android Studio

This plugin integrates Swift Android Toolchain to Gradle

Build Toolchain

System Requirements

Building stdlib require Linux and building macOS compiler requires macOS So building scripts uses vagrant to automate eentire process on macOS host


Install virtualbox
Install vagrant
Install vagrant plugins

vagrant plugin install vagrant-disksize
vagrant plugin install vagrant-scp

macOS build tools

Install cmake and ninja

brew install cmake ninja pkg-config

Android NDK

Install Android NDK 15c from android-ndk-r15c-darwin-x86_64.zip and define ANDROID_NDK_HOME env variable

cd $somewhere
wget https://dl.google.com/android/repository/android-ndk-r15c-darwin-x86_64.zip
unzip android-ndk-r15c-darwin-x86_64.zip
rm android-ndk-r15c-darwin-x86_64.zip
export ANDROID_NDK_HOME=$PWD/android-ndk-r15c


Run build/build.sh from project root. Resulting toolchain will be generated in out/swift-android-$VERSION.zip

To clean building directory run build/clean.sh in project root

It usually take around 2 hours on top MacBook Pro

Support Me
About Me
Google+: Trinea trinea
GitHub: Trinea