Slide 1

Slide 1 text

Code reuse with Kotlin Multiplatform

Slide 2

Slide 2 text

Felipe Costa Senior Software engineer @ OLX Brasil Passionate Software Craftsman. I have been developing mobile applications since 2011 and enjoying it mostly in Kotlin 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

Karol & Multiplatform Chat MVP

Slide 7

Slide 7 text

Why reuse code between platforms?

Slide 8

Slide 8 text

DRY

Slide 9

Slide 9 text

Risk mitigation

Slide 10

Slide 10 text

Feature parity

Slide 11

Slide 11 text

Solutions

Slide 12

Slide 12 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 13

Slide 13 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 14

Slide 14 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 15

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

Slide 16 text

Why Kotlin Multiplatform?

Slide 17

Slide 17 text

Optional Sharing Low risk No Big decisions

Slide 18

Slide 18 text

100% Native smooth interop

Slide 19

Slide 19 text

Code sharing not “cross platform”

Slide 20

Slide 20 text

Activity community

Slide 21

Slide 21 text

Good tools

Slide 22

Slide 22 text

Modern Language

Slide 23

Slide 23 text

Not UI not necessarily

Slide 24

Slide 24 text

Many Platforms

Slide 25

Slide 25 text

Common *.kt

Slide 26

Slide 26 text

plugins { id 'kotlin-multiplatform' version '1.3.31' } repositories { mavenCentral() } kotlin { sourceSets { commonMain { dependencies { implementation kotlin('stdlib-common') } } } } Build.gradle

Slide 27

Slide 27 text

plugins { id 'kotlin-multiplatform' version '1.3.31' } repositories { mavenCentral() } kotlin { sourceSets { commonMain { dependencies { implementation kotlin('stdlib-common') } } } } Build.gradle

Slide 28

Slide 28 text

plugins { id 'kotlin-multiplatform' version '1.3.31' } repositories { mavenCentral() } kotlin { sourceSets { commonMain { dependencies { implementation kotlin('stdlib-common') } } } } Build.gradle

Slide 29

Slide 29 text

plugins { id 'kotlin-multiplatform' version '1.3.31' } repositories { mavenCentral() } kotlin { sourceSets { commonMain { dependencies { implementation kotlin('stdlib-common') } } } } Build.gradle

Slide 30

Slide 30 text

Common *.kt

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

Common *.kt *.class *.jar, *.apk *.js binário nativo Android/ NDK iOS Mac Linux Windows Webassembly Outros

Slide 42

Slide 42 text

Common *.kt *.class *.jar, *.apk *.js binário nativo Android/ NDK iOS Mac Linux Windows Webassembly Outros

Slide 43

Slide 43 text

