Skip to main content
  1. All Posts/

swift-android-toolchain

Tools Shell

Swift Android Toolchain

Automated scripts to build Swift Android cross compilation toolchain for macOS

Installation

Prebuilt toolchains are located on Github Releases

Prepare environment (macOS x86_64 or macOS arm64)

  1. [IMPORTANT] Install XCode 13.0 and make it default in Command Line
  2. Install brew if needed
  3. 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-r21e-darwin-x86_64.zip
unzip android-ndk-r21e-darwin-x86_64.zip
rm -rf android-ndk-r21e-darwin-x86_64.zip
 
# instal swift android toolchain
SWIFT_ANDROID=$(curl --silent "https://api.github.com/repos/readdle/swift-android-toolchain/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/1/')
wget https://github.com/readdle/swift-android-toolchain/releases/latest/download/swift-android-$SWIFT_ANDROID.zip
unzip swift-android-$SWIFT_ANDROID.zip
rm -rf swift-android-$SWIFT_ANDROID.zip

swift-android-$SWIFT_ANDROID/bin/swift-android tools --update
ln -sfn swift-android-$SWIFT_ANDROID swift-android-current
unset SWIFT_ANDROID
  1. Setup environment variables by putting this to .profile
export ANDROID_NDK_HOME=$HOME/android/android-ndk-r21e
export SWIFT_ANDROID_HOME=$HOME/android/swift-android-current
 
export PATH=$ANDROID_NDK_HOME:$PATH
export PATH=$SWIFT_ANDROID_HOME/bin:$SWIFT_ANDROID_HOME/build-tools/current:$PATH
  1. Include .profile to your .bashrc or .zshrc if needed by adding this line
source $HOME/.profile

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 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

Build swift modules with Android Studio

This plugin integrates Swift Android Toolchain to Gradle

Other swift releated projects

  1. Anotation Processor for generating JNI code
  2. Sample todo app
  3. Cross-platform swift weather app