Slide 1

Slide 1 text

Kotlin Multiplatform: Sharing code between Android and iOS Wellington Costa Pereira Mobile Engineer @ Hash

Slide 2

Slide 2 text

What is Kotlin Multiplatform?

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

It’s an open source project developed and maintained by Jetbrains and some companies such as Square and Touchlab, that enables optional code sharing for several platforms, natively integrated, and based on Kotlin Programming Language. What is Kotlin Multiplatform?

Slide 5

Slide 5 text

It’s an open source project developed and maintained by Jetbrains and some companies such as Square and Touchlab, that enables optional code sharing for several platforms, natively integrated, and based on Kotlin Programming Language. The main advantage is the capability of write Kotlin code, e.g. business code, that can be shared to several platforms, and compile it to native code, saving efforts to write the same code in different languages for each platforms. What is Kotlin Multiplatform?

Slide 6

Slide 6 text

Optional You can decide which code to share

Slide 7

Slide 7 text

Natively integrated It compiles to native code with low efforts

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Code sharing Not “cross platform”

Slide 10

Slide 10 text

Not UI code Share logic code and infrastructure code

Slide 11

Slide 11 text

Supported platforms

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

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

Under the hood…

Slide 23

Slide 23 text

:shared:

Slide 24

Slide 24 text

:shared: Kotlin IR

Slide 25

Slide 25 text

:shared: Kotlin IR Android iOS Windows Linux JVM Javascript

Slide 26

Slide 26 text

expect/actual

Slide 27

Slide 27 text

expect/actual mechanism "// In common code expect val isMainThread: Boolean

Slide 28

Slide 28 text

expect/actual mechanism "// In common code expect val isMainThread: Boolean "// In Android/JVM actual val isMainThread: Boolean get() = Looper.getMainLooper() ""=== Looper.myLooper()

Slide 29

Slide 29 text

expect/actual mechanism "// In common code expect val isMainThread: Boolean "// In Android/JVM actual val isMainThread: Boolean get() = Looper.getMainLooper() ""=== Looper.myLooper() "// In iOS/Native actual val isMainThread: Boolean get() = NSThread.isMainThread()

Slide 30

Slide 30 text

Weapons starter pack

Slide 31

Slide 31 text

Weapons starter pack • SQLDelight

Slide 32

Slide 32 text

SQLDelight SQLDelight generates typesafe APIs from SQL statements and verifies at compile-time the schemas, statements, migrations, and so on.

Slide 33

Slide 33 text

SQLDelight SQLDelight generates typesafe APIs from SQL statements and verifies at compile-time the schemas, statements, migrations, and so on. It also provides IDE features like autocomplete.

Slide 34

Slide 34 text

SQLDelight SQLDelight generates typesafe APIs from SQL statements and verifies at compile-time the schemas, statements, migrations, and so on. It also provides IDE features like autocomplete. SQLDelight currently supports the SQLite dialect, and there are supported drivers for Android, JVM, iOS and Windows.

Slide 35

Slide 35 text

Why SQL code?

Slide 36

Slide 36 text

SQLDelight • SQL is standardized.

Slide 37

Slide 37 text

SQLDelight • SQL is standardized. • Everyone understands SQL.

Slide 38

Slide 38 text

SQLDelight • SQL is standardized. • Everyone understands SQL. • It’s not required to learn a custom DSL of an ORM library.

Slide 39

Slide 39 text

SQLDelight • SQL is standardized. • Everyone understands SQL. • It’s not required to learn a custom DSL of an ORM library. • You write SQL code and let SQLDelight generates Kotlin code.

Slide 40

Slide 40 text

How?

Slide 41

Slide 41 text

SQLDelight • Write SQL into .sq files

Slide 42

Slide 42 text

SQLDelight • Write SQL into .sq files • Build the project

Slide 43

Slide 43 text

SQLDelight • Write SQL into .sq files • Build the project • Use generated code!

Slide 44

Slide 44 text

SQLDelight • Write SQL into .sq files • Build the project • Use generated code! • Migrate you database writing code into .sqm files

Slide 45

Slide 45 text

SQLDelight dependencies { classpath("com.squareup.sqldelight:gradle-plugin:1.3.0") }

Slide 46

Slide 46 text

SQLDelight dependencies { classpath("com.squareup.sqldelight:gradle-plugin:1.3.0") } plugins { id("com.squareup.sqldelight") }

Slide 47

Slide 47 text

SQLDelight dependencies { classpath("com.squareup.sqldelight:gradle-plugin:1.3.0") } plugins { id("com.squareup.sqldelight") } sqldelight { database(name = "TodoAppDatabase") { packageName = "io.github.wellingtoncosta.todoapp" } }

