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

KMP for Mobile Developers

KMP for Mobile Developers

KMP for Mobile Developers @ Droidcon Berlin 2021

Enrique López Mañas

November 02, 2021
Tweet

More Decks by Enrique López Mañas

Other Decks in Programming

Transcript

  1. KMP for Mobile
    Developers
    Enrique López Mañas

    View Slide

  2. Ego Slide
    • Google Developer Expert



    🇻🇳 🇩🇪 🇪🇸


    • Kotlin Weekly publisher


    • @eenriquelopez


    • Mainly Android Kotlin, Backend Kotlin, iOS (mostly Swift)

    View Slide

  3. expect fun

    View Slide

  4. Motivation
    < Costs
    Sharing code within the company


    Feature teams


    > Quality
    One tech-stack

    View Slide

  5. Multiplatform development
    Platform Proprietary
    Hybrid HTML &


    JavaScript Frameworks
    Cross-platform


    Native

    View Slide

  6. Kotlin
    Modern language


    View Slide

  7. Kotlin

    View Slide

  8. Kotlin

    View Slide

  9. Kotlin
    Interop


    View Slide

  10. Kotlin
    Community support


    View Slide

  11. Kotlin
    Top-notch tooling


    View Slide

  12. Kotlin
    Sharing is optional!


    No life or death commitment


    View Slide

  13. Kotlin/Anywhere
    Android
    Browser
    Server


    View Slide

  14. Kotlin/Native
    Common
    JVM Native
    iOS
    framework
    Your iOS
    dev
    Your
    Android
    Dev
    JS

    View Slide

  15. Kotlin/Native or KMP or KMM

    View Slide

  16. KMP
    Sharing architecture (not UI)


    Shared UI is a mess


    View Slide

  17. Kotlin/Native
    - Uses LLVM (5.0)


    - Provides runtime guarantees


    - Exceptions, memory management


    - Interop with C/Objective-C (Swift)


    - Platform libraries (POSIX, Apple Frameworks, Win32, DOM, etc)

    View Slide

  18. Kotlin/Native
    Kotlin
    Compiler
    LLVM
    Kotlin Source


    Code (.kt)
    Platform


    Binary
    IR

    View Slide

  19. The compiler
    -Shares Front-end with Kotlin/JVM and Kotlin/JS


    - Written in Java and Kotlin


    - Produces bitcode via LLVM API

    View Slide

  20. Memory Management
    - ARC with Cycle Collector


    - When working pure Kotlin,don’t worry about memory management


    - Weak references supported


    - Memory sharing model


    - Different threads have disjoint object graphs


    - Object subgraphs can be transferred between threads


    - Immutable objects can be shared

    View Slide

  21. Interoperatibility
    - Interoperatibility with C, Objective-C and Swift


    - Kotlin can call C/Objective-C and vice-versa


    - Kotlin can extend Objective-C classes and vice-versa


    - Memory management aware of Objective-C runtime

    View Slide

  22. Mapping
    - Numbers are kept


    - Strings converted


    - Kotlin declarations wrapping C entities (functions, struct, unions, etc.) are auto-
    generated


    - Objective-C OO concepts (classes, protocols, blocks) are represented as
    matching Kotlin entities (classes, interfaces, lambdas)


    - For Objective-C, Kotlin code can be compiled to a framework

    View Slide

  23. Mapping

    View Slide

  24. Kotlin Library
    - Hold collection of code for reusability and sharing


    - Own format: ‘klib’ extension, which holds metadata and bitcode


    - Tool for creating and storing libraries in repositories

    View Slide

  25. expect/actual

    View Slide

  26. expect/actual

    View Slide

  27. expect/actual

    View Slide

  28. expect/actual JVM

    View Slide

  29. expect/actual JS

    View Slide

  30. expect/actual

    View Slide

  31. expect/actual
    // Common


    expect fun randomUUID(): String


    // Android


    import java.util.*


    actual fun randomUUID() = UUID.randomUUID().toString()


    // iOS


    import platform.Foundation.NSUUID


    actual fun randomUUID(): String = NSUUID().UUIDString()

    View Slide

  32. Square
    JetBrains
    TouchLab Others

    View Slide

  33. Community projects
    - Sqldelight


    - SQLiter


    - Multiplatform settings


    - Stately


    - OKIO2

    View Slide

  34. Existing native libraries
    - Ktor


    - Kotlinx.Coroutines


    - Kotlinx.io


    - Atomic-fu

    View Slide

  35. ktor
    - Web application framework

    -Domain Specific Language (DSL) syntax for web apps-Kotlin coroutines for
    asynchronous programming

    -Can be used on web, iOS and Android

    -Provides a unified toolset with a single language, like Node.js but with type-safety
    and build-concurrency.

    View Slide

  36. ktor

    View Slide

  37. ktor

    View Slide

  38. ktor

    View Slide

  39. ktor

    View Slide

  40. ktor

    View Slide

  41. ktor

    View Slide

  42. Coroutines
    JetBrains async library

    View Slide

  43. Coroutines

    View Slide

  44. Coroutines

    View Slide

  45. kotlinx.io
    Library for I/O primitives building and manipulations


    Experimental

    View Slide

  46. kotlinx.io

    View Slide

  47. kotlinx.io

    View Slide

  48. kotlinx.io

    View Slide

  49. AtomicFU
    Library for atomic operations in Kotlin

    View Slide

  50. Considerations in state
    No threading primitives: synchronized, volatile


    Use atomic-fu instead

    View Slide

  51. AtomicFU

    View Slide

  52. AtomicFU

    View Slide

  53. SQLiter
    •Evoution of knarch.db

    •From Touchlab

    •SQLiter -> SQL Driver


    View Slide

  54. SQLiter
    .
    DatabaseInstance.

    View Slide

  55. SQLiter

    View Slide

  56. Multiplatform settings
    SharedPreferences on Android and NSUserDefaults on iOS.

    View Slide

  57. Multiplatform settings

    View Slide

  58. kotlinx.serialization

    View Slide

  59. Considerations in state
    An object belongs to one thread

    View Slide

  60. Considerations in state
    Frozen objects can be shared by threads

    View Slide

  61. Concurrency - frozen objects
    Everything you have written until now is not frozen

    View Slide

  62. Considerations in state
    Runtime safety: KMP can verify safe mutability

    View Slide

  63. Considerations in state - Generics

    View Slide

  64. Considerations in state - Generics

    View Slide

  65. Considerations in state - Generics

    View Slide

  66. Considerations in state

    View Slide

  67. Considerations in state
    Exhaustive enums

    View Slide

  68. Considerations in state
    Default arguments

    View Slide

  69. Considerations in state
    // Kotlin
    enum class LogLevel {
    ERROR,
    WARNING,
    INFO,
    DEBUG
    }
    class Logger {
    companion object default {
    fun log(level: LogLevel = LogLevel.ERROR, message: String, completion: (Boolean) ->
    Unit) { }
    }
    }

    View Slide

  70. Considerations in state
    // Swift
    Logger.default.log(.error, "An error ocurred") {
    // Closure
    }

    View Slide

  71. Considerations in state

    View Slide

  72. Considerations in state
    iOS devs?


    View Slide

  73. Architecture
    https://proandroiddev.com/kotlin-multiplatform-mvvm-clean-architecture-f20b99f90b95
    Javier Arroyo

    View Slide

  74. KMM Survey Q1-Q2 2021

    View Slide

  75. KMM Survey Q1-Q2 2021

    View Slide

  76. KMM Survey Q1-Q2 2021

    View Slide

  77. Case studies?
    https://kotlinlang.org/lp/mobile/case-studies/

    View Slide

  78. The future
    Hard to make predictions, easier to bet safe
    “I don’t know what will happen in 10 years, but there will


    be people asking in SO about regular expressions, and


    there will be another JS framework ”

    View Slide

  79. The future
    @lehtimaeki

    View Slide

  80. Recommendations
    - Use it with caution


    - Shared components


    - Keep versioning in mind


    - Do not compromise 100% of a project

    View Slide

  81. Further resources
    Kotlin Slack (kotlinlang.slack.com)
    Touchlab resources (https://github.com/touchlab)
    Kotlin Weekly (http://kotlinweekly.net)
    K/N documentation (https://kotlinlang.org/docs/
    native-overview.html)

    View Slide

  82. Thanks!

    View Slide