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

K/N for mobile developers (including libraries' snippets)

Enrique López Mañas
October 21, 2019
34

K/N for mobile developers (including libraries' snippets)

Enrique López Mañas

October 21, 2019
Tweet

Transcript

  1. K/N for Mobile Citizens
    Enrique López Mañas

    View Slide

  2. Ego Slide
    • Freelance Dev
    • Google Developer Expert
    • KotlinWeekly publisher
    • @eenriquelopez
    • Mainly Android Kotlin, Backend Kotlin, iOS (mostly Swift)

    View Slide

  3. Motivation
    < Costs
    Sharing code within the company
    Feature teams
    > Quality
    One tech-stack

    View Slide

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

    View Slide

  5. Kotlin
    Modern language

    View Slide

  6. Kotlin

    View Slide

  7. Kotlin

    View Slide

  8. Kotlin
    Interop

    View Slide

  9. Kotlin
    Community support

    View Slide

  10. Kotlin
    Top-notch tooling

    View Slide

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

    View Slide

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

    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
    Sharing architecture (not UI)
    Shared UI is a mess

    View Slide

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

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

    View Slide

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

    View Slide

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

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

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

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

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

    View Slide

  24. expect/actual

    View Slide

  25. expect/actual

    View Slide

  26. expect/actual

    View Slide

  27. expect/actual JVM

    View Slide

  28. expect/actual JS

    View Slide

  29. Square
    JetBrains
    TouchLab Others

    View Slide

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

    View Slide

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

    View Slide

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

  33. ktor

    View Slide

  34. ktor

    View Slide

  35. ktor

    View Slide

  36. ktor

    View Slide

  37. ktor

    View Slide

  38. Coroutines
    JetBrains async library

    View Slide

  39. Coroutines

    View Slide

  40. Coroutines

    View Slide

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

    View Slide

  42. kotlinx.io

    View Slide

  43. kotlinx.io

    View Slide

  44. kotlinx.io

    View Slide

  45. AtomicFU
    Library for atomic operations in Kotlin

    View Slide

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

    View Slide

  47. AtomicFU

    View Slide

  48. AtomicFU

    View Slide

  49. AtomicFU

    View Slide

  50. SQLiter/SQLager
    •Evoution of knarch.db
    •From Touchlab
    •SQLiter -> SQL Driver
    •SQLager -> SQLite library

    View Slide

  51. SQLiter/SQLager
    .
    DatabaseInstance.

    View Slide

  52. SQLiter/SQLager

    View Slide

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

    View Slide

  54. Multiplatform settings

    View Slide

  55. Multiplatform settings

    View Slide

  56. kotlinx.serialization

    View Slide

  57. Considerations in state
    An object belongs to one thread

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  61. Considerations in state - Generics

    View Slide

  62. Considerations in state - Generics

    View Slide

  63. Considerations in state - Generics

    View Slide

  64. Considerations in state

    View Slide

  65. Considerations in state
    iOS devs?

    View Slide

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

  67. The future

    View Slide

  68. Flutter vs Kotlin/Native?
    Flutter - custom UI Widgets
    Kotlin - more performant
    Kotlin - better ecosystem

    View Slide

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

    View Slide

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

  71. View Slide