Slide 1

Slide 1 text

KMP for Mobile Developers Enrique López Mañas

Slide 2

Slide 2 text

Ego Slide • Google Developer Expert • 🇻🇳 🇩🇪 🇪🇸 • Kotlin Weekly publisher • @eenriquelopez • Mainly Android Kotlin, Backend Kotlin, iOS (mostly Swift)

Slide 3

Slide 3 text

expect fun

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Kotlin Modern language

Slide 7

Slide 7 text

Kotlin

Slide 8

Slide 8 text

Kotlin

Slide 9

Slide 9 text

Kotlin Interop

Slide 10

Slide 10 text

Kotlin Community support

Slide 11

Slide 11 text

Kotlin Top-notch tooling

Slide 12

Slide 12 text

Kotlin Sharing is optional! No life or death commitment

Slide 13

Slide 13 text

Kotlin/Anywhere Android Browser Server

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Kotlin/Native or KMP or KMM

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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)

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Mapping

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

expect/actual

Slide 26

Slide 26 text

expect/actual

Slide 27

Slide 27 text

expect/actual

Slide 28

Slide 28 text

expect/actual JVM

Slide 29

Slide 29 text

expect/actual JS

Slide 30

Slide 30 text

expect/actual

Slide 31

Slide 31 text

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()

Slide 32

Slide 32 text

Square JetBrains TouchLab Others

Slide 33

Slide 33 text

Community projects - Sqldelight - SQLiter - Multiplatform settings - Stately - OKIO2

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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. 


Slide 36

Slide 36 text

ktor

Slide 37

Slide 37 text

ktor

Slide 38

Slide 38 text

ktor

Slide 39

Slide 39 text

ktor

Slide 40

Slide 40 text

ktor

Slide 41

Slide 41 text

ktor

Slide 42

Slide 42 text

Coroutines JetBrains async library

Slide 43

Slide 43 text

Coroutines

Slide 44

Slide 44 text

Coroutines

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

kotlinx.io

Slide 47

Slide 47 text

kotlinx.io

Slide 48

Slide 48 text

kotlinx.io

Slide 49

Slide 49 text

AtomicFU Library for atomic operations in Kotlin

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

AtomicFU

Slide 52

Slide 52 text

AtomicFU

Slide 53

Slide 53 text

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


Slide 54

Slide 54 text

SQLiter . DatabaseInstance.

Slide 55

Slide 55 text

SQLiter

Slide 56

Slide 56 text

Multiplatform settings SharedPreferences on Android and NSUserDefaults on iOS.

Slide 57

Slide 57 text

Multiplatform settings

Slide 58

Slide 58 text

kotlinx.serialization

Slide 59

Slide 59 text

Considerations in state An object belongs to one thread

Slide 60

Slide 60 text

Considerations in state Frozen objects can be shared by threads

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

Considerations in state Runtime safety: KMP can verify safe mutability

Slide 63

Slide 63 text

Considerations in state - Generics

Slide 64

Slide 64 text

Considerations in state - Generics

Slide 65

Slide 65 text

Considerations in state - Generics

Slide 66

Slide 66 text

Considerations in state

Slide 67

Slide 67 text

Considerations in state Exhaustive enums

Slide 68

Slide 68 text

Considerations in state Default arguments

Slide 69

Slide 69 text

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) { } } }

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

Considerations in state

Slide 72

Slide 72 text

Considerations in state iOS devs?

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

KMM Survey Q1-Q2 2021

Slide 75

Slide 75 text

KMM Survey Q1-Q2 2021

Slide 76

Slide 76 text

KMM Survey Q1-Q2 2021

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

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 ”

Slide 79

Slide 79 text

The future @lehtimaeki

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

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)

Slide 82

Slide 82 text

Thanks!