11# Getting Started with Swift on Android
22
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:
66
771 . How to run a simple "Hello, world" program on your Android device.
882 . How to run the Swift test suite on an Android device.
@@ -12,7 +12,7 @@ bug using https://bugs.swift.org/.
1212
1313## FAQ
1414
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:
1616
1717### Does this mean I can write Android applications in Swift?
1818
@@ -30,32 +30,44 @@ Swift-to-Java bridging.
3030To follow along with this guide, you'll need:
3131
32321 . 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,
3434 please make sure you are able to build for Linux by following the
3535 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),
3739 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
4042 remote debugging in order to deploy built stdlib products to the device. You
4143 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 .
4345
4446## "Hello, world" on Android
4547
46- ### 1. Building the Swift stdlib for Android
48+ ### 1. Building the standalone Swift stdlib for Android
4749
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):
5054
5155```
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
5359$ utils/build-script \
5460 -R \ # Build in ReleaseAssert mode.
5561 --android \ # Build for Android.
5662 --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
5971```
6072
6173### 2. Compiling ` hello.swift ` to run on an Android device
@@ -66,15 +78,16 @@ Create a simple Swift file named `hello.swift`:
6678print (" Hello, Android" )
6779```
6880
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
7082source file, targeting Android:
7183
7284```
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
7588 # The location of the tools used to build Android binaries
7689 -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
7891 -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.
7992 -resource-dir build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift
8093 hello.swift
@@ -111,12 +124,15 @@ $ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswi
111124$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswiftGlibc.so /data/local/tmp
112125$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswiftSwiftOnoneSupport.so /data/local/tmp
113126$ 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
114130```
115131
116132In addition, you'll also need to copy the Android NDK's libc++:
117133
118134```
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
120136```
121137
122138Finally, 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:
157173```
158174$ utils/build-script \
159175 -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).
161177 --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
164180 --android-api-level 21
165181```
182+
183+ This will build the Swift compiler and other host tools first, so expect a much
184+ longer build.
0 commit comments