kotlin { jvm() js() // For ARM, should be changed to iosArm32 or iosArm64 // For Linux, should be changed to e.g. linuxX64 // For MacOS, should be changed to e.g. macosX64 // For Windows, should be changed to e.g. mingwX64 macosX64("macos") sourceSets { commonMain { /* */ } } } Build.gradle

Slide 44

Slide 44 text

kotlin { jvm() js() // For ARM, should be changed to iosArm32 or iosArm64 // For Linux, should be changed to e.g. linuxX64 // For MacOS, should be changed to e.g. macosX64 // For Windows, should be changed to e.g. mingwX64 macosX64("macos") sourceSets { commonMain { /* */ } } } Build.gradle

Slide 45

Slide 45 text

kotlin { jvm() js() // For ARM, should be changed to iosArm32 or iosArm64 // For Linux, should be changed to e.g. linuxX64 // For MacOS, should be changed to e.g. macosX64 // For Windows, should be changed to e.g. mingwX64 macosX64("macos") sourceSets { commonMain { /* */ } } } Build.gradle

Slide 46

Slide 46 text

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

Slide 47

Slide 47 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"; } } JVM

Slide 48

Slide 48 text

if (typeof kotlin === 'undefined') { throw new Error("Error loading module 'hello-world-multiplatform'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'hello-world-multiplatform'."); } this['hello-world-multiplatform'] = function (_, Kotlin) { 'use strict'; function hello() { return 'Hello from Kotlin Multiplatform'; } var package$sample = _.sample || (_.sample = {}); package$sample.hello = hello; Kotlin.defineModule('hello-world-multiplatform', _); return _; }(typeof this['hello-world-multiplatform'] === 'undefined' ? {} : this['hello-world-multiplatform'], kotlin); JS

Slide 49

Slide 49 text

MacOS Binary .kexe

Slide 50

Slide 50 text

kotlin { jvm() js() macosX64("macos") { binaries { sharedLib("sharedLib") } } sourceSets { commonMain { /* */ } } } Build.gradle

Slide 51

Slide 51 text

kotlin { jvm() js() macosX64("macos") { binaries { sharedLib("sharedLib") } } sourceSets { commonMain { /* */ } } } Build.gradle

Slide 52

Slide 52 text

kotlin { jvm() js() macosX64("macos") { binaries { sharedLib("sharedLib") } } sourceSets { commonMain { /* */ } } } Build.gradle

Slide 53

Slide 53 text

MacOS Dynamic Library #ifndef KONAN_LIBSHAREDLIB_H #define KONAN_LIBSHAREDLIB_H #ifdef __cplusplus extern "C" { #endif /* typedefes */ typedef struct { /* Service functions. */ /* ... */ /* User functions. */ struct { struct { struct { const char* (*hello)(); } sample; } root; } kotlin; } libsharedLib_ExportedSymbols; extern libsharedLib_ExportedSymbols* libsharedLib_symbols(void); #ifdef __cplusplus } /* extern "C" */ #endif #endif /* KONAN_LIBSHAREDLIB_H */

Slide 54

Slide 54 text

kotlin { jvm() js() macosX64("macos") { binaries { framework("framework") } } sourceSets { commonMain { /* */ } } } Build.gradle

Slide 55

Slide 55 text

kotlin { jvm() js() macosX64("macos") { binaries { framework("framework") } } sourceSets { commonMain { /* */ } } } Build.gradle

Slide 56

Slide 56 text

kotlin { jvm() js() macosX64("macos") { binaries { framework("framework") } } sourceSets { commonMain { /* */ } } } Build.gradle

Slide 57

Slide 57 text

MacOS 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 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

Common *.kt Kotlin/JVM *.kt, *.java Kotlin/JS *.kt, *.js Kotlin/Native *.kt *.class *.jar, *.apk *.js binário nativo Android/ NDK iOS Mac Linux Windows Webassembly Others

Slide 64

Slide 64 text

Libraries

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

kotlin { jvm() macosX64("macos") sourceSets { commonMain { /* */ } jvmMain { dependencies { implementation kotlin('stdlib-jdk8') } } } } build.gradle

Slide 69

Slide 69 text

kotlin { jvm() macosX64("macos") sourceSets { commonMain { /* */ } jvmMain { dependencies { implementation kotlin('stdlib-jdk8') } } } } build.gradle

Slide 70

Slide 70 text

kotlin { jvm() macosX64("macos") sourceSets { commonMain { /* */ } jvmMain { dependencies { implementation kotlin('stdlib-jdk8') } } } } build.gradle

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

kotlin { jvm() js() macosX64("macos") sourceSets { commonMain { /* */ } jvmMain { /* */ } jsMain { dependencies { implementation kotlin('stdlib-js') } } } } build.gradle

Slide 75

Slide 75 text

kotlin { jvm() js() macosX64("macos") sourceSets { commonMain { /* */ } jvmMain { /* */ } jsMain { dependencies { implementation kotlin('stdlib-js') } } } } build.gradle

Slide 76

Slide 76 text

kotlin { jvm() js() macosX64("macos") sourceSets { commonMain { /* */ } jvmMain { /* */ } jsMain { dependencies { implementation kotlin('stdlib-js') } } } } build.gradle

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

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

Slide 80

Slide 80 text

kotlin { jvm() js() macosX64("macos") sourceSets { commonMain { /* */ } jvmMain { /* */ } jsMain { /* */ } macosMain { } } } build.gradle

Slide 81

Slide 81 text

kotlin { jvm() js() macosX64("macos") sourceSets { commonMain { /* */ } jvmMain { /* */ } jsMain { /* */ } macosMain { } } } build.gradle

Slide 82

Slide 82 text

kotlin { jvm() js() macosX64("macos") sourceSets { commonMain { /* */ } jvmMain { /* */ } jsMain { /* */ } macosMain { } } } build.gradle

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

kotlin { jvm() js() macosX64("macos") sourceSets { commonMain { /* */ } commonTest { /* */ } jvmMain { /* */ } jvmTest { /* */ } jsMain { /* */ } jsTest { /* */ } macosMain { /* */ } macosTest { /* */ } } } build.gradle

Slide 87

Slide 87 text

kotlin { jvm() js() macosX64("macos") sourceSets { commonMain { /* */ } commonTest { /* */ } jvmMain { /* */ } jvmTest { /* */ } jsMain { /* */ } jsTest { /* */ } macosMain { /* */ } macosTest { /* */ } } } build.gradle

Slide 88

Slide 88 text

kotlin { jvm() js() macosX64("macos") sourceSets { commonMain { /* */ } commonTest { /* */ } jvmMain { /* */ } jvmTest { /* */ } jsMain { /* */ } jsTest { /* */ } macosMain { /* */ } macosTest { /* */ } } } build.gradle

Slide 89

Slide 89 text

kotlin { /* */ sourceSets { /* */ commonTest { dependencies { implementation kotlin('test-common') implementation kotlin('test-annotations-common') } } jvmTest { dependencies { implementation kotlin('test') implementation kotlin('test-junit') } } jsTest { dependencies { implementation kotlin('test-js') } } macosTest { } } } build.gradle

Slide 90

Slide 90 text

import kotlin.test.Test import kotlin.test.assertTrue class SampleTestsJVM { @Test fun testHello() { assertTrue("JVM" in hello()) } } JVM

Slide 91

Slide 91 text

import kotlin.test.Test import kotlin.test.assertTrue class SampleTestsJS { @Test fun testHello() { assertTrue("JS" in hello()) } } JS

Slide 92

Slide 92 text

import kotlin.test.Test import kotlin.test.assertTrue class SampleTestsNative { @Test fun testHello() { assertTrue("Native" in hello()) } } Native

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

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

Slide 95

Slide 95 text

Ktor Kotlinx.Coroutines kotlinx.io kotlinx.serialization sqldelight MPSettings Stately KorLibs File Access Better test support Date UI state

Slide 96

Slide 96 text

Multiplatform Chat MVP

Slide 97

Slide 97 text

Architecture Common Client Android iOS Web Server

Slide 98

Slide 98 text

Architecture Android iOS Web Server Main Common Message Client ChatViewModel ChatClient

Slide 99

Slide 99 text

Common ChatClient.kt Kotlin/JVM ChatClient.kt Kotlin/JS ChatClient.kt Kotlin/Native ChatClient.kt ChatClient.class ChatClient.js binário nativo Android/ NDK iOS Mac Linux Windows Webassembly Outros

Slide 100

Slide 100 text

internal expect open class ChatClient(url: String) { open fun start() open fun send(message: Message) open fun receive(receiveBlock: (Message) -> Unit) open fun onFailure(throwableBlock: (Throwable) -> Unit) } Common

Slide 101

Slide 101 text

internal expect open class ChatClient(url: String) { open fun start() open fun send(message: Message) open fun receive(receiveBlock: (Message) -> Unit) open fun onFailure(throwableBlock: (Throwable) -> Unit) } Common

Slide 102

Slide 102 text

internal expect open class ChatClient(url: String) { open fun start() open fun send(message: Message) open fun receive(receiveBlock: (Message) -> Unit) open fun onFailure(throwableBlock: (Throwable) -> Unit) } Common

Slide 103

Slide 103 text

internal actual open class ChatClient actual constructor(val url: String) : WebSocketListener() { actual open fun start() { /* */ } actual open fun send(message: Message) { /* */ } actual open fun receive(receiveBlock: (Message) -> Unit) { /* */ } actual open fun onFailure(throwableBlock: (Throwable) -> Unit) { /* */ } override fun onMessage(webSocket: WebSocket, text: String) { /* */ } override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { /* */ } } Android

Slide 104

Slide 104 text

internal actual open class ChatClient actual constructor(val url: String) : WebSocketListener() { actual open fun start() { /* */ } actual open fun send(message: Message) { /* */ } actual open fun receive(receiveBlock: (Message) -> Unit) { /* */ } actual open fun onFailure(throwableBlock: (Throwable) -> Unit) { /* */ } override fun onMessage(webSocket: WebSocket, text: String) { /* */ } override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { /* */ } } Android

Slide 105

Slide 105 text

internal actual open class ChatClient actual constructor(val url: String) : WebSocketListener() { actual open fun start() { /* */ } actual open fun send(message: Message) { /* */ } actual open fun receive(receiveBlock: (Message) -> Unit) { /* */ } actual open fun onFailure(throwableBlock: (Throwable) -> Unit) { /* */ } override fun onMessage(webSocket: WebSocket, text: String) { /* */ } override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { /* */ } } Android

Slide 106

Slide 106 text

internal actual open class ChatClient actual constructor(val url: String) { actual open fun start() { /* */ } actual open fun send(message: Message) { /* */ } actual open fun receive(receiveBlock: (Message) -> Unit) { /* */ } actual open fun onFailure(throwableBlock: (Throwable) -> Unit) { /* */ } } JS

Slide 107

Slide 107 text

internal actual open class ChatClient actual constructor(val url: String) { actual open fun start() { /* */ } actual open fun send(message: Message) { /* */ } actual open fun receive(receiveBlock: (Message) -> Unit) { /* */ } actual open fun onFailure(throwableBlock: (Throwable) -> Unit) { /* */ } } JS

Slide 108

Slide 108 text

internal actual open class ChatClient actual constructor(val url: String) { actual open fun start() { /* */ } actual open fun send(message: Message) { /* */ } actual open fun receive(receiveBlock: (Message) -> Unit) { /* */ } actual open fun onFailure(throwableBlock: (Throwable) -> Unit) { /* */ } } JS

Slide 109

Slide 109 text

internal actual open class ChatClient actual constructor(val url: String) { actual open fun start() { /* */ } actual open fun send(message: Message) { /* */ } actual open fun receive(receiveBlock: (Message) -> Unit) { /* */ } actual open fun onFailure(throwableBlock: (Throwable) -> Unit) { /* */ } inner class WebSocketDelegate : NSObject(), SRWebSocketDelegateProtocol { override fun webSocket(webSocket: SRWebSocket?, didReceiveMessage: Any?) { /* */ } } } iOS

Slide 110

Slide 110 text

internal actual open class ChatClient actual constructor(val url: String) { actual open fun start() { /* */ } actual open fun send(message: Message) { /* */ } actual open fun receive(receiveBlock: (Message) -> Unit) { /* */ } actual open fun onFailure(throwableBlock: (Throwable) -> Unit) { /* */ } inner class WebSocketDelegate : NSObject(), SRWebSocketDelegateProtocol { override fun webSocket(webSocket: SRWebSocket?, didReceiveMessage: Any?) { /* */ } } } iOS

Slide 111

Slide 111 text

internal actual open class ChatClient actual constructor(val url: String) { actual open fun start() { /* */ } actual open fun send(message: Message) { /* */ } actual open fun receive(receiveBlock: (Message) -> Unit) { /* */ } actual open fun onFailure(throwableBlock: (Throwable) -> Unit) { /* */ } inner class WebSocketDelegate : NSObject(), SRWebSocketDelegateProtocol { override fun webSocket(webSocket: SRWebSocket?, didReceiveMessage: Any?) { /* */ } } } iOS

Slide 112

Slide 112 text

Demo

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

Challenges

Slide 115

Slide 115 text

Experimental

Slide 116

Slide 116 text

Threads

Slide 117

Slide 117 text

Debug

Slide 118

Slide 118 text

Compile Time

Slide 119

Slide 119 text

Chat MVP delivered

Slide 120

Slide 120 text

No content

Slide 121

Slide 121 text

felipehjcosta [email protected]