Slide 1

Slide 1 text

Reuso de código com Kotlin Multiplataforma

Slide 2

Slide 2 text

Felipe Costa Engenheiro de software Senior @ OLX Profissional de software apaixonado. Atuo como desenvolvedor, principalmente, para dispositivos móveis desde 2011 e fazendo- o principalmente em Kotlin desde 2016.

Slide 3

Slide 3 text

Nosso Propósito Aproximamos Brasileiros para transformar itens em felicidade.

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 Autenticação Experimentação inserção Busca Recomendação

Slide 6

Slide 6 text

Karol & MVP de um chat multiplataforma

Slide 7

Slide 7 text

Por quê reutilizar código entre plataformas?

Slide 8

Slide 8 text

DRY

Slide 9

Slide 9 text

Mitigação de risco

Slide 10

Slide 10 text

Paridade de funcionalidades

Slide 11

Slide 11 text

Soluções

Slide 12

Slide 12 text

Entrada Processo Saída Tipo de App Linguagem nativa: Kotlin & Swift & JS Outra linguagem Cross-Compilação Inclui Runtime (Interpretador, VM, Bibliotecas) Código nativo Linguagem nativa: Javascript Nativo Web Nativo Web

Slide 13

Slide 13 text

Entrada Processo Saída Tipo de app Linguagem nativa Outra linguagem: Javascript Cross-Compilação Inclui Runtime (Interpretador, VM, Bibliotecas) Código nativo + Bundle JS Linguagem nativa Nativo Web React Native

Slide 14

Slide 14 text

Flutter Entrada Processo Saída Tipo de app Linguagem nativa Outra linguagem: Dart Cross-Compilação Inclui Runtime (Interpretador, VM, Bibliotecas) Código nativo Linguagem nativa Nativo Web

Slide 15

Slide 15 text

Kotlin Multiplataforma Entrada Processo Saída Tipo de app Linguagem nativa: Kotlin Outra linguagem Cross-Compilação Inclui Runtime (Interpretador, VM, Bibliotecas) Código nativo Linguagem nativa: Javascript Nativo Web Android iOS Web

Slide 16

Slide 16 text

Por quê Kotlin Multiplataforma?

Slide 17

Slide 17 text

Compartilhamento opcional Baixo risco Sem Grandes decisões

Slide 18

Slide 18 text

100% Nativo Interop suave

Slide 19

Slide 19 text

Compartilhamento de código Não é “cross plataforma”

Slide 20

Slide 20 text

Comunidade ativa

Slide 21

Slide 21 text

Boas ferramentas

Slide 22

Slide 22 text

Linguagem moderna

Slide 23

Slide 23 text

Sem UI não necessariamente

Slide 24

Slide 24 text

Muitas plataformas

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

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

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

Slide 46

Slide 46 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 47

Slide 47 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 48

Slide 48 text

MacOS Binary .kexe

Slide 49

Slide 49 text

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

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

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 53

Slide 53 text

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

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

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 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 { /* */ 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 89

Slide 89 text

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

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

Bibliotecas

Slide 95

Slide 95 text

Ktor Kotlinx.Coroutines kotlinx.io kotlinx.serialization sqldelight MPSettings Stately KorLibs Acesso a arquivos Melhor suporte a testes Data Estado de UI

Slide 96

Slide 96 text

MVP de um Chat Multiplataforma

Slide 97

Slide 97 text

Arquitetura Common Client Android iOS Web Server

Slide 98

Slide 98 text

Arquitetura 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

Desafios

Slide 115

Slide 115 text

Experimental

Slide 116

Slide 116 text

Threads

Slide 117

Slide 117 text

Debug

Slide 118

Slide 118 text

Tempo de compilação

Slide 119

Slide 119 text

MVP de Chat entregue

Slide 120

Slide 120 text

No content

Slide 121

Slide 121 text

felipehjcosta [email protected]