$30 off During Our Annual Pro Sale. View Details »

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

  2. Ego Slide • Google Developer Expert • 🇻🇳 🇩🇪 🇪🇸

    • Kotlin Weekly publisher • @eenriquelopez • Mainly Android Kotlin, Backend Kotlin, iOS (mostly Swift)
  3. expect fun

  4. Motivation < Costs Sharing code within the company Feature teams

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

    Native
  6. Kotlin Modern language

  7. Kotlin

  8. Kotlin

  9. Kotlin Interop

  10. Kotlin Community support

  11. Kotlin Top-notch tooling

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

  13. Kotlin/Anywhere Android Browser Server

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

    Android Dev JS
  15. Kotlin/Native or KMP or KMM

  16. KMP Sharing architecture (not UI) Shared UI is a mess

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

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

    in Java and Kotlin - Produces bitcode via LLVM API
  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
  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
  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
  23. Mapping

  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
  25. expect/actual

  26. expect/actual

  27. expect/actual

  28. expect/actual JVM

  29. expect/actual JS

  30. expect/actual

  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()
  32. Square JetBrains TouchLab Others

  33. Community projects - Sqldelight - SQLiter - Multiplatform settings -

    Stately - OKIO2
  34. Existing native libraries - Ktor - Kotlinx.Coroutines - Kotlinx.io -

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

  36. ktor

  37. ktor

  38. ktor

  39. ktor

  40. ktor

  41. ktor

  42. Coroutines JetBrains async library

  43. Coroutines

  44. Coroutines

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

  46. kotlinx.io

  47. kotlinx.io

  48. kotlinx.io

  49. AtomicFU Library for atomic operations in Kotlin

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

    instead
  51. AtomicFU

  52. AtomicFU

  53. SQLiter •Evoution of knarch.db 
 •From Touchlab 
 •SQLiter ->

    SQL Driver 
 

  54. SQLiter . DatabaseInstance.

  55. SQLiter

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

  57. Multiplatform settings

  58. kotlinx.serialization

  59. Considerations in state An object belongs to one thread

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

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

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

  63. Considerations in state - Generics

  64. Considerations in state - Generics

  65. Considerations in state - Generics

  66. Considerations in state

  67. Considerations in state Exhaustive enums

  68. Considerations in state Default arguments

  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) { } } }
  70. Considerations in state // Swift Logger.default.log(.error, "An error ocurred") {

    // Closure }
  71. Considerations in state

  72. Considerations in state iOS devs?

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

  74. KMM Survey Q1-Q2 2021

  75. KMM Survey Q1-Q2 2021

  76. KMM Survey Q1-Q2 2021

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

  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 ”
  79. The future @lehtimaeki

  80. Recommendations - Use it with caution - Shared components -

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