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

Kotlin/Native for Multiplatform development

Kotlin/Native for Multiplatform development

Slides of my talk at the AndroidTO. Toronto, 6th November 2018.

Enrique López Mañas

November 07, 2018
Tweet

More Decks by Enrique López Mañas

Other Decks in Programming

Transcript

  1. Kotlin/Native for
    Multiplatform
    Development
    Enrique López Mañas
    Google Developer Expert

    View Slide

  2. Ego Slide
    • Freelance Dev
    • Google Developer Expert
    • @eenriquelopez

    View Slide

  3. Multiplatform development
    < Costs
    Sharing code within the company
    Feature teams
    > Quality

    View Slide

  4. Multiplatform development
    Platform Proprietary
    Hybrid HTML &
    JavaScript Frameworks
    Cross-platform
    Native

    View Slide

  5. Kotlin
    Modern language

    View Slide

  6. Kotlin
    Interop

    View Slide

  7. Kotlin
    Community support

    View Slide

  8. Kotlin
    Top-notch tooling

    View Slide

  9. Kotlin
    Sharing is optional!
    No life or death commitment

    View Slide

  10. Current situation
    Q2 Q3 Q4 Q1 Q2
    v0.7 v0.8
    v0.8.2 v0.9.3

    View Slide

  11. Kotlin/Anywhere
    Android
    Browser
    Server

    View Slide

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

    View Slide

  13. Kotlin/Native
    Sharing architecture (not UI)
    Shared UI is a mess

    View 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 Slide

  15. Kotlin/Native
    Kotlin
    Compiler
    LLVM
    Kotlin Source
    Code (.kt)
    Platform
    Binary
    IR

    View Slide

  16. The compiler
    -Shares Front-end with Kotlin/JVM and Kotlin/JS
    - Written in Java and Kotlin
    - Produces bitcode via LLVM API

    View 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 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 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 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 Slide

  21. Existing native libraries
    - Ktor
    - Kotlinx.Coroutines
    - Kotlinx.io
    - Atomic-fu

    View Slide

  22. Community projects
    - Sqldelight
    - Knarch.db
    - Multiplatform settings
    - Stately
    - OKIO2 (under development)

    View Slide

  23. expect/actual

    View Slide

  24. expect/actual

    View Slide

  25. expect/actual

    View Slide

  26. expect/actual JVM

    View Slide

  27. expect/actual JS

    View Slide

  28. Considerations in state
    An object belongs to one thread

    View Slide

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

    View Slide

  30. Considerations in state
    No threading primitives: synchronized, volatile
    Use atomic-fu instead

    View Slide

  31. Considerations in state
    Runtime safety: Kotlin/Native can verify safe mutability

    View Slide

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

    View Slide

  33. Get started
    git clone https://github.com/JetBrains/kotlin-native
    ./gradlew dependencies:update
    ./gradlew bundle
    kotlinc hello.kt -o hello -opt

    View Slide

  34. 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

  35. Flutter vs Kotlin/Native?
    Flutter - custom UI Widgets
    Dart - better Threading support
    Kotlin - more performant
    Kotlin - better ecosystem

    View Slide

  36. Recommendations
    - Use it with caution
    - Shared components
    - Keep versioning in mind
    - Do not compromise 100% of a project

    View Slide

  37. 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/
    reference/native-overview.html)

    View Slide

  38. View Slide