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

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

7d67eb78fab223823acad6714cd2d3ca?s=47 Enrique López Mañas
October 21, 2019
26

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

7d67eb78fab223823acad6714cd2d3ca?s=128

Enrique López Mañas

October 21, 2019
Tweet

Transcript

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

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

    KotlinWeekly publisher • @eenriquelopez • Mainly Android Kotlin, Backend Kotlin, iOS (mostly Swift)
  3. Motivation < Costs Sharing code within the company Feature teams

    > Quality One tech-stack
  4. Multiplatform development Platform Proprietary Hybrid HTML & JavaScript Frameworks Cross-platform

    Native
  5. Kotlin Modern language

  6. Kotlin

  7. Kotlin

  8. Kotlin Interop

  9. Kotlin Community support

  10. Kotlin Top-notch tooling

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

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

    v0.9.3
  13. Kotlin/Anywhere Android Browser Server

  14. Kotlin/Native Common JVM Native iOS framework Your iOS dev Your

    Android Dev JS
  15. Kotlin/Native Sharing architecture (not UI) Shared UI is a mess

  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)
  17. Kotlin/Native Kotlin Compiler LLVM Kotlin Source Code (.kt) Platform Binary

    IR
  18. The compiler -Shares Front-end with Kotlin/JVM and Kotlin/JS - Written

    in Java and Kotlin - Produces bitcode via LLVM API
  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
  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
  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
  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
  23. Get started git clone https://github.com/JetBrains/kotlin-native ./gradlew dependencies:update ./gradlew bundle kotlinc-native

    hello.kt -o hello -opt
  24. expect/actual

  25. expect/actual

  26. expect/actual

  27. expect/actual JVM

  28. expect/actual JS

  29. Square JetBrains TouchLab Others

  30. Community projects - Sqldelight - Knarch.db - Multiplatform settings -

    Stately - OKIO2 (under development)
  31. Existing native libraries - Ktor - Kotlinx.Coroutines - Kotlinx.io -

    Atomic-fu
  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.
  33. ktor

  34. ktor

  35. ktor

  36. ktor

  37. ktor

  38. Coroutines JetBrains async library

  39. Coroutines

  40. Coroutines

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

  42. kotlinx.io

  43. kotlinx.io

  44. kotlinx.io

  45. AtomicFU Library for atomic operations in Kotlin

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

    instead
  47. AtomicFU

  48. AtomicFU

  49. AtomicFU

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

    •SQLager -> SQLite library
  51. SQLiter/SQLager . DatabaseInstance.

  52. SQLiter/SQLager

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

  54. Multiplatform settings

  55. Multiplatform settings

  56. kotlinx.serialization

  57. Considerations in state An object belongs to one thread

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

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

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

  61. Considerations in state - Generics

  62. Considerations in state - Generics

  63. Considerations in state - Generics

  64. Considerations in state

  65. Considerations in state iOS devs?

  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 ”
  67. The future

  68. Flutter vs Kotlin/Native? Flutter - custom UI Widgets Kotlin -

    more performant Kotlin - better ecosystem
  69. Recommendations - Use it with caution - Shared components -

    Keep versioning in mind - Do not compromise 100% of a project
  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)
  71. None