Slide 48

Slide 48 text

SQLDelight 
 // For Android target
 implementation("com.squareup.sqldelight:android-driver:1.3.0")

Slide 49

Slide 49 text

SQLDelight 
 // For Android target
 implementation("com.squareup.sqldelight:android-driver:1.3.0") // For iOS target implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.3.7")

Slide 50

Slide 50 text

SQLDelight // Todo.sq file
 CREATE TABLE todos ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, external_id TEXT UNIQUE DEFAULT NULL, title TEXT NOT NULL, description TEXT, status TEXT AS TodoStatus NOT NULL );

Slide 51

Slide 51 text

SQLDelight // Todo.sq file
 selectAll: SELECT * FROM todos; selectByExternalId: SELECT * FROM todos WHERE external_id = ?; insert: INSERT INTO todos (external_id, title, description, status) VALUES (?,?,?,?); updateStatus: UPDATE todos SET status = ? WHERE id = ?;

Slide 52

Slide 52 text

Tahdah!

Slide 53

Slide 53 text

• SQLDelight • Ktor Weapons starter pack

Slide 54

Slide 54 text

Ktor Ktor is an open source project sponsored by Jetbrains for building backend applications.

Slide 55

Slide 55 text

Ktor Ktor is an open source project sponsored by Jetbrains for building backend applications. It also has a HTTP client that supports several platforms using Kotlin Multiplatform.

Slide 56

Slide 56 text

Ktor Ktor is an open source project sponsored by Jetbrains for building backend applications. It also has a HTTP client that supports several platforms using Kotlin Multiplatform. Currently, the supported platforms are Android, JVM, iOS, JS and native.

Slide 57

Slide 57 text

Ktor 
 // For common module
 implementation(“io.ktor:ktor-client-core:1.3.2”)

Slide 58

Slide 58 text

Ktor 
 // For common module
 implementation(“io.ktor:ktor-client-core:1.3.2”) // For Android target implementation(“io.ktor:ktor-client-android:1.3.2”)

Slide 59

Slide 59 text

Ktor 
 // For common module
 implementation(“io.ktor:ktor-client-core:1.3.2”) // For Android target implementation(“io.ktor:ktor-client-android:1.3.2”) // For iOS target implementation(“io.ktor:ktor-client-ios:1.3.2”)

Slide 60

Slide 60 text

Ktor // Common code expect val engine: HttpClientEngineFactory

Slide 61

Slide 61 text

Ktor // Common code expect val engine: HttpClientEngineFactory val client = HttpClient(engine) { … }

Slide 62

Slide 62 text

Ktor // Common code expect val engine: HttpClientEngineFactory val client = HttpClient(engine) { install(JsonFeature) { serializer = KotlinxSerializer(json) } }

Slide 63

Slide 63 text

Ktor // Common code expect val engine: HttpClientEngineFactory val client = HttpClient(engine) { install(JsonFeature) { serializer = KotlinxSerializer(json) } install(Logging) { logger = Logger.SIMPLE level = LogLevel.ALL } … }

Slide 64

Slide 64 text

Ktor // Common code suspend fun getAll(): List { return client.get { url { path("todos") } } }

Slide 65

Slide 65 text

Ktor // Common code suspend fun getAll(): List { return client.get { url { path("todos") } } } suspend fun save(request: SaveTodoRequest): TodoResponse { return client.post { url { path("todos") } header(HttpHeaders.ContentType, ContentType.Application.Json) body = request } }

Slide 66

Slide 66 text

• SQLDelight • Ktor • Kotlin Serialization Weapons starter pack

Slide 67

Slide 67 text

Kotlin Serialization dependencies { classpath("org.jetbrains.kotlin:kotlin-serialization:1.3.72") }

Slide 68

Slide 68 text

Kotlin Serialization dependencies { classpath("org.jetbrains.kotlin:kotlin-serialization:1.3.72") } // Common code implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.20.0")

Slide 69

Slide 69 text

Kotlin Serialization dependencies { classpath("org.jetbrains.kotlin:kotlin-serialization:1.3.72") } // Common code implementation(“org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.20.0") // Android target implementation(“org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0")

Slide 70

Slide 70 text

Kotlin Serialization dependencies { classpath("org.jetbrains.kotlin:kotlin-serialization:1.3.72") } // Common code implementation(“org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.20.0") // Android target implementation(“org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0") // iOS target implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:0.20.0")

Slide 71

Slide 71 text

Kotlin Serialization @Serializable data class TodoResponse( val id: String, val title: String, val description: String? = null, val status: TodoStatus )

