Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introducation to Swift for Android

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Introducation to Swift for Android

Avatar for Andriy Druk

Andriy Druk

March 30, 2018
Tweet

Other Decks in Programming

Transcript

  1. 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
  2. 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 Frontend Swift LLVM IR LLVM IR
  3. Current compiler limitation • Only Mac • Only armv-7a (or

    with legacy mode on arm64) • Android 5.0+ • + 32 Mb to apk file
  4. 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:
  5. Swift Android Toolchain !// project.gradle buildscript { repositories { maven

    { url "https:!//dl.bintray.com/readdle/maven" } !// … }2 dependencies { classpath 'com.readdle.android.swift:gradle:1.1.3' !// … }1 }0 !// app.gradle apply plugin: ‘com.readdle.android.swift' swift { cleanEnabled true debug { extraBuildFlags("-Xswiftc", "-DDEBUG") } }
  6. Java Interoperability public func swiftFunc(str: String) !-> String? @_silgen_name("Java_com_readdle_swiftjava_sample_Sample_func") public

    func Java_com_readdle_swiftjava_sample_Sample_func(env: UnsafeMutablePointer<JNIEnv?>, javaString: jstring) !-> jstring? { let swiftString: String do { swiftString = try String.from(javaObject: this) }4 catch { _ = JNI.api.ThrowNew(JNI.env, SwiftRuntimeErrorClass, "Error") return nil }3 let result = swiftFunc(str: swiftString) do { return try result!?.javaObject() }2 catch { _ = JNI.api.ThrowNew(JNI.env, SwiftRuntimeErrorClass, "Error") return nil }1 }0
  7. 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
  8. 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
  9. Swift Android Annotation Processor public protocol SampleBlockDelegate { func onCall()

    }1 @SwiftDelegate(protocols = {"SampleBlockDelegate"}) public interface SampleInterfaceDelegateAndroid { @SwiftCallbackFunc void onCall(); }0
  10. XCTest compile packages compile tests copy binaries on Android >>>

    run executable copy resources (optional) >>> copy results (optional) <<<
  11. What’s next? • Swift + Kotlin = ❤ • ARM64

    compiler (deadline August 2019) • X86, X86_64 compiler (Chromebook support) • LLDB with Swift support: client and server