Skip to content

Commit 6393170

Browse files
authored
Merge pull request #985 from swift-android-sdk/android-docs
Add a Getting Started document for the Swift Android SDK
2 parents 5f48fd1 + 5ddf3a8 commit 6393170

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed

_data/authors.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,13 @@ parispittman:
510510
name: Paris Pittman
511511
github: parispittman
512512

513+
marcprux:
514+
name: Marc Prud'hommeaux
515+
email: marc@skip.tools
516+
gravatar: marcprux
517+
github: marcprux
518+
about: "Marc Prud'hommeaux works on bringing Swift application development to Android at Skip.tools."
519+
513520
maxdesiatov:
514521
name: Max Desiatov
515522
github: maxdesiatov

_data/documentation.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
description: |
6464
Learn how to get started building binaries for Linux with no system dependencies (not even the Swift runtime or C library).
6565
Even better, you can do this from any system with a Swift toolchain, allowing you to develop on macOS or Windows and easily deploy to Linux when you go to production.
66+
- title: Getting Started with the Swift SDK for Android
67+
url: /documentation/articles/swift-sdk-for-android-getting-started.html
68+
description: |
69+
Learn how to get started cross-compiling Swift packages for Android.
6670
- title: Getting Started with Swift SDKs for WebAssembly
6771
url: /documentation/articles/wasm-getting-started.html
6872
description: |
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
---
2+
layout: page
3+
date: 2025-10-24 11:00:00
4+
title: Getting Started with the Swift SDK for Android
5+
author: [marcprux]
6+
---
7+
8+
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.
9+
10+
### Getting Started
11+
12+
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).
13+
14+
To cross-compile Swift code for Android, you need three separate components:
15+
16+
1. **The Host Toolchain**: this is the `swift` command and related tools that you will use to build and run your Swift code.
17+
2. **The Swift SDK for Android**: the set of libraries, headers, and other resources needed to generate and run Swift code for the Android target.
18+
3. **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.
19+
20+
21+
#### 1. Install the Host Toolchain
22+
23+
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.
24+
25+
The easiest and recommended way to manage host toolchains on macOS and Linux is to use [the swiftly command](/swiftly/documentation/swiftly/getting-started) command. Once that has been setup, you can install the host toolchain with:
26+
27+
```console
28+
$ swiftly install main-snapshot-2025-10-16
29+
Installing Swift main-snapshot-2025-10-16
30+
Installing package in user home directory...
31+
main-snapshot-2025-10-16 installed successfully!
32+
33+
$ swiftly use main-snapshot-2025-10-16
34+
The global default toolchain has been set to `main-snapshot-2025-10-16` (was 6.2.0)
35+
36+
$ swiftly run swift --version
37+
Apple Swift version 6.3-dev (LLVM 0d0246569621d5b, Swift 199240b3fe97eda)
38+
Target: arm64-apple-macosx15.0
39+
```
40+
41+
#### 2. Install the Swift SDK for Android
42+
43+
Next, download and install the Swift SDK bundle using the built-in `swift sdk` command:
44+
45+
```console
46+
$ swift sdk install https://download.swift.org/development/android-sdk/swift-DEVELOPMENT-SNAPSHOT-2025-10-16-a/swift-DEVELOPMENT-SNAPSHOT-2025-10-16-a_android-0.1.artifactbundle.tar.gz --checksum 451844c232cf1fa02c52431084ed3dc27a42d103635c6fa71bae8d66adba2500
47+
48+
Swift SDK bundle at `https://download.swift.org/development/android-sdk/swift-DEVELOPMENT-SNAPSHOT-2025-10-16-a/swift-DEVELOPMENT-SNAPSHOT-2025-10-16-a_android-0.1.artifactbundle.tar.gz` successfully installed as swift-DEVELOPMENT-SNAPSHOT-2025-10-16-a-android-0.1.artifactbundle.
49+
```
50+
51+
You should now see the Android Swift SDK included in the `swift sdk list` command:
52+
53+
```console
54+
$ swiftly run swift sdk list
55+
swift-DEVELOPMENT-SNAPSHOT-2025-10-16-a-android-0.1
56+
```
57+
58+
#### 3. Install and configure the Android NDK
59+
60+
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](https://developer.android.com/ndk/guides), but the simplest is to just download and unzip the archive from the [NDK Downloads page](https://developer.android.com/ndk/downloads/#lts-downloads) directly.
61+
62+
You can automate this with the following commands in a directory of your choosing:
63+
64+
```console
65+
$ mkdir ~/android-ndk
66+
$ cd ~/android-ndk
67+
$ curl -fSLO https://dl.google.com/android/repository/android-ndk-r27d-$(uname -s).zip
68+
$ unzip -q android-ndk-r27d-*.zip
69+
$ export ANDROID_NDK_HOME=$PWD/android-ndk-r27d
70+
```
71+
72+
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:
73+
74+
```console
75+
$ cd ~/Library/org.swift.swiftpm || cd ~/.swiftpm
76+
$ ./swift-sdks/swift-DEVELOPMENT-SNAPSHOT-2025-10-16-a-android-0.1.artifactbundle/swift-android/scripts/setup-android-sdk.sh
77+
setup-android-sdk.sh: success: ndk-sysroot linked to Android NDK at android-ndk-r27d/toolchains/llvm/prebuilt
78+
```
79+
80+
*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.*
81+
82+
At this point, you will have a fully working cross-compilation toolchain for Android.
83+
84+
### Hello World on Android
85+
86+
Now let's try it out with the canonical "Hello World" program. First, create a directory to hold your code:
87+
88+
```console
89+
$ cd /tmp
90+
$ mkdir hello
91+
$ cd hello
92+
```
93+
94+
Next, ask Swift to create a new package for you:
95+
96+
```console
97+
$ swiftly run swift package init --type executable
98+
```
99+
100+
Try it out by building and running locally for the host machine:
101+
102+
```console
103+
$ swiftly run swift build
104+
Building for debugging...
105+
[8/8] Applying hello
106+
Build complete! (15.29s)
107+
108+
$ .build/debug/hello
109+
Hello, world!
110+
```
111+
112+
With the Swift SDK for Android installed and configured, you can now cross-compile the executable to Android for the `x86_64` architecture:
113+
114+
```console
115+
$ swiftly run swift build --swift-sdk x86_64-unknown-linux-android28 --static-swift-stdlib
116+
Building for debugging...
117+
[8/8] Linking hello
118+
Build complete! (2.04s)
119+
120+
$ file .build/x86_64-unknown-linux-android28/debug/hello
121+
.build/x86_64-unknown-linux-android28/debug/hello: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, with debug_info, not stripped
122+
```
123+
124+
or for the `aarch64` architecture:
125+
126+
```console
127+
$ swiftly run swift build --swift-sdk aarch64-unknown-linux-android28 --static-swift-stdlib
128+
Building for debugging...
129+
[8/8] Linking hello
130+
Build complete! (2.04s)
131+
132+
$ file .build/aarch64-unknown-linux-android28/debug/hello
133+
.build/aarch64-unknown-linux-android28/debug/hello: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, with debug_info, not stripped
134+
```
135+
136+
With a connected Android device that has [USB debugging enabled](https://developer.android.com/studio/debug/dev-options#Enable-debugging) or a locally-running Android [emulator](https://developer.android.com/studio/run/emulator#get-started), 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`](https://developer.android.com/tools/adb) utility:
137+
138+
```console
139+
$ adb push .build/aarch64-unknown-linux-android28/debug/hello /data/local/tmp
140+
.build/aarch64-unknown-linux-android28/debug/hello: 1 file pushed, 0 skipped. 155.9 MB/s (69559568 bytes in 0.425s)
141+
142+
$ adb push $ANDROID_NDK_HOME/toolchains/llvm/prebuilt/*/sysroot/usr/lib/aarch64-linux-android/libc++_shared.so /data/local/tmp/
143+
aarch64-linux-android/libc++_shared.so: 1 file pushed, 0 skipped. 145.7 MB/s (1794776 bytes in 0.012s)
144+
145+
$ adb shell /data/local/tmp/hello
146+
Hello, world!
147+
```
148+
149+
### Next Steps
150+
151+
Congratulations, you have built and run your first Swift program on Android!
152+
153+
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](https://developer.android.com/training/articles/perf-jni)).
154+
155+
Visit the [swift-android-examples](https://github.com/swiftlang/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.
156+
157+
These larger development topics will be expanded on in future articles and documentation. In the meantime, please visit the [Swift Android forums](https://forums.swift.org/c/platform/android/115) to discuss and seek help with the Swift SDK for Android.
158+

0 commit comments

Comments
 (0)