Slide 72

Slide 72 text

Kotlin Serialization @Serializable data class TodoResponse( val id: String, val title: String, val description: String? = null, val status: TodoStatus ) @Serializable data class SaveTodoRequest( val title: String, val description: String?, val status: TodoStatus )

Slide 73

Slide 73 text

• SQLDelight • Ktor • Kotlin Serialization • Kotlin Coroutines Weapons starter pack

Slide 74

Slide 74 text

• SQLDelight • Ktor • Kotlin Serialization • Kotlin Coroutines • Kotlin/Native Cocoapod plugin Weapons starter pack

Slide 75

Slide 75 text

Kotlin/Native Cocoapod plugin plugins { id("org.jetbrains.kotlin.native.cocoapods") }

Slide 76

Slide 76 text

Kotlin/Native Cocoapod plugin plugins { id("org.jetbrains.kotlin.native.cocoapods") } cocoapods { authors = "Wellington Costa Pereira" license = "MIT" summary = "Common core for Todo App." homepage = "https://github.com/wellingtoncosta/todo-app-kotlin-multiplatform" }

Slide 77

Slide 77 text

Kotlin/Native Cocoapod plugin Pod::Spec.new do |spec| spec.name = 'shared' spec.version = '1.0' spec.homepage = 'https://github.com/wellingtoncosta/todo-app-kotlin-multiplatform' spec.source = { :git => "Not Published", :tag => "Cocoapods/#{spec.name}/#{spec.version}" } spec.authors = 'Wellington Costa Pereira' spec.license = 'MIT' spec.summary = 'Common core for Todo App.' spec.static_framework = true spec.vendored_frameworks = "build/cocoapods/framework/shared.framework" spec.libraries = "c++" spec.module_name = "#{spec.name}_umbrella" spec.pod_target_xcconfig = { 'KOTLIN_TARGET[sdk=iphonesimulator*]' => 'ios_x64', 'KOTLIN_TARGET[sdk=iphoneos*]' => 'ios_arm', 'KOTLIN_TARGET[sdk=watchsimulator*]' => 'watchos_x86', 'KOTLIN_TARGET[sdk=watchos*]' => 'watchos_arm', 'KOTLIN_TARGET[sdk=appletvsimulator*]' => 'tvos_x64', 'KOTLIN_TARGET[sdk=appletvos*]' => 'tvos_arm64', 'KOTLIN_TARGET[sdk=macosx*]' => 'macos_x64' } spec.script_phases = [ { :name => 'Build shared', :execution_position => :before_compile, :shell_path => '/bin/sh', :script => <<-SCRIPT set -ev REPO_ROOT="$PODS_TARGET_SRCROOT" "$REPO_ROOT/../gradlew" -p "$REPO_ROOT" :shared:syncFramework \ -Pkotlin.native.cocoapods.target=$KOTLIN_TARGET \ -Pkotlin.native.cocoapods.configuration=$CONFIGURATION \ -Pkotlin.native.cocoapods.cflags="$OTHER_CFLAGS" \ -Pkotlin.native.cocoapods.paths.headers="$HEADER_SEARCH_PATHS" \ -Pkotlin.native.cocoapods.paths.frameworks="$FRAMEWORK_SEARCH_PATHS" SCRIPT } ] end

Slide 78

Slide 78 text

Kotlin/Native Cocoapod plugin target 'TodoApp' do # Comment the next line if you don't want to use dynamic frameworks use_frameworks! # Pods for TodoApp pod 'SnapKit', '~> 5.0.1' pod 'shared', :path => '../shared' target 'TodoAppTests' do inherit! :search_paths # Pods for testing end target 'TodoAppUITests' do # Pods for testing end end

Slide 79

Slide 79 text

Kotlin/Native Cocoapod plugin target 'TodoApp' do # Comment the next line if you don't want to use dynamic frameworks use_frameworks! # Pods for TodoApp pod 'SnapKit', '~> 5.0.1' pod 'shared', :path => '../shared' target 'TodoAppTests' do inherit! :search_paths # Pods for testing end target 'TodoAppUITests' do # Pods for testing end end

Slide 80

Slide 80 text

What’s next?

Slide 81

Slide 81 text

• Incremental builds What’s next?

Slide 82

Slide 82 text

• Incremental builds • Write tests What’s next?

Slide 83

Slide 83 text

• Incremental builds • Write tests • Modularization What’s next?

Slide 84

Slide 84 text

• Incremental builds • Write tests • Modularization • Experiment Bazel Build What’s next?

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

Questions?

Slide 87

Slide 87 text

Thank you! ;) wellingtoncosta wellingtoncosta128 [email protected]