1
1
# Getting Started with Swift on Android
2
2
3
- The Swift stdlib can be compiled for Android armv7, x86_64, and aarch64 targets ,
4
- which makes it possible to execute Swift code on a mobile device running
5
- Android or an emulator. This guide explains:
3
+ The Swift standard library ( stdlib) can be compiled for Android armv7, x86_64,
4
+ and aarch64 targets, which makes it possible to execute Swift code on a mobile
5
+ device running Android or an emulator. This guide explains:
6
6
7
7
1 . How to run a simple "Hello, world" program on your Android device.
8
8
2 . How to run the Swift test suite on an Android device.
@@ -12,7 +12,7 @@ bug using https://bugs.swift.org/.
12
12
13
13
## FAQ
14
14
15
- Let's answer a few frequently asked questions right off the bat:
15
+ Let's answer a frequently asked question right off the bat:
16
16
17
17
### Does this mean I can write Android applications in Swift?
18
18
@@ -30,32 +30,44 @@ Swift-to-Java bridging.
30
30
To follow along with this guide, you'll need:
31
31
32
32
1 . A Linux environment capable of building Swift from source, preferably
33
- Ubuntu 18 .04 or Ubuntu 16 .04. Before attempting to build for Android,
33
+ Ubuntu 20 .04 or Ubuntu 18 .04. Before attempting to build for Android,
34
34
please make sure you are able to build for Linux by following the
35
35
instructions in the Swift project README.
36
- 2 . The latest version of the Android NDK (r23b at the time of this writing),
36
+ 2 . The latest build of the Swift compiler for your Linux distro, available at
37
+ https://www.swift.org/download/ or sometimes your distro package manager.
38
+ 3 . The latest version of the Android LTS NDK (r23c at the time of this writing),
37
39
available to download here:
38
- https://developer.android.com/ndk/downloads/index.html .
39
- 3 . An Android device with remote debugging enabled or the emulator. We require
40
+ https://developer.android.com/ndk/downloads
41
+ 4 . An Android device with remote debugging enabled or the emulator. We require
40
42
remote debugging in order to deploy built stdlib products to the device. You
41
43
may turn on remote debugging by following the official instructions:
42
- https://developer.chrome.com/devtools/docs/remote-debugging .
44
+ https://developer.chrome.com/devtools/docs/remote-debugging .
43
45
44
46
## "Hello, world" on Android
45
47
46
- ### 1. Building the Swift stdlib for Android
48
+ ### 1. Building the standalone Swift stdlib for Android
47
49
48
- Enter your Swift directory, then run the build script, passing the path to the
49
- Android NDK:
50
+ Enter your Swift directory, check out the tag corresponding to the prebuilt
51
+ Swift compiler, then run the build script, passing the path to the Android NDK
52
+ and the prebuilt Swift toolchain (add --skip-early-swift-driver if you already
53
+ have a Swift toolchain in your path):
50
54
51
55
```
52
- $ NDK_PATH=path/to/android-ndk-r23b
56
+ $ NDK_PATH=path/to/android-ndk-r23c
57
+ $ SWIFT_PATH=path/to/swift-DEVELOPMENT-SNAPSHOT-2022-05-31-a-ubuntu20.04/usr/bin
58
+ $ git checkout swift-DEVELOPMENT-SNAPSHOT-2022-05-31-a
53
59
$ utils/build-script \
54
60
-R \ # Build in ReleaseAssert mode.
55
61
--android \ # Build for Android.
56
62
--android-ndk $NDK_PATH \ # Path to an Android NDK.
57
- --android-arch armv7 \ # Optionally specify Android architecture, alternately aarch64 or x86_64
58
- --android-api-level 21 # The Android API level to target. Swift only supports 21 or greater.
63
+ --android-arch aarch64 \ # Optionally specify Android architecture, alternately armv7 or x86_64
64
+ --android-api-level 21 \ # The Android API level to target. Swift only supports 21 or greater.
65
+ --stdlib-deployment-targets=android-aarch64 \ # Only cross-compile the stdlib for Android, ie don't build the native stdlib for Linux
66
+ --native-swift-tools-path=$SWIFT_PATH \ # Path to your prebuilt Swift compiler
67
+ --native-clang-tools-path=$SWIFT_PATH \ # Path to a prebuilt clang compiler, one comes with the Swift toolchain
68
+ --build-swift-tools=0 \ # Don't build the Swift compiler and other host tools
69
+ --build-llvm=0 \ # Don't build the LLVM libraries, but generate some CMake files needed by the Swift stdlib build
70
+ --skip-build-cmark # Don't build the CommonMark library that's only needed by the Swift compiler
59
71
```
60
72
61
73
### 2. Compiling ` hello.swift ` to run on an Android device
@@ -66,15 +78,16 @@ Create a simple Swift file named `hello.swift`:
66
78
print (" Hello, Android" )
67
79
```
68
80
69
- Then use the built Swift compiler from the previous step to compile a Swift
81
+ Then use the standalone Swift stdlib from the previous step to compile a Swift
70
82
source file, targeting Android:
71
83
72
84
```
73
- $ NDK_PATH="path/to/android-ndk-r23b"
74
- $ build/Ninja-ReleaseAssert/swift-linux-x86_64/bin/swiftc \ # The Swift compiler built in the previous step
85
+ $ NDK_PATH="path/to/android-ndk-r23c"
86
+ $ SWIFT_PATH=path/to/swift-DEVELOPMENT-SNAPSHOT-2022-05-31-a-ubuntu20.04/usr/bin
87
+ $ $SWIFT_PATH/swiftc \ # The prebuilt Swift compiler you downloaded
75
88
# The location of the tools used to build Android binaries
76
89
-tools-directory ${NDK_PATH}/toolchains/llvm/prebuilt/linux-x86_64/bin/ \
77
- -target armv7 -unknown-linux-androideabi21 \ # Targeting Android armv7 at API 21
90
+ -target aarch64 -unknown-linux-android21 \ # Targeting Android aarch64 at API 21
78
91
-sdk ${NDK_PATH}/toolchains/llvm/prebuilt/linux-x86_64/sysroot \ # The SDK is the Android unified sysroot and the resource-dir is where you just built the Swift stdlib.
79
92
-resource-dir build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift
80
93
hello.swift
@@ -111,12 +124,15 @@ $ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswi
111
124
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswiftGlibc.so /data/local/tmp
112
125
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswiftSwiftOnoneSupport.so /data/local/tmp
113
126
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswiftRemoteMirror.so /data/local/tmp
127
+ $ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswift_Concurrency.so /data/local/tmp
128
+ $ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libdispatch.so /data/local/tmp
129
+ $ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libBlocksRuntime.so /data/local/tmp
114
130
```
115
131
116
132
In addition, you'll also need to copy the Android NDK's libc++:
117
133
118
134
```
119
- $ adb push /path/to/android-ndk-r23b /sources/cxx-stl/llvm-libc++/libs/armeabi-v7a /libc++_shared.so /data/local/tmp
135
+ $ adb push /path/to/android-ndk-r23c /sources/cxx-stl/llvm-libc++/libs/arm64-v8a /libc++_shared.so /data/local/tmp
120
136
```
121
137
122
138
Finally, you'll need to copy the ` hello ` executable you built in the
@@ -157,9 +173,12 @@ device. As in part three, you'll need to connect your Android device via USB:
157
173
```
158
174
$ utils/build-script \
159
175
-R \ # Build in ReleaseAssert mode.
160
- -T \ # Run all tests, including on the Android device (add --host-test to only run Android tests on the linux host).
176
+ -T \ # Run all tests, including on the Android device (add --host-test to only run Android tests on the Linux host).
161
177
--android \ # Build for Android.
162
- --android-ndk ~/android-ndk-r23b \ # Path to an Android NDK.
163
- --android-arch armv7 \ # Optionally specify Android architecture, alternately aarch64
178
+ --android-ndk ~/android-ndk-r23c \ # Path to an Android NDK.
179
+ --android-arch aarch64 \ # Optionally specify Android architecture, alternately armv7
164
180
--android-api-level 21
165
181
```
182
+
183
+ This will build the Swift compiler and other host tools first, so expect a much
184
+ longer build.
0 commit comments