1
1
# Getting Started with Swift on Android
2
2
3
- The Swift stdlib can be compiled for Android armv7 targets, which makes it
4
- possible to execute Swift code on a mobile device running Android. This guide
5
- explains:
3
+ The Swift stdlib can be compiled for Android armv7 and aarch64 targets, which
4
+ makes it possible to execute Swift code on a mobile device running Android.
5
+ This guide explains:
6
6
7
7
1 . How to run a simple "Hello, world" program on your Android device.
8
- 2 . How to run the Swift test suite, targeting Android, and on an Android device.
8
+ 2 . How to run the Swift test suite on an Android device.
9
9
10
10
If you encounter any problems following the instructions below, please file a
11
11
bug using https://bugs.swift.org/ .
@@ -30,13 +30,12 @@ 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, specifically
33
- Ubuntu 16.04 or Ubuntu 15.10 (Ubuntu 14.04 has not been tested recently).
34
- The stdlib is currently only buildable for Android from a Linux environment.
35
- Before attempting to build for Android, please make sure you are able to build
36
- for Linux by following the instructions in the Swift project README.
37
- 2 . The latest version of the Android NDK (r16 at the time of this writing),
33
+ Ubuntu 18.04 or Ubuntu 16.04. Before attempting to build for Android,
34
+ please make sure you are able to build for Linux by following the
35
+ instructions in the Swift project README.
36
+ 2 . The latest version of the Android NDK (r21 at the time of this writing),
38
37
available to download here:
39
- http ://developer.android.com/ndk/downloads/index.html .
38
+ https ://developer.android.com/ndk/downloads/index.html .
40
39
3 . An Android device with remote debugging enabled. We require remote
41
40
debugging in order to deploy built stdlib products to the device. You may
42
41
turn on remote debugging by following the official instructions:
@@ -74,16 +73,18 @@ Android NDK, as well as the directories that contain the `libicuucswift.so` and
74
73
75
74
```
76
75
$ ARM_DIR=path/to/libicu-libiconv-android
77
- $ NDK_PATH=path/to/android-ndk16
76
+ $ NDK_PATH=path/to/android-ndk21
78
77
$ utils/build-script \
79
78
-R \ # Build in ReleaseAssert mode.
80
79
--android \ # Build for Android.
81
- --android-ndk $NDK_PATH \ # Path to an Android NDK.
80
+ --android-ndk $NDK_PATH \ # Path to an Android NDK.
81
+ --android-arch armv7 \ # Optionally specify Android architecture, alternately aarch64
82
82
--android-api-level 21 \ # The Android API level to target. Swift only supports 21 or greater.
83
83
--android-icu-uc ${ARM_DIR}/libicuucswift.so \
84
84
--android-icu-uc-include ${ARM_DIR}/icu/source/common \
85
85
--android-icu-i18n ${ARM_DIR}/libicui18nswift.so \
86
- --android-icu-i18n-include ${ARM_DIR}/icu/source/i18n
86
+ --android-icu-i18n-include ${ARM_DIR}/icu/source/i18n \
87
+ --android-icu-data ${ARM_DIR}/libicudataswift.so
87
88
```
88
89
89
90
### 3. Compiling ` hello.swift ` to run on an Android device
@@ -94,27 +95,18 @@ Create a simple Swift file named `hello.swift`:
94
95
print (" Hello, Android" )
95
96
```
96
97
97
- To compile it, we need to make sure the correct linker is used. Symlink the
98
- gold linker in the Android NDK into your ` PATH ` :
99
-
100
- ```
101
- $ sudo ln -s \
102
- /path/to/android-ndk-r16/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/arm-linux-androideabi/bin/ld.gold \
103
- /usr/bin/armv7-none-linux-androideabi-ld.gold
104
- ```
105
-
106
98
Then use the built Swift compiler from the previous step to compile a Swift
107
99
source file, targeting Android:
108
100
109
101
```
110
- $ NDK_PATH="path/to/android-ndk16 "
111
- $ build/Ninja-ReleaseAssert/swift-linux-x86_64/bin/swiftc \ # The Swift compiler built in the previous step.
112
- # The location of the tools used to build Android binaries
113
- -tools-directory ${NDK_PATH}/toolchains/arm-linux-androideabi-4.9 /prebuilt/linux-x86_64/arm-linux-androideabi/ bin
114
- -target armv7 -none-linux-androideabi \ # Targeting android-armv7.
115
- -sdk ${NDK_PATH}/platforms/android-21/arch-arm \ # Use the same NDK path and API version as you used to build the stdlib in the previous step.
116
- -L ${NDK_PATH}/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a \ # Link the Android NDK's libc++ and libgcc.
117
- -L ${NDK_PATH}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x \
102
+ $ NDK_PATH="path/to/android-ndk21 "
103
+ $ build/Ninja-ReleaseAssert/swift-linux-x86_64/bin/swiftc \ # The Swift compiler built in the previous step.
104
+ # The location of the tools used to build Android binaries
105
+ -tools-directory ${NDK_PATH}/toolchains/llvm /prebuilt/linux-x86_64/bin/ \
106
+ -target armv7a -none-linux-androideabi \ # Targeting android-armv7.
107
+ -sdk ${NDK_PATH}/platforms/android-21/arch-arm \ # Use the same architecture and API version as you used to build the stdlib in the previous step.
108
+ -Xclang-linker -nostdlib++ \ # Don't link libc++, and supply the path to libgcc.
109
+ -L ${NDK_PATH}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/armv7-a \
118
110
hello.swift
119
111
```
120
112
@@ -149,7 +141,6 @@ $ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswi
149
141
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswiftGlibc.so /data/local/tmp
150
142
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswiftSwiftOnoneSupport.so /data/local/tmp
151
143
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswiftRemoteMirror.so /data/local/tmp
152
- $ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswiftSwiftExperimental.so /data/local/tmp
153
144
```
154
145
155
146
You will also need to push the icu libraries:
@@ -163,7 +154,7 @@ adb push /path/to/libicu-android/armeabi-v7a/libicuucswift.so /data/local/tmp
163
154
In addition, you'll also need to copy the Android NDK's libc++:
164
155
165
156
```
166
- $ adb push /path/to/android-ndk-r14 /sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_shared.so /data/local/tmp
157
+ $ adb push /path/to/android-ndk-r21 /sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_shared.so /data/local/tmp
167
158
```
168
159
169
160
Finally, you'll need to copy the ` hello ` executable you built in the
@@ -192,7 +183,7 @@ Congratulations! You've just run your first Swift program on Android.
192
183
## Running the Swift test suite hosted on an Android device
193
184
194
185
When running the test suite, build products are automatically pushed to your
195
- device. As in part one , you'll need to connect your Android device via USB:
186
+ device. As in part four , you'll need to connect your Android device via USB:
196
187
197
188
1 . Connect your Android device to your computer via USB. Ensure that remote
198
189
debugging is enabled for that device by following the official instructions:
@@ -204,28 +195,14 @@ device. As in part one, you'll need to connect your Android device via USB:
204
195
```
205
196
$ utils/build-script \
206
197
-R \ # Build in ReleaseAssert mode.
207
- -T \ # Run all tests .
198
+ -T --host-test \ # Run all tests, including on the Android host .
208
199
--android \ # Build for Android.
209
- --android-ndk ~/android-ndk-r13 \ # Path to an Android NDK.
200
+ --android-ndk ~/android-ndk-r21 \ # Path to an Android NDK.
201
+ --android-arch armv7 \ # Optionally specify Android architecture, alternately aarch64
210
202
--android-ndk-version 21 \
211
203
--android-icu-uc ~/libicu-android/armeabi-v7a/libicuuc.so \
212
204
--android-icu-uc-include ~/libicu-android/armeabi-v7a/icu/source/common \
213
205
--android-icu-i18n ~/libicu-android/armeabi-v7a/libicui18n.so \
214
- --android-icu-i18n-include ~/libicu-android/armeabi-v7a/icu/source/i18n/
215
- ```
216
-
217
- ## Build Android Toolchain
218
-
219
- This toolchain will generate the .so and .swiftmodule files of the Swift standard library and Foundation framework for the Android environment, armv7 architecture. Those files are needed when building any Swift library to be included in an application for Android.
220
-
221
- To build the toolchain run:
222
-
223
- ```
224
- $ utils/android/build-toolchain
225
- ```
226
-
227
- It will be built on:
228
-
229
- ```
230
- path/to/swift-source/swift-android-toolchain
206
+ --android-icu-i18n-include ~/libicu-android/armeabi-v7a/icu/source/i18n/ \
207
+ --android-icu-data ~/libicu-android/armeabi-v7a/libicudata.so
231
208
```
0 commit comments