Slide 1

Slide 1 text

Expecting fun with Kotlin Multiplatform Mobile

Slide 2

Slide 2 text

Felipe Costa Software engineering manager @ OLX Brasil Software professional passionate about mobile and open source technologies with over 10 years of experience as a software engineer. Kotlin enthusiast since 2016.

Slide 3

Slide 3 text

Our purpose We approach Brazilians to turn items into happiness.

Slide 4

Slide 4 text

Kotlin @ OLX 0 Python Javascript Java C PHP Kotlin Objective-C Perl

Slide 5

Slide 5 text

Kotlin @ OLX 0 Android Authentication Experimentação Insertion Search Recomendation

Slide 6

Slide 6 text

Kotlin History

Slide 7

Slide 7 text

2011 Jetbrains unveiled Project Kotlin 2012 Jetbrains open sourced the project 2015 Using Project Kotlin for Android (Square) 2016 Kotlin 1.0 (first officially stable release) 2017 First-class support for Kotlin on Android

Slide 8

Slide 8 text

2017 Kotlin 1.2 (Kotlin Multiplatform) 2018 Kotlin 1.3 (coroutines) 2019 Preferred language for Android app developers 2020 Kotlin 1.4 (support for Apple's platforms) 2020 Kotlin Multiplatform Mobile Goes Alpha

Slide 9

Slide 9 text

Why Kotlin Multiplatform Mobile (KMM)?

Slide 10

Slide 10 text

Optional Sharing Low risk No Big decisions

Slide 11

Slide 11 text

100% Native smooth interop First class citizen

Slide 12

Slide 12 text

Code sharing not “cross platform”

Slide 13

Slide 13 text

Activity community

Slide 14

Slide 14 text

Good tools

Slide 15

Slide 15 text

Modern Language

Slide 16

Slide 16 text

Not necessarily UI Business rules

Slide 17

Slide 17 text

Who uses KMM

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Evaluating other solutions

Slide 23

Slide 23 text

Input Process Output App type Native Language: Kotlin & Swift & JS Outra linguagem Cross-Compile Inclui Runtime (Interpretador, VM, Bibliotecas) Native Code Native Language: Javascript Native Web Native Web

Slide 24

Slide 24 text

Input Process Output App type Native Language Other Language: Javascript Cross-Compile Include Runtime (Interpreter, VM, Libraries) Native Code + Bundle JS Native Language Native Web React Native

Slide 25

Slide 25 text

Flutter Input Process Output App type Native Language Other Language: Dart Cross-Compile Include Runtime (Interpreter, VM, Libraries) Native Code Native Language Native Web

Slide 26

Slide 26 text

Kotlin Multiplatform Input Process Output App Type Native Language: Kotlin Other Language Cross-Compile Include Runtime (Interpreter, VM, Libraries) Native Code Native Language: Javascript Native Web Android iOS Web

Slide 27

Slide 27 text

Architecture

Slide 28

Slide 28 text

Architecture

Slide 29

Slide 29 text

Multiplaform code

Slide 30

Slide 30 text

Common *.kt

Slide 31

Slide 31 text

plugins { id 'kotlin-multiplatform' version ‘1.4.0' } repositories { mavenCentral() } kotlin { sourceSets { commonMain { dependencies { implementation “io.ktor:ktor-client-core:1.4.0” } } } } Build.gradle

Slide 32

Slide 32 text

plugins { id 'kotlin-multiplatform' version ‘1.4.0' } repositories { mavenCentral() } kotlin { sourceSets { commonMain { dependencies { implementation "io.ktor:ktor-client-core:1.4.0" } } } } Build.gradle

Slide 33

Slide 33 text

plugins { id 'kotlin-multiplatform' version '1.4.0' } repositories { mavenCentral() } kotlin { sourceSets { commonMain { dependencies { implementation “io.ktor:ktor-client-core:1.4.0” } } } } Build.gradle

Slide 34

Slide 34 text

plugins { id 'kotlin-multiplatform' version '1.4.0' } repositories { mavenCentral() } kotlin { sourceSets { commonMain { dependencies { implementation “io.ktor:ktor-client-core:1.4.0” } } } } Build.gradle

Slide 35

Slide 35 text

Common *.kt

Slide 36

Slide 36 text

Common *.kt *.class *.jar, *.apk

Slide 37

Slide 37 text

kotlin { android() sourceSets { commonMain { /* */ } } } Build.gradle

Slide 38

Slide 38 text

kotlin { android() sourceSets { commonMain { /* */ } } } Build.gradle

Slide 39

Slide 39 text

kotlin { android() sourceSets { commonMain { /* */ } } } Build.gradle

Slide 40

Slide 40 text

Common *.kt *.class *.jar, *.apk binário nativo iOS Watch

Slide 41

Slide 41 text

kotlin { android() iosArm64 { binaries { framework(“shared”) } } sourceSets { commonMain { /* */ } } } Build.gradle

Slide 42

Slide 42 text

kotlin { android() iosArm64 { binaries { framework(“shared”) } } sourceSets { commonMain { /* */ } } } Build.gradle

Slide 43

Slide 43 text

kotlin { android() iosArm64 { binaries { framework(“shared”) } } sourceSets { commonMain { /* */ } } } Build.gradle

Slide 44

Slide 44 text

fun hello(): String = "Hello from Kotlin Multiplatform" Common

Slide 45

Slide 45 text

import kotlin.Metadata; import org.jetbrains.annotations.NotNull; @Metadata( mv = {1, 1, 15}, bv = {1, 0, 3}, k = 2, d1 = {"\u0000\b\n\u0000\n\u0002\u0010\u000e\n\u0000\u001a\u0006\u0010\u0000\u001a\u00020\u0001¨\u0006\u0002"}, d2 = {"hello", "", "Platform module com.example.hello-world-multiplatform.jvmMain including [com.example.hello-world-multiplatform.commonMain]"} ) public final class SampleKt { @NotNull public static final String hello() { return "Hello from Kotlin Multiplatform"; } } Android

Slide 46

Slide 46 text

iOS Framework #import NS_ASSUME_NONNULL_BEGIN @interface KotlinBase : NSObject - (instancetype)init __attribute__((unavailable)); + (instancetype)new __attribute__((unavailable)); + (void)initialize __attribute__((objc_requires_super)); @end; @interface KotlinBase (KotlinBaseCopying) @end; /* More types */ __attribute__((objc_subclassing_restricted)) __attribute__((swift_name("SampleKt"))) @interface FrameworkSampleKt : KotlinBase + (NSString *)hello __attribute__((swift_name("hello()"))); @end; NS_ASSUME_NONNULL_END

Slide 47

Slide 47 text

Platform-specific code

Slide 48

Slide 48 text

fun hello(): String = "Hello from Kotlin Multiplatform" Common

Slide 49

Slide 49 text

object Platform { val name: String } fun hello(): String = "Hello from ${Platform.name}" Common

Slide 50

Slide 50 text

object Platform { val name: String = “Android" } fun hello(): String = "Hello from ${Platform.name}" Android

Slide 51

Slide 51 text

object Platform { val name: String = “iOS" } fun hello(): String = "Hello from ${Platform.name}" iOS

Slide 52

Slide 52 text

Common *.kt *.class *.jar, *.apk native binary iOS Watch

Slide 53

Slide 53 text

Common *.kt Kotlin/JVM *.kt, *.java Kotlin/Native *.kt, *.c *.class *.jar, *.apk native binary iOS Watch

Slide 54

Slide 54 text

Expect/Actual

Slide 55

Slide 55 text

expect object Platform { val name: String } fun hello(): String = "Hello from ${Platform.name}" Common

Slide 56

Slide 56 text

expect object Platform { val name: String } fun hello(): String = "Hello from ${Platform.name}" Common

Slide 57

Slide 57 text

expect object Platform { val name: String } fun hello(): String = "Hello from ${Platform.name}" Common

Slide 58

Slide 58 text

kotlin { android() iosArm64 { binaries { framework(“shared”) } } sourceSets { commonMain { /* */ } androidMain { dependencies { /* */ } } } } build.gradle

Slide 59

Slide 59 text

kotlin { android() iosArm64 { binaries { framework(“shared”) } } sourceSets { commonMain { /* */ } androidMain { dependencies { /* */ } } } } build.gradle

Slide 60

Slide 60 text

kotlin { android() iosArm64 { binaries { framework(“shared”) } } sourceSets { commonMain { /* */ } androidMain { dependencies { /* */ } } } } build.gradle

Slide 61

Slide 61 text

actual object Platform { actual val name: String = "Android" } fun hello(): String = "Hello from Android” Android

Slide 62

Slide 62 text

actual object Platform { actual val name: String = "Android" } fun hello(): String = "Hello from Android” Android

Slide 63

Slide 63 text

actual object Platform { actual val name: String = "Android" } fun hello(): String = "Hello from Android” Android

Slide 64

Slide 64 text

kotlin { android() iosArm64 { binaries { framework(“shared”) } } sourceSets { commonMain { /* */ } androidMain { /* */ } iosArm64Main { } } } build.gradle

Slide 65

Slide 65 text

kotlin { android() iosArm64 { binaries { framework(“shared”) } } sourceSets { commonMain { /* */ } androidMain { /* */ } iosArm64Main { } } } build.gradle

Slide 66

Slide 66 text

kotlin { android() iosArm64 { binaries { framework(“shared”) } } sourceSets { commonMain { /* */ } androidMain { /* */ } iosArm64Main { } } } build.gradle

Slide 67

Slide 67 text

actual object Platform { actual val name: String = "iOS" } fun hello(): String = "Hello from iOS” iOS

Slide 68

Slide 68 text

actual object Platform { actual val name: String = "iOS" } fun hello(): String = "Hello from iOS” iOS

Slide 69

Slide 69 text

actual object Platform { actual val name: String = "iOS" } fun hello(): String = "Hello from iOS” iOS

Slide 70

Slide 70 text

Tests?

Slide 71

Slide 71 text

kotlin { android() iosArm64 { binaries { framework(“shared”) } } sourceSets { commonMain { /* */ } commonTest { /* */ } androidMain { /* */ } androidTest { /* */ } iosArm64Main { /* */ } iosArm64Test { /* */ } } } build.gradle

Slide 72

Slide 72 text

kotlin { android() iosArm64 { binaries { framework(“shared”) } } sourceSets { commonMain { /* */ } commonTest { /* */ } androidMain { /* */ } androidTest { /* */ } iosArm64Main { /* */ } iosArm64Test { /* */ } } } build.gradle

Slide 73

Slide 73 text

kotlin { android() iosArm64 { binaries { framework(“shared”) } } sourceSets { commonMain { /* */ } commonTest { /* */ } androidMain { /* */ } androidTest { /* */ } iosArm64Main { /* */ } iosArm64Test { /* */ } } } build.gradle

Slide 74

Slide 74 text

kotlin { /* */ sourceSets { /* */ commonTest { dependencies { implementation kotlin('test-common') implementation kotlin('test-annotations-common') } } androidTest { dependencies { implementation kotlin('test') implementation kotlin('test-junit') } } iosArm64Test { } } } build.gradle

Slide 75

Slide 75 text

import kotlin.test.Test import kotlin.test.assertTrue class SampleTestsAndroid { @Test fun testHello() { assertTrue(“Android" in hello()) } } Android

Slide 76

Slide 76 text

import kotlin.test.Test import kotlin.test.assertTrue class SampleTestsIOS { @Test fun testHello() { assertTrue(“iOS" in hello()) } } iOS

Slide 77

Slide 77 text

expect class Sample() { fun checkMe(): Int } object Platform { val name: String } fun hello(): String = "Hello from ${Platform.name}" Common

Slide 78

Slide 78 text

import kotlin.test.Test import kotlin.test.assertTrue class SampleTests { @Test fun testMe() { assertTrue(Sample().checkMe() > 0) } } Common

Slide 79

Slide 79 text

Libraries

Slide 80

Slide 80 text

Ktor Kotlinx.Coroutines kotlinx.io kotlinx.serialization sqldelight MPSettings Stately KorLibs kotlinx-datetime Decompose (UI State)

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

Multiplatform Chat

Slide 83

Slide 83 text

Architecture Common Client Android iOS Server

Slide 84

Slide 84 text

Architecture Android iOS Server Main Common Message Client ChatViewModel PlatformSocket

Slide 85

Slide 85 text

Common PlatformSocket.kt Kotlin/JVM PlatformSocket.kt Kotlin/Native PlatformSocket.kt *.class *.jar, *.apk native binary iOS Watch

Slide 86

Slide 86 text

expect class PlatformSocket(url: String) { fun openSocket(listener: PlatformSocketListener) fun closeSocket(code: Int, reason: String) fun sendMessage(msg: String) } interface PlatformSocketListener { fun onOpen() fun onFailure(t: Throwable) fun onMessage(msg: String) fun onClosing(code: Int, reason: String) fun onClosed(code: Int, reason: String) } Common

Slide 87

Slide 87 text

expect class PlatformSocket(url: String) { fun openSocket(listener: PlatformSocketListener) fun closeSocket(code: Int, reason: String) fun sendMessage(msg: String) } interface PlatformSocketListener { fun onOpen() fun onFailure(t: Throwable) fun onMessage(msg: String) fun onClosing(code: Int, reason: String) fun onClosed(code: Int, reason: String) } Common

Slide 88

Slide 88 text

expect class PlatformSocket(url: String) { fun openSocket(listener: PlatformSocketListener) fun closeSocket(code: Int, reason: String) fun sendMessage(msg: String) } interface PlatformSocketListener { fun onOpen() fun onFailure(t: Throwable) fun onMessage(msg: String) fun onClosing(code: Int, reason: String) fun onClosed(code: Int, reason: String) } Common

Slide 89

Slide 89 text

import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response import okhttp3.WebSocket actual class PlatformSocket actual constructor(url: String) { private val socketEndpoint = url private var webSocket: WebSocket? = null actual fun openSocket(listener: PlatformSocketListener) { val socketRequest = Request.Builder().url(socketEndpoint).build() val webClient = OkHttpClient().newBuilder().build() webSocket = webClient.newWebSocket( socketRequest, object : okhttp3.WebSocketListener() { override fun onOpen(webSocket: WebSocket, response: Response) = listener.onOpen() override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) = listener.onFailure(t) override fun onMessage(webSocket: WebSocket, text: String) = listener.onMessage(text) override fun onClosing(webSocket: WebSocket, code: Int, reason: String) = listener.onClosing(code, reason) override fun onClosed(webSocket: WebSocket, code: Int, reason: String) = listener.onClosed(code, reason) } ) } actual fun closeSocket(code: Int, reason: String) { webSocket?.close(code, reason) webSocket = null } actual fun sendMessage(msg: String) { webSocket?.send(msg) } } Android

Slide 90

Slide 90 text

import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response import okhttp3.WebSocket actual class PlatformSocket actual constructor(url: String) { private val socketEndpoint = url private var webSocket: WebSocket? = null actual fun openSocket(listener: PlatformSocketListener) { val socketRequest = Request.Builder().url(socketEndpoint).build() val webClient = OkHttpClient().newBuilder().build() webSocket = webClient.newWebSocket( socketRequest, object : okhttp3.WebSocketListener() { override fun onOpen(webSocket: WebSocket, response: Response) = listener.onOpen() override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) = listener.onFailure(t) override fun onMessage(webSocket: WebSocket, text: String) = listener.onMessage(text) override fun onClosing(webSocket: WebSocket, code: Int, reason: String) = listener.onClosing(code, reason) override fun onClosed(webSocket: WebSocket, code: Int, reason: String) = listener.onClosed(code, reason) } ) } actual fun closeSocket(code: Int, reason: String) { webSocket?.close(code, reason) webSocket = null } actual fun sendMessage(msg: String) { webSocket?.send(msg) } } Android

Slide 91

Slide 91 text

import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response import okhttp3.WebSocket actual class PlatformSocket actual constructor(url: String) { private val socketEndpoint = url private var webSocket: WebSocket? = null actual fun openSocket(listener: PlatformSocketListener) { val socketRequest = Request.Builder().url(socketEndpoint).build() val webClient = OkHttpClient().newBuilder().build() webSocket = webClient.newWebSocket( socketRequest, object : okhttp3.WebSocketListener() { override fun onOpen(webSocket: WebSocket, response: Response) = listener.onOpen() override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) = listener.onFailure(t) override fun onMessage(webSocket: WebSocket, text: String) = listener.onMessage(text) override fun onClosing(webSocket: WebSocket, code: Int, reason: String) = listener.onClosing(code, reason) override fun onClosed(webSocket: WebSocket, code: Int, reason: String) = listener.onClosed(code, reason) } ) } actual fun closeSocket(code: Int, reason: String) { webSocket?.close(code, reason) webSocket = null } actual fun sendMessage(msg: String) { webSocket?.send(msg) } } Android

Slide 92

Slide 92 text

import platform.Foundation.* import platform.darwin.NSObject actual class PlatformSocket actual constructor(url: String) { private val socketEndpoint = NSURL.URLWithString(url)!! private var webSocket: NSURLSessionWebSocketTask? = null actual fun openSocket(listener: PlatformSocketListener) { val urlSession = NSURLSession.sessionWithConfiguration( configuration = NSURLSessionConfiguration.defaultSessionConfiguration(), delegate = object : NSObject(), NSURLSessionWebSocketDelegateProtocol { override fun URLSession(session: NSURLSession, webSocketTask: NSURLSessionWebSocketTask, didOpenWithProtocol: String?) { listener.onOpen() } override fun URLSession(session: NSURLSession, webSocketTask: NSURLSessionWebSocketTask, didCloseWithCode: NSURLSessionWebSocketCloseCode, reason: NSData?) { listener.onClosed(didCloseWithCode.toInt(), reason.toString()) } }, delegateQueue = NSOperationQueue.currentQueue() ) webSocket = urlSession.webSocketTaskWithURL(socketEndpoint) listenMessages(listener) webSocket?.resume() } private fun listenMessages(listener: PlatformSocketListener) { webSocket?.receiveMessageWithCompletionHandler { message, nsError -> when { nsError != null -> { listener.onFailure(Throwable(nsError.description)) } message != null -> { message.string?.let { listener.onMessage(it) } } } listenMessages(listener) } } actual fun closeSocket(code: Int, reason: String) { webSocket?.cancelWithCloseCode(code.toLong(), null) webSocket = null } actual fun sendMessage(msg: String) { val message = NSURLSessionWebSocketMessage(msg) webSocket?.sendMessage(message) { err -> err?.let { println("send $msg error: $it") } } } } iOS

Slide 93

Slide 93 text

import platform.Foundation.* import platform.darwin.NSObject actual class PlatformSocket actual constructor(url: String) { private val socketEndpoint = NSURL.URLWithString(url)!! private var webSocket: NSURLSessionWebSocketTask? = null actual fun openSocket(listener: PlatformSocketListener) { val urlSession = NSURLSession.sessionWithConfiguration( configuration = NSURLSessionConfiguration.defaultSessionConfiguration(), delegate = object : NSObject(), NSURLSessionWebSocketDelegateProtocol { override fun URLSession(session: NSURLSession, webSocketTask: NSURLSessionWebSocketTask, didOpenWithProtocol: String?) { listener.onOpen() } override fun URLSession(session: NSURLSession, webSocketTask: NSURLSessionWebSocketTask, didCloseWithCode: NSURLSessionWebSocketCloseCode, reason: NSData?) { listener.onClosed(didCloseWithCode.toInt(), reason.toString()) } }, delegateQueue = NSOperationQueue.currentQueue() ) webSocket = urlSession.webSocketTaskWithURL(socketEndpoint) listenMessages(listener) webSocket?.resume() } private fun listenMessages(listener: PlatformSocketListener) { webSocket?.receiveMessageWithCompletionHandler { message, nsError -> when { nsError != null -> { listener.onFailure(Throwable(nsError.description)) } message != null -> { message.string?.let { listener.onMessage(it) } } } listenMessages(listener) } } actual fun closeSocket(code: Int, reason: String) { webSocket?.cancelWithCloseCode(code.toLong(), null) webSocket = null } actual fun sendMessage(msg: String) { val message = NSURLSessionWebSocketMessage(msg) webSocket?.sendMessage(message) { err -> err?.let { println("send $msg error: $it") } } } } iOS

Slide 94

Slide 94 text

import platform.Foundation.* import platform.darwin.NSObject actual class PlatformSocket actual constructor(url: String) { private val socketEndpoint = NSURL.URLWithString(url)!! private var webSocket: NSURLSessionWebSocketTask? = null actual fun openSocket(listener: PlatformSocketListener) { val urlSession = NSURLSession.sessionWithConfiguration( configuration = NSURLSessionConfiguration.defaultSessionConfiguration(), delegate = object : NSObject(), NSURLSessionWebSocketDelegateProtocol { override fun URLSession(session: NSURLSession, webSocketTask: NSURLSessionWebSocketTask, didOpenWithProtocol: String?) { listener.onOpen() } override fun URLSession(session: NSURLSession, webSocketTask: NSURLSessionWebSocketTask, didCloseWithCode: NSURLSessionWebSocketCloseCode, reason: NSData?) { listener.onClosed(didCloseWithCode.toInt(), reason.toString()) } }, delegateQueue = NSOperationQueue.currentQueue() ) webSocket = urlSession.webSocketTaskWithURL(socketEndpoint) listenMessages(listener) webSocket?.resume() } private fun listenMessages(listener: PlatformSocketListener) { webSocket?.receiveMessageWithCompletionHandler { message, nsError -> when { nsError != null -> { listener.onFailure(Throwable(nsError.description)) } message != null -> { message.string?.let { listener.onMessage(it) } } } listenMessages(listener) } } actual fun closeSocket(code: Int, reason: String) { webSocket?.cancelWithCloseCode(code.toLong(), null) webSocket = null } actual fun sendMessage(msg: String) { val message = NSURLSessionWebSocketMessage(msg) webSocket?.sendMessage(message) { err -> err?.let { println("send $msg error: $it") } } } } iOS

Slide 95

Slide 95 text

Demo

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

Challenges

Slide 98

Slide 98 text

Alpha

Slide 99

Slide 99 text

Threads

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

Debug

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

Testing support

Slide 104

Slide 104 text

Compilation Time

Slide 105

Slide 105 text

Multiplatform UI

Slide 106

Slide 106 text

Compose

Slide 107

Slide 107 text

https://kotlinlang.org/lp/mobile/

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

felipehjcosta [email protected]