This document introduces Swift for Android, describing how Swift code can be compiled to run on Android. It discusses the Swift Android toolchain, how to set it up and build Swift packages for Android. It also covers interfacing Swift code with Java via annotations and JNI, debugging Swift on Android, and the current limitations of the Swift Android compiler. The goal is to enable cross-platform native mobile development with Swift targeting both iOS and Android.
4. Cross-platform native
mobile development
C++ Swift Kotlin Native
Standart library STD + STL Foundation
Korlin Native
Runtime
Memory Manual ARC ARC
Multithreading pthread (for POSIX) GCD Kotlin routines
Build system and
third-party libs
autoconf ->
configure -> make,
CMake, ninja, etc.
Swift package
Manager
Gradle
Objective-C
interoperability
Objective-C++ Just works™ One way
5. Swift Android Compiler
C Frontend
C++ Frontend
ARM Backend
ARM64 Backend
x86 Backend
x86_64 Backend
C
C++
ARM
ARM64
x86
x86_64
Common
Optimizer
Swift FrontendSwift
LLVM IR LLVM IR
6. Current compiler limitation
• Only Mac
• Only armv-7a (or with legacy mode on arm64)
• Android 5.0+
• + 32 Mb to apk file
7. Swift Android Toolchain
git clone https:!//github.com/readdle/swift-android-toolchain
export SWIFT_ANDROID_HOME=$HOME/android/swift-android-4.0d
export PATH=$ANDROID_NDK_HOME:$PATH
export PATH=$SWIFT_ANDROID_HOME/bin:$SWIFT_ANDROID_HOME/build-tools/current:$PATH
wget https:!//dl.bintray.com/readdle/swift-android-toolchain/swift-android-4.0d.zip
swift android tools !--update
OR
PROFILE:
UPDATE ANDROID TOOLS:
12. Swift Android Annotation
Processor
@SwiftValue
public class SampleValue {
@NonNull
public String str1;
@NonNull
public String str2;
@NonNull
public String str3;
!// Swift JNI constructor
public SampleValue() {
}1
}0
public struct SampleValue: Codable {
public var str1: String
public var str2: String
public var str3: String
}2
13. Swift Android Annotation
Processor
public class SampleReference {
public func getRandomValue() !-> SampleValue {
return SampleValue(str1: "1", str2: "2", str3: "3")
}3
}2
@SwiftReference
public class SampleReference {
!// Swift JNI private native pointer
private long nativePointer = 0L;
!// Swift JNI private constructor
private SampleReference() {
}1
!// Swift JNI release method
public native void release();
@NonNull
public native SampleValue getRandomValue();
}0
14. Swift Android Annotation
Processor
public protocol SampleBlockDelegate {
func onCall()
}1
@SwiftDelegate(protocols = {"SampleBlockDelegate"})
public interface SampleInterfaceDelegateAndroid {
@SwiftCallbackFunc
void onCall();
}0
15. ART (JAVA)
NATIVE (SWIFT)
Blueprint Project
git clone https://github.com/andriydruk/swift-android-architecture
Activity
VIEW
VIEW MODEL
REPOSITORY
(+ in-memory cache)
GENERATED
JNI
22. What’s next?
• Swift + Kotlin = ❤
• ARM64 compiler (deadline August 2019)
• X86, X86_64 compiler (Chromebook support)
• LLDB with Swift support: client and server