11# Getting Started with Swift on Android
22
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:
66
771 . 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.
99
1010If you encounter any problems following the instructions below, please file a
1111bug using https://bugs.swift.org/ .
@@ -30,13 +30,12 @@ Swift-to-Java bridging.
3030To follow along with this guide, you'll need:
3131
32321 . 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),
3837 available to download here:
39- http ://developer.android.com/ndk/downloads/index.html .
38+ https ://developer.android.com/ndk/downloads/index.html .
40393 . An Android device with remote debugging enabled. We require remote
4140 debugging in order to deploy built stdlib products to the device. You may
4241 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
7473
7574```
7675$ ARM_DIR=path/to/libicu-libiconv-android
77- $ NDK_PATH=path/to/android-ndk16
76+ $ NDK_PATH=path/to/android-ndk21
7877$ utils/build-script \
7978 -R \ # Build in ReleaseAssert mode.
8079 --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
8282 --android-api-level 21 \ # The Android API level to target. Swift only supports 21 or greater.
8383 --android-icu-uc ${ARM_DIR}/libicuucswift.so \
8484 --android-icu-uc-include ${ARM_DIR}/icu/source/common \
8585 --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
8788```
8889
8990### 3. Compiling ` hello.swift ` to run on an Android device
@@ -94,27 +95,18 @@ Create a simple Swift file named `hello.swift`:
9495print (" Hello, Android" )
9596```
9697
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-
10698Then use the built Swift compiler from the previous step to compile a Swift
10799source file, targeting Android:
108100
109101```
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 \
118110 hello.swift
119111```
120112
@@ -149,7 +141,6 @@ $ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswi
149141$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswiftGlibc.so /data/local/tmp
150142$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswiftSwiftOnoneSupport.so /data/local/tmp
151143$ 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
153144```
154145
155146You 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
163154In addition, you'll also need to copy the Android NDK's libc++:
164155
165156```
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
167158```
168159
169160Finally, 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.
192183## Running the Swift test suite hosted on an Android device
193184
194185When 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:
196187
1971881 . Connect your Android device to your computer via USB. Ensure that remote
198189 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:
204195```
205196$ utils/build-script \
206197 -R \ # Build in ReleaseAssert mode.
207- -T \ # Run all tests .
198+ -T --host-test \ # Run all tests, including on the Android host .
208199 --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
210202 --android-ndk-version 21 \
211203 --android-icu-uc ~/libicu-android/armeabi-v7a/libicuuc.so \
212204 --android-icu-uc-include ~/libicu-android/armeabi-v7a/icu/source/common \
213205 --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
231208```
0 commit comments