Slide 1

Slide 1 text

Kotlin Multiplatform In Production Kevin Galligan

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Touchlab

Slide 4

Slide 4 text

Community community!

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

What is Kotlin Multiplatform?

Slide 7

Slide 7 text

kot·lin mul·ti·plat·form /ˌkätˈlin məltiˈplatfôrm,ˌkätˈlin məltīˈplatfôrm/ noun noun: kotlin multiplatform 1.optional, natively-integrated, open-source, code sharing platform, based on the popular, modern language kotlin. facilitates non-ui logic availability on many platforms.

Slide 8

Slide 8 text

Optional Sharing Low risk. No Big Decisions.

Slide 9

Slide 9 text

Natively Integrated smooth interop

Slide 10

Slide 10 text

open source

Slide 11

Slide 11 text

Code Sharing not “cross platform”

Slide 12

Slide 12 text

popular

Slide 13

Slide 13 text

popular

Slide 14

Slide 14 text

popular

Slide 15

Slide 15 text

language & tools

Slide 16

Slide 16 text

modern

Slide 17

Slide 17 text

Not UI well, not necessarily UI

Slide 18

Slide 18 text

Shared UI == Failure!

Slide 19

Slide 19 text

Shared Logic == Computers

Slide 20

Slide 20 text

Many Platforms

Slide 21

Slide 21 text

Kotlin

Slide 22

Slide 22 text

Kotlin JVM

Slide 23

Slide 23 text

Kotlin JVM JS

Slide 24

Slide 24 text

Kotlin JVM JS Native

Slide 25

Slide 25 text

Kotlin JVM JS Native iOS Mac Linux Windows Android/NDK Others…

Slide 26

Slide 26 text

Kotlin JVM JS Native iOS Mac Linux Windows Android/NDK Others… Webassembly

Slide 27

Slide 27 text

Kotlin JVM JS Native iOS Mac Linux Windows Android/NDK Others… Webassembly Many Platforms

Slide 28

Slide 28 text

Why Kotlin? High Efficiency Low Risk Modern Language/Tools Highly Engaged Community

Slide 29

Slide 29 text

mobile is easy

Slide 30

Slide 30 text

mobile is easy

Slide 31

Slide 31 text

Status

Slide 32

Slide 32 text

Q3 Q2 Q4 Q1 Q2 2018 2019 0 .6 v0.7 v0.8 v0.8.2 v0.9.3 IDE tooling! Coroutines?

Slide 33

Slide 33 text

Q3 Q2 Q4 Q1 Q2 2018 2019 0 .6 v0.7 v0.8 v0.8.2 v0.9.3 IDE tooling! Coroutines? K/N 1.0, Kotlin 1.3 Gradle 4.10+ Android Studio

Slide 34

Slide 34 text

Q3 Q2 Q4 Q1 Q2 2018 2019 0 .6 v0.7 v0.8 v0.8.2 v0.9.3 IDE tooling! Coroutines? K/N 1.0, Kotlin 1.3 Gradle 4.10+ Android Studio Other samples/libraries Production deployments Compiler plugins! MT Coroutines!

Slide 35

Slide 35 text

Q3 Q2 Q4 Q1 Q2 2018 2019 0 .6 v0.7 v0.8 v0.8.2 v0.9.3 IDE tooling! Coroutines? K/N 1.0, Kotlin 1.3 Gradle 4.10+ Android Studio Compiler plugins! Other samples/libraries Production deployments Paid license/debugger Reactive Library Big Production Apps Webassembly stuff? MT Coroutines!

Slide 36

Slide 36 text

TL;DR March start getting real end of Q1

Slide 37

Slide 37 text

SHARED CODE FOR ANDROID & IOS

Slide 38

Slide 38 text

Common

Slide 39

Slide 39 text

Common mainThread?

Slide 40

Slide 40 text

expect val mainThread:Boolean

Slide 41

Slide 41 text

expect val mainThread:Boolean actual val mainThread: Boolean get() = Looper.myLooper() === Looper.getMainLooper()

Slide 42

Slide 42 text

expect val mainThread:Boolean actual val mainThread: Boolean get() = Looper.myLooper() === Looper.getMainLooper() actual val mainThread: Boolean get() = NSThread.isMainThread()

Slide 43

Slide 43 text

expect val mainThread:Boolean actual val mainThread: Boolean get() = Looper.myLooper() === Looper.getMainLooper() actual val mainThread: Boolean get() = NSThread.isMainThread() actual val mainThread: Boolean = true

Slide 44

Slide 44 text

