Build Swift for Android from Mac
- Add
swift-android-toolchain
as a git submodule - Use
./path-to-submodule/swift-build.sh
wherever needed
UIKit-cross-platform uses this method (along with the Android Studio integration, below).
- Clone the repo
- Add the cloned destination to your
PATH
environment variable (e.g. in~/.bash_profile
or equivalent)
You probably want Gradle to compile your native sources automatically when building the android app. In order to achieve this, you have to create another CMakeLists.txt
file and reference it from app/build.gradle
.
externalNativeBuild {
cmake {
version "3.16.2"
path "CMakeLists.txt" // android/app/src/CMakeLists.txt
}
}
In android/app/src/CMakeLists.txt
, add the following code after the cmake_minimum_required(VERSION x.y.z)
declaration.
# destination of project level CMakeLists.txt for building native sources
get_filename_component(PROJECT_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/../../ ABSOLUTE)
# path to swift-android-toolchain
set(BuildSwiftProject_DIR ../../swift-android-toolchain)
find_package(BuildSwiftProject REQUIRED)
build_swift_project(
PROJECT_DIRECTORY ${PROJECT_DIRECTORY}
)
Check out getting-started for a working example.
ANDROID_ABI="armeabi-v7a" CMAKE_BUILD_TYPE="Debug" swift-build.sh
Since we use downloaded versions of the swift toolchain and Android NDK in this build, MacOS complains about potential security issues. Running the build might show you alerts for every binary that is invoked by the build script, asking to manually grant permissions.
Running the fixMacOSPermissions.sh
script, after setup, but before invoking the build, should resolve this issue.
Making this toolchain was only possible by standing on the shoulders of giants.
Many thanks to Vlad Gorlov for his great work with the swift-everywhere-toolchain, Gonzalo Lorralde for swifty-robot-environment and John Holdsworth for his android_toolchain. And to the Swift community as a whole for their ongoing work at making the language great.