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

Kotlin Multiplatform

Kotlin Multiplatform

Introduction to Kotlin Multiplatform that I gave at Kotlin Everywhere in Minneapolis on October 5, 2019

Bryan Herbst

October 05, 2019
Tweet

More Decks by Bryan Herbst

Other Decks in Technology

Transcript

  1. Kotlin Multiplatform
    Bryan Herbst

    View full-size slide

  2. Beware: Kotlin MPP
    is under development

    View full-size slide

  3. Why cross-platform?

    View full-size slide

  4. More platforms =
    More developers
    More money
    More code
    More bugs

    View full-size slide

  5. More platforms =
    More developers
    More money
    More code
    More bugs

    View full-size slide

  6. More platforms =
    More developers
    More money
    More code
    More bugs

    View full-size slide

  7. More platforms =
    More developers
    More money
    More code
    More bugs

    View full-size slide

  8. Fewer platforms =
    Fewer developers?
    Less money?
    Less code?
    Fewer bugs?

    View full-size slide

  9. User Experience is table
    stakes

    View full-size slide

  10. User Experience is table
    stakes
    How native is the UI?
    How responsive is the app?

    View full-size slide

  11. User Experience is table
    stakes
    How native is the UI?
    How responsive is the app?

    View full-size slide

  12. Developer happiness is critical

    View full-size slide

  13. Developer happiness is critical
    Speed of platform updates
    Interop with native
    Interop with existing code
    Language

    View full-size slide

  14. Developer happiness is critical
    Speed of platform updates
    Interop with native
    Interop with existing code
    Language

    View full-size slide

  15. Developer happiness is critical
    Speed of platform updates
    Interop with native
    Interop with existing code
    Language

    View full-size slide

  16. Developer happiness is critical
    Speed of platform updates
    Interop with native
    Interop with existing code
    Language

    View full-size slide

  17. Cross-platform architecture

    View full-size slide

  18. What to share?

    View full-size slide

  19. As much (or as little)
    as you want

    View full-size slide

  20. Embrace platform differences,
    but provide a consistent core

    View full-size slide

  21. Assumption: you already
    have an iOS & Android app

    View full-size slide

  22. Feature
    UI
    Logic
    Data

    View full-size slide

  23. Feature
    UI
    Logic
    Data
    Data models,
    Networking

    View full-size slide

  24. Feature
    UI
    Logic
    Data
    Great place to
    start!

    View full-size slide

  25. Feature
    UI
    Logic
    Data
    ViewControllers,
    ViewModels

    View full-size slide

  26. Feature
    UI
    Logic
    Data
    Trickier

    View full-size slide

  27. Feature
    UI
    Logic
    Data
    Layouts, Views

    View full-size slide

  28. Feature
    UI
    Logic
    Data
    Probably not
    worth it

    View full-size slide

  29. What is platform dependent?

    View full-size slide

  30. What is platform dependent?
    Almost everything the user sees

    View full-size slide

  31. What is platform dependent?
    Logging

    View full-size slide

  32. What is platform dependent?
    Disk storage

    View full-size slide

  33. What is platform dependent?
    Networking

    View full-size slide

  34. Shared Library
    Common
    Android iOS

    View full-size slide

  35. Shared Library
    Common
    Android iOS
    Android
    App
    iOS
    App

    View full-size slide

  36. Shared Library
    Common
    Android iOS
    Android
    App
    iOS
    App
    lib lib

    View full-size slide

  37. Mono Repo
    Common
    Android iOS
    Android
    App
    iOS
    App
    lib lib

    View full-size slide

  38. Shared Library
    Common
    Android iOS
    Android
    App
    iOS
    App
    lib lib

    View full-size slide

  39. How Kotlin MPP works

    View full-size slide

  40. kotlinc
    Main.kt

    View full-size slide

  41. kotlinc
    Frontend
    Intermediate
    representation
    Backend

    View full-size slide

  42. kotlinc
    Frontend
    Intermediate
    representation
    Backend

    View full-size slide

  43. kotlinc
    Frontend
    Intermediate
    representation
    Backend

    View full-size slide

  44. kotlinc
    Frontend
    Intermediate
    representation
    Kotlin JVM

    View full-size slide

  45. kotlinc
    Frontend
    Intermediate
    representation
    Kotlin JS

    View full-size slide

  46. kotlinc
    Frontend
    Intermediate
    representation
    Kotlin Native

    View full-size slide

  47. Kotlin JVM
    Output is bytecode

    View full-size slide

  48. Kotlin JVM
    100% interoperability

    View full-size slide

  49. Kotlin JS
    Output is JS

    View full-size slide

  50. Kotlin JS
    Output is JS

    View full-size slide

  51. Kotlin JS - libraries
    Dukat binds strongly-typed libraries

    View full-size slide

  52. Kotlin JS - libraries
    dynamic type supports loose typing

    View full-size slide

  53. Dynamic
    val dyn: dynamic = ...

    View full-size slide

  54. Dynamic
    Disables type checking

    View full-size slide

  55. Dynamic
    Can call any property or function

    View full-size slide

  56. Kotlin Native
    Outputs native binary

    View full-size slide

  57. Kotlin Native
    Outputs native binary
    Executable

    View full-size slide

  58. Kotlin Native
    Outputs native binary
    Executable
    C library

    View full-size slide

  59. Kotlin Native
    Outputs native binary
    Executable
    C library
    Apple framework

    View full-size slide

  60. Kotlin Native
    Compiled with LLVM

    View full-size slide

  61. Kotlin Native
    arm32, arm64
    x86, x86_64
    mingw x86_64
    MIPS, rPi
    wasm32

    View full-size slide

  62. Native libraries
    1. Create .def
    2. Cinterop creates kotlin bindings of
    C/C++ code

    View full-size slide

  63. Native libraries
    1. Create .def
    2. cinterop produces Kotlin bindings

    View full-size slide

  64. Native libraries
    Platform libraries

    View full-size slide

  65. Native libraries
    Platform libraries
    e.g. Open GL, objc, posix

    View full-size slide

  66. Kotlin Multiplatform

    View full-size slide

  67. Main.kt Frontend IR
    JVM
    Native
    JS

    View full-size slide

  68. Let’s get started!

    View full-size slide

  69. plugins {
    id 'org.jetbrains.kotlin.multiplatform'
    version '1.3.50’
    }

    View full-size slide

  70. kotlin {
    jvm() // Creates a JVM target
    }

    View full-size slide

  71. kotlin {
    iosArm32("ios32")
    iosArm64("ios64")
    configure([ios32, ios64]) {
    binaries.framework {
    baseName = "SharedCode"
    }
    }
    }

    View full-size slide

  72. kotlin {
    iosArm32("ios32")
    iosArm64("ios64")
    configure([ios32, ios64]) {
    binaries.framework {
    baseName = "SharedCode"
    }
    }
    }

    View full-size slide

  73. kotlin {
    iosArm32("ios32")
    iosArm64("ios64")
    // Simulator
    iosX64("iosX64")
    }

    View full-size slide

  74. val sdk = System.getenv("SDK_NAME")
    if (sdk?.startsWith("iphoneos")) {
    iosX64("iosX64")
    } else {
    //....
    }

    View full-size slide

  75. Carthage or CocoaPods

    View full-size slide

  76. plugins {
    id("org.jetbrains.kotlin.native.cocoapods")
    }
    kotlin {
    cocoapods {
    summary = "Shared library"
    homepage = "http://github.com/shared-lib"
    }
    }

    View full-size slide

  77. Time to write some code

    View full-size slide

  78. Android
    Notes UI
    NotesView
    Model
    Note (model)
    Note API
    iOS
    Notes UI
    NotesView
    Controller
    Note (model)
    Note API

    View full-size slide

  79. Android
    Notes UI
    NotesView
    Model
    Note API
    iOS
    Notes UI
    NotesView
    Controller
    Note API
    Shared
    Note (model)

    View full-size slide

  80. Note JSON
    {
    content: "Hello",
    status: "Pinned"
    }

    View full-size slide

  81. iOS
    struct Note {
    let content: String
    let status: String
    }
    Android
    data class Note(
    val content: String,
    val status: Status
    )
    enum class Status { }

    View full-size slide

  82. iOS
    struct Note {
    let content: String
    let status: String
    }
    Android
    data class Note(
    val content: String,
    val status: Status
    )
    enum class Status { }

    View full-size slide

  83. iOS
    struct Note {
    let content: String
    let status: String
    }
    Android
    data class Note(
    val content: String,
    val status: Status
    )
    enum class Status { }

    View full-size slide

  84. data class Note(
    val content: String,
    val status: String
    )
    Shared

    View full-size slide

  85. data class Note(
    val content: String,
    val status: String
    )
    Android

    View full-size slide

  86. __attribute__((objc_subclassing_restricted))
    __attribute__((swift_name("Note")))
    @interface SharedNote : KotlinBase
    @property (readonly) NSString * content;
    @property (readonly) NSString * status;
    @end;
    iOS

    View full-size slide

  87. __attribute__((objc_subclassing_restricted))
    __attribute__((swift_name("Note")))
    @interface SharedNote : KotlinBase
    @property (readonly) NSString * content;
    @property (readonly) NSString * status;
    @end;
    iOS

    View full-size slide

  88. __attribute__((objc_subclassing_restricted))
    __attribute__((swift_name("Note")))
    @interface SharedNote : KotlinBase
    @property (readonly) NSString * content;
    @property (readonly) NSString * status;
    @end;
    iOS – Objective C

    View full-size slide

  89. class Note: KotlinBase {
    private(set) var content: String?
    private(set) var status: String?
    }
    iOS – Swift

    View full-size slide

  90. Android
    Notes UI
    NotesView
    Model
    Note API
    iOS
    Notes UI
    NotesView
    Controller
    Note API
    Shared
    Note (model)

    View full-size slide

  91. Android
    Notes UI
    NotesView
    Model
    iOS
    Notes UI
    NotesView
    Controller
    Shared
    Note (model)
    Note API

    View full-size slide

  92. Serialization
    Kotlinx.serialization

    View full-size slide

  93. @Serializable
    data class Note(
    val content: String
    val status: String
    )

    View full-size slide

  94. Json.nonstrict
    .stringify(
    Note.serializer(),
    note
    )

    View full-size slide

  95. Json.nonstrict
    .parse(
    Note.serializer(),
    jsonString
    )

    View full-size slide

  96. Json.nonstrict
    .parse(
    Note.serializer().list,
    jsonString
    )

    View full-size slide

  97. Serialization is multiplatform!

    View full-size slide

  98. Serialization is multiplatform!
    Charsets, input streams, buffers

    View full-size slide

  99. commonMain {
    dependencies {
    implementation "serialization-runtime-common"
    }
    }

    View full-size slide

  100. jvmMain {
    dependencies {
    implementation "serialization-runtime"
    }
    }

    View full-size slide

  101. nativeMain {
    dependencies {
    implementation "serialization-runtime-native"
    }
    }

    View full-size slide

  102. Networking
    KTOR

    View full-size slide

  103. Under the hood - engines
    Android: OkHttp, HttpUrlConnection

    View full-size slide

  104. Under the hood - engines
    Android: OkHttp, HttpUrlConnection
    iOS: NSUrlSession

    View full-size slide

  105. class NoteApi() {
    private val client = HttpClient()
    suspend fun fetchNote(): String
    = client.get("http://...")
    }

    View full-size slide

  106. How do we support
    different platforms?

    View full-size slide

  107. common/PlatformName.kt
    expect fun getPlatform(): String

    View full-size slide

  108. android/PlatformName.kt
    actual fun getPlatform()= "Android"

    View full-size slide

  109. ios/PlatformName.kt
    actual fun getPlatform() = "iOS"

    View full-size slide

  110. Common/KeyValueStore.kt
    expect class KeyValueStore() {
    fun setString(key: String, value: String)
    fun getString(key: String): String
    }

    View full-size slide

  111. android/KeyValueStore.kt
    actual class KeyValueStore() {
    private val prefs: SharedPreferences
    actual fun setString() //...
    actual fun getString() //...
    }

    View full-size slide

  112. ios/KeyValueStore.kt
    actual class KeyValueStore() {
    private val userDefaults: NSUserDefaults
    actual fun setString() //...
    actual fun getString() //...
    }

    View full-size slide

  113. expect interface Closeable {
    fun close()
    }
    actual typealias Closeable = java.io.Closeable

    View full-size slide

  114. Platform differences

    View full-size slide

  115. Unsupported in Swift/Obj-C
    Suspend functions
    Inline classes

    View full-size slide

  116. Asyncronous work

    View full-size slide

  117. Coroutines
    Only single-threaded code is currently
    supported for Kotlin/Native

    View full-size slide

  118. Exceptions
    All Kotlin exceptions are unchecked

    View full-size slide

  119. Exceptions
    All Kotlin exceptions are unchecked
    Swift only has checked errors

    View full-size slide

  120. Exceptions
    Annotate with @Throws

    View full-size slide

  121. Primitive types
    Int -> KotlinInt

    View full-size slide

  122. Primitive types
    Int -> KotlinInt (NSNumber)

    View full-size slide

  123. Generics
    Experimental for Kotlin Native

    View full-size slide

  124. Generics
    Experimental for Kotlin Native
    extraOpts "-Xobjc-generics"

    View full-size slide

  125. Kotlin:
    class Generic
    Swift:
    class Generic

    View full-size slide

  126. Kotlin:
    class Generic
    Swift:
    class Generic

    View full-size slide

  127. Kotlin:
    class Generic(val item: T)
    Swift:
    class Generic {
    let item: T?
    }

    View full-size slide

  128. Kotlin:
    class Generic(val item: T)
    Swift:
    class Generic {
    let item: T?
    }
    ?

    View full-size slide

  129. Kotlin:
    class Generic(val item: T)
    Swift:
    class Generic {
    let item: T
    }

    View full-size slide