swift-android-toolchain
swift-android-toolchain copied to clipboard
Build Swift for Android from your Mac
swift-android-toolchain
Build Swift for Android from Mac
Installation
For use within a specific project
- Add
swift-android-toolchainas a git submodule - Use
./path-to-submodule/swift-build.shwherever needed
UIKit-cross-platform uses this method (along with the Android Studio integration, below).
For use globally from the command line
- Clone the repo
- Add the cloned destination to your
PATHenvironment variable (e.g. in~/.bash_profileor equivalent)
Usage
In Android Studio / Gradle
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.
From the command line
ANDROID_ABI="armeabi-v7a" CMAKE_BUILD_TYPE="Debug" swift-build.sh
MacOS file permissions
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.
Credits
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.