Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Kotlin Modern language

Slide 6

Slide 6 text

Kotlin

Slide 7

Slide 7 text

Kotlin

Slide 8

Slide 8 text

Kotlin Interop

Slide 9

Slide 9 text

Kotlin Community support

Slide 10

Slide 10 text

Kotlin Top-notch tooling

Slide 11

Slide 11 text

Kotlin Sharing is optional! No life or death commitment

Slide 12

Slide 12 text

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

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

Slide 16

Slide 16 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 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 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 20

Slide 20 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 21

Slide 21 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 22

Slide 22 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 23

Slide 23 text

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

Slide 24

Slide 24 text

expect/actual

Slide 25

Slide 25 text

expect/actual

Slide 26

Slide 26 text

expect/actual

Slide 27

Slide 27 text

expect/actual JVM

Slide 28

Slide 28 text

expect/actual JS

Slide 29

Slide 29 text

Square JetBrains TouchLab Others

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 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 33

Slide 33 text

ktor

Slide 34

Slide 34 text

ktor

Slide 35

Slide 35 text

ktor

Slide 36

Slide 36 text

ktor

Slide 37

Slide 37 text

ktor

Slide 38

Slide 38 text

Coroutines JetBrains async library

Slide 39

Slide 39 text

Coroutines

Slide 40

Slide 40 text

Coroutines

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

kotlinx.io

Slide 43

Slide 43 text

kotlinx.io

Slide 44

Slide 44 text

kotlinx.io

Slide 45

Slide 45 text

AtomicFU Library for atomic operations in Kotlin

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

AtomicFU

Slide 48

Slide 48 text

AtomicFU

Slide 49

Slide 49 text

AtomicFU

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

SQLiter/SQLager . DatabaseInstance.

Slide 52

Slide 52 text

SQLiter/SQLager

Slide 53

Slide 53 text

Multiplatform settings SharedPreferences on Android and NSUserDefaults on iOS.

Slide 54

Slide 54 text

Multiplatform settings

Slide 55

Slide 55 text

Multiplatform settings

Slide 56

Slide 56 text

kotlinx.serialization

Slide 57

Slide 57 text

Considerations in state An object belongs to one thread

Slide 58

Slide 58 text

Considerations in state Frozen objects can be shared by threads

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

Considerations in state - Generics

Slide 62

Slide 62 text

Considerations in state - Generics

Slide 63

Slide 63 text

Considerations in state - Generics

Slide 64

Slide 64 text

Considerations in state

Slide 65

Slide 65 text

Considerations in state iOS devs?

Slide 66

Slide 66 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 67

Slide 67 text

The future

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

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

Slide 71

Slide 71 text

No content