Since it was first open-sourced in 2015, Swift has grown from a language focused on creating apps for Darwin-based systems (iOS, macOS, etc.) into a cross-platform development language supporting Linux, Windows, and various embedded systems. With the release of the Swift SDK for Android, it is now possible to use Swift for Android application development as well.
Building a Swift package for Android involves installing and configuring a cross-compilation SDK. Cross-compilation is the process of building code on one platform, the host, to run on a different platform, the target. In the context of Swift for Android, this typically involves compiling Swift code on a host macOS or Linux machine to produce executables or libraries for the target Android OS. This differs from compiling Swift for the host platform, where the host and target are the same (for example, compiling and running Swift code on macOS for macOS).
To cross-compile Swift code for Android, you need three separate components:
- The Host Toolchain: this is the swift command and related tools that you will use to build and run your Swift code.
- The Swift SDK for Android: the set of libraries, headers, and other resources needed to generate and run Swift code for the Android target.
- The Android NDK: the “Native Development Kit” for Android includes the cross-compilation tools like clang and ld that are used by the host toolchain to cross-compile and link.
The first thing to note is that while swift may already be installed on your system (such as through an Xcode installation on macOS), using a cross-compilation Swift SDK requires that the host toolchain and the Swift SDK versions match exactly. For this reason, you will need to install the specific version of the host toolchain for the given Swift SDK version.
The easiest and recommended way to manage host toolchains on macOS and Linux is to use the swiftly command command. Once that has been setup, you can install the host toolchain with:
2. Install the Swift SDK for Android <?xml version="1.0" encoding="utf-8"?>
Next, download and install the Swift SDK bundle using the built-in swift sdk command:
You should now see the Android Swift SDK included in the swift sdk list command:
3. Install and configure the Android NDK <?xml version="1.0" encoding="utf-8"?>
The Swift SDK for Android depends on the Android NDK version 27d to provide the headers and tools necessary for cross-compiling to Android architectures. There are a variety of ways to install the Android NDK, but the simplest is to just download and unzip the archive from the NDK Downloads page directly.
You can automate this with the following commands in a directory of your choosing:
Once you have downloaded and unpacked the NDK, you must link it to the Swift SDK for Android by running the setup-android-sdk.sh utility script included with the Swift SDK bundle:
Note that if you have already installed the NDK in a different location, you can simply set the ANDROID_NDK_HOME environment variable to that location and run the setup-android-sdk.sh script.
At this point, you will have a fully working cross-compilation toolchain for Android.
Hello World on Android <?xml version="1.0" encoding="utf-8"?>
Now let’s try it out with the canonical “Hello World” program. First, create a directory to hold your code:
Next, ask Swift to create a new package for you:
Try it out by building and running locally for the host machine:
With the Swift SDK for Android installed and configured, you can now cross-compile the executable to Android for the x86_64 architecture:
or for the aarch64 architecture:
With a connected Android device that has USB debugging enabled or a locally-running Android emulator, you can now copy the executable over, along with the required libc++_shared.so dependency from the Android NDK, and run it with the adb utility:
Congratulations, you have built and run your first Swift program on Android!
Note that Android applications are typically not deployed as command-line executable tools. Rather, they are assembled into an .apk archive and launched as an app from the home screen. To support this, Swift modules can be built as shared object libraries for each supported architecture and included in the app archive. The Swift code can then be accessed from the Android app — which is typically written in Java or Kotlin — through the Java Native Interface (JNI).
Visit the swift-android-examples repository to see a variety of projects that demonstrate how to build full Android applications that utilize the Swift SDK for Android.
These larger development topics will be expanded on in future articles and documentation. In the meantime, please visit the Swift Android forums to discuss and seek help with the Swift SDK for Android.
.png)

