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 full-size slide

  2. Ego Slide
    • Google Developer Expert



    🇻🇳 🇩🇪 🇪🇸


    • Kotlin Weekly publisher


    • @eenriquelopez


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

    View full-size slide

  3. Motivation
    < Costs
    Sharing code within the company


    Feature teams


    > Quality
    One tech-stack

    View full-size slide

  4. Multiplatform development
    Platform Proprietary
    Hybrid HTML &


    JavaScript Frameworks
    Cross-platform


    Native

    View full-size slide

  5. Kotlin
    Modern language


    View full-size slide

  6. Kotlin
    Interop


    View full-size slide

  7. Kotlin
    Community support


    View full-size slide

  8. Kotlin
    Top-notch tooling


    View full-size slide

  9. Kotlin
    Sharing is optional!


    No life or death commitment


    View full-size slide

  10. Kotlin/Anywhere
    Android
    Browser
    Server


    View full-size slide

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

    View full-size slide

  12. Kotlin/Native or KMP or KMM

    View full-size slide

  13. KMP
    Sharing architecture (not UI)


    Shared UI is a mess


    View full-size slide

  14. 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 full-size slide

  15. Kotlin/Native
    Kotlin
    Compiler
    LLVM
    Kotlin Source


    Code (.kt)
    Platform


    Binary
    IR

    View full-size slide

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


    - Written in Java and Kotlin


    - Produces bitcode via LLVM API

    View full-size slide

  17. 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 full-size slide

  18. 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 full-size slide

  19. 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 full-size slide

  20. 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 full-size slide

  21. expect/actual

    View full-size slide

  22. expect/actual

    View full-size slide

  23. expect/actual

    View full-size slide

  24. expect/actual JVM

    View full-size slide

  25. expect/actual JS

    View full-size slide

  26. expect/actual

    View full-size slide

  27. 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 full-size slide

  28. Square
    JetBrains
    TouchLab Others

    View full-size slide

  29. Community projects
    - Sqldelight


    - SQLiter


    - Multiplatform settings


    - Stately


    - OKIO2

    View full-size slide

  30. Existing native libraries
    - Ktor


    - Kotlinx.Coroutines


    - Kotlinx.io


    - Atomic-fu

    View full-size slide

  31. 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 full-size slide

  32. Coroutines
    JetBrains async library

    View full-size slide

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


    Experimental

    View full-size slide

  34. AtomicFU
    Library for atomic operations in Kotlin

    View full-size slide

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


    Use atomic-fu instead

    View full-size slide

  36. SQLiter
    •Evoution of knarch.db

    •From Touchlab

    •SQLiter -> SQL Driver


    View full-size slide

  37. SQLiter
    .
    DatabaseInstance.

    View full-size slide

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

    View full-size slide

  39. Multiplatform settings

    View full-size slide

  40. kotlinx.serialization

    View full-size slide

  41. Considerations in state
    An object belongs to one thread

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  45. Considerations in state - Generics

    View full-size slide

  46. Considerations in state - Generics

    View full-size slide

  47. Considerations in state - Generics

    View full-size slide

  48. Considerations in state

    View full-size slide

  49. Considerations in state
    Exhaustive enums

    View full-size slide

  50. Considerations in state
    Default arguments

    View full-size slide

  51. 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 full-size slide

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

    View full-size slide

  53. Considerations in state

    View full-size slide

  54. Considerations in state
    iOS devs?


    View full-size slide

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

    View full-size slide

  56. KMM Survey Q1-Q2 2021

    View full-size slide

  57. KMM Survey Q1-Q2 2021

    View full-size slide

  58. KMM Survey Q1-Q2 2021

    View full-size slide

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

    View full-size slide

  60. 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 full-size slide

  61. The future
    @lehtimaeki

    View full-size slide

  62. Recommendations
    - Use it with caution


    - Shared components


    - Keep versioning in mind


    - Do not compromise 100% of a project

    View full-size slide

  63. 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 full-size slide