expect fun currentTimeMillis():Long expect fun backgroundTask(backJob:()-> B, mainJob:(B) -> Unit) expect fun backgroundTask(backJob:()->Unit) expect fun networkBackgroundTask(backJob:()->Unit) expect fun initContext():NativeOpenHelperFactory expect fun goFreeze(a:T):T expect fun T.freeze2(): T expect fun simpleGet(url:String):String expect fun logException(t:Throwable) expect fun settingsFactory(): Settings.Factory expect fun createUuid():String

Slide 45

Slide 45 text

expect class Date { fun toLongMillis():Long } expect class DateFormatHelper(format:String){ fun toDate(s:String):Date fun format(d:Date):String }

Slide 46

Slide 46 text

actual class Date(val date:java.util.Date) { actual fun toLongMillis(): Long = date.time } actual class DateFormatHelper actual constructor(format: String) { val dateFormatter = object : ThreadLocal(){ override fun initialValue(): DateFormat = SimpleDateFormat(format) } actual fun toDate(s: String): Date = Date(dateFormatter.get()!!.parse(s)) actual fun format(d: Date): String = dateFormatter.get()!!.format(d.date) }

Slide 47

Slide 47 text

fun initPlatformClient( staticFileLoader: (filePrefix: String, fileType: String) -> String?, analyticsCallback: (name: String, params: Map) -> Unit, clLogCallback: (s: String) -> Unit) {

Slide 48

Slide 48 text

fun initPlatformClient( staticFileLoader: (filePrefix: String, fileType: String) -> String?, analyticsCallback: (name: String, params: Map) -> Unit, clLogCallback: (s: String) -> Unit) { AppContext.initPlatformClient ({filePrefix, fileType -> loadAsset("${filePrefix}.${fileType}")}, {name: String, params: Map -> val event = CustomEvent(name) //Loop Answers.getInstance().logCustom(event) }, { Log.w("MainApp", it) })

Slide 49

Slide 49 text

let appContext = AppContext() appContext.doInitPlatformClient(staticFileLoader: loadAsset, analyticsCallback: analyticsCallback, clLogCallback: csLog) func loadAsset(filePrefix:String, fileType:String) -> String?{ do{ let bundleFile = Bundle.main.path(forResource: filePrefix, ofType: fileType) return try String(contentsOfFile: bundleFile!) } catch { return nil } }

Slide 50

Slide 50 text

Common

Slide 51

Slide 51 text

JVM Native Common

Slide 52

Slide 52 text

JVM Native Common Framework

Slide 53

Slide 53 text

JVM Native Common Framework

Slide 54

Slide 54 text

JVM Native Common Android Stuff Framework iOS Stuff

Slide 55

Slide 55 text

Libraries!

Slide 56

Slide 56 text

Jetbrains • Ktor • Kotlinx.Coroutines • Kotlinx.io • Kotlinx.serialization • Atomic-fu (maybe?)

Slide 57

Slide 57 text

Community • Sqldelight • Knarch.db • Multiplatform Settings • Stately • OKIO2 (developing) • Timber (sort of)

Slide 58

Slide 58 text

Community • Sqldelight • Knarch.db • Multiplatform Settings • Stately • OKIO2 (developing) • Timber (sort of)

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

SQLiter trim, lightly opinionated SQLite driver

Slide 61

Slide 61 text

SQLit trim, lightly opinionated SQLite driver

Slide 62

Slide 62 text

Targets Native SQLite

Slide 63

Slide 63 text

Targets Native SQLite Native SQL Encrypted

Slide 64

Slide 64 text

Targets Native SQLite Native SQL Encrypted Android SQLite

Slide 65

Slide 65 text

Targets Native SQLite Native SQL Encrypted Android SQLite Android SQL Encrypted

Slide 66

Slide 66 text

Targets Native SQLite Native SQL Encrypted Android SQLite Android SQL Encrypted JS?

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

Public Service Announcement if you understand anything about native…

Slide 70

Slide 70 text

STATE

Slide 71

Slide 71 text

3 Ecosystems JVM, JS, and Native

Slide 72

Slide 72 text

Kotlin/Native State Rules

Slide 73

Slide 73 text

Rule #1 Live state belongs to 1 thread

Slide 74

Slide 74 text

Rule #2 Frozen state can be shared by threads

Slide 75

Slide 75 text

No threading primitives No “synchronized”, “volatile”, etc

Slide 76

Slide 76 text

Runtime Safety Kotlin/Native can verify safe mutability

Slide 77

Slide 77 text

JVM/JS? See Kotlinconf keynote

Slide 78

Slide 78 text

Short term pain Tradeoff for future

Slide 79

Slide 79 text

How does Kotlin know?!

Slide 80

Slide 80 text

FROZEN!

Slide 81

Slide 81 text

FROZEN!

Slide 82

Slide 82 text

Runtime Designation AKA a flag

Slide 83

Slide 83 text

Call freeze()

Slide 84

Slide 84 text

One-way operation No unfreeze()

Slide 85

Slide 85 text

class TalkExamples{ var justCountingStuff:Int = 0 init { backgroundCall { //do something justCountingStuff++ }.freeze() } }

Slide 86

Slide 86 text

class TalkExamples{ var justCountingStuff:Int = 0 init { backgroundCall { //do something justCountingStuff++ }.freeze() } }

Slide 87

Slide 87 text

Usually OK Data objects should be immutable

Slide 88

Slide 88 text

Global state more difficult Service object, large memory state

Slide 89

Slide 89 text

Passing State

Slide 90

Slide 90 text

DetachedObjectGraph(TransferMode.SAFE) { ListData("asdf") }

Slide 91

Slide 91 text

val data = ListData("asdf") DetachedObjectGraph(TransferMode.SAFE) { data }

Slide 92

Slide 92 text

private val stateBox: AtomicReference> = AtomicReference( DetachedObjectGraph(mode = TransferMode.SAFE, producer = { mutableListOf() as Any }) ) private val lock = NSLock() internal fun withLockDetached(proc: (MutableList) -> MutableList) { lock.lock() try { stateBox.value = DetachedObjectGraph(mode = TransferMode.SAFE, producer = { val dataList = stateBox.value.attach() as MutableList proc(dataList) as Any }) } finally { lock.unlock() } }

Slide 93

Slide 93 text

Detaching Time must visit whole graph

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

Atomics! Mutable immutable

Slide 98

Slide 98 text

AtomicInt/AtomicLong

Slide 99

Slide 99 text

AtomicReference Update with frozen objects

Slide 100

Slide 100 text

val lambdas = AtomicReference(null) fun initPlatformClient( staticFileLoader: (filePrefix: String, fileType: String) -> String?, analyticsCallback: (name: String, params: Map) -> Unit, clLogCallback: (s: String) -> Unit) { lambdas.value = PlatformLambdas( staticFileLoader, analyticsCallback, clLogCallback).freeze() }

Slide 101

Slide 101 text

Stately! v0.3.1~ish

Slide 102

Slide 102 text

Multiplatform Definitions • freeze() method and frozen info • Atomics (Int, Long, Reference) • K/N state-related annotations (@ThreadLocal/ @SharedImmutable) • Shared Collections!

Slide 103

Slide 103 text

More Info and Tutorials https://github.com/touchlab/KotlinMultiplatformStuff

Slide 104

Slide 104 text

Making a Library! a very small library

Slide 105

Slide 105 text

Download Intellij EAP https://www.jetbrains.com/idea/nextversion/

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

No content

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

targets { fromPreset(presets.jvm, 'jvm') // This preset is for iPhone emulator // Switch here to presets.iosArm64 to build library for iPhone device fromPreset(presets.iosX64, 'ios') { compilations.main.outputKinds('FRAMEWORK') } }

Slide 111

Slide 111 text

targets { fromPreset(presets.jvm, 'jvm') // This preset is for iPhone emulator // Switch here to presets.iosArm64 to build library for iPhone device fromPreset(presets.macosX64, 'ios') /*{ compilations.main.outputKinds('FRAMEWORK') }*/ }

Slide 112

Slide 112 text

targets { fromPreset(presets.jvm, 'jvm') fromPreset(presets.js, 'js') fromPreset(presets.macosX64, 'macos') fromPreset(presets.iosX64, 'iosX64') fromPreset(presets.iosArm32, 'iosArm32') fromPreset(presets.iosArm64, 'iosArm64') }

Slide 113

Slide 113 text

Live Coding is a Bad Idea don’t be a hero

Slide 114

Slide 114 text

Go to Github https://github.com/kpgalligan/MyNewLibrary

Slide 115

Slide 115 text

Much More obviously

Slide 116

Slide 116 text

Libraries Needed • File Access • Date (310 port) • Mocking (and other test support) • Reactive • UI State

Slide 117

Slide 117 text

Getting Started

Slide 118

Slide 118 text

Join Kotlin Slack https://slack.kotlinlang.org

Slide 119

Slide 119 text

Sample Apps the conference apps

Slide 120

Slide 120 text

https://github.com/touchlab/DroidconKotlin/

Slide 121

Slide 121 text

Sample Libraries Stately SQLDelight

Slide 122

Slide 122 text

Office Hours - Kotlin Multiplatform 2pm in Coblenz

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

[email protected] @kpgalligan

Slide 125

Slide 125 text

[email protected] @kpgalligan Join the team !