Slide 1

Slide 1 text

Into the Multi-Verse Nishant Srivastava @nisrulz otlin MP:

Slide 2

Slide 2 text

Kotlin MP? @nisrulz #fosdem

Slide 3

Slide 3 text

Kotlin MP: Kotlin MultiPlatform “Utilizing Kotlin language to build for multiple target platforms,enabling code sharing across them while being as flexible as can be” @nisrulz #fosdem

Slide 4

Slide 4 text

Kotlin MP: Kotlin MultiPlatform ❏ Open Source ❏ Uses Kotlin language for common code ❏ Multiple target platforms ❏ Code sharing of data/domain/presentation layer @nisrulz #fosdem

Slide 5

Slide 5 text

Kotlin MP: Kotlin MultiPlatform ❏ Integrates with existing native platform ❏ Leverage native platform capabilities when needed ❏ Optional i.e not opinionated @nisrulz #fosdem

Slide 6

Slide 6 text

But why? @nisrulz #fosdem

Slide 7

Slide 7 text

But why? ❏ Introduction of Kotlin/Native in the toolchain enabled targeting more platforms than just JS and JVM ❏ Existing solutions are very opinionated @nisrulz #fosdem

Slide 8

Slide 8 text

But why? @nisrulz #fosdem

Slide 9

Slide 9 text

Into the Multi-Verse of Platforms ...or Target Platforms @nisrulz #fosdem ꩜

Slide 10

Slide 10 text

Target Platforms Compiled artifacts to be consumed by specific platform. Kotlin/JVM → JAR/AAR → Java, Android, Spring Boot Kotlin/JS → JS → Javascript, React, Node @nisrulz #fosdem

Slide 11

Slide 11 text

Target Platforms Compiled artifacts to be consumed by specific platform. Kotlin/Native → ● androidNativeArm32 and androidNativeArm64 for Android NDK ● iosArm32, iosArm64, iosX64 for iOS ● watchosArm32, watchosArm64, watchosX86 for watchOS ● tvosArm64, tvosX64 for tvOS @nisrulz #fosdem

Slide 12

Slide 12 text

Target Platforms Compiled artifacts to be consumed by specific platform. Kotlin/Native → ● linuxArm32Hfp, linuxMips32, linuxMipsel32, linuxX64 for Linux ● macosX64 for MacOS ● mingwX64 and mingwX86 for Windows ● wasm32 for WebAssembly @nisrulz #fosdem

Slide 13

Slide 13 text

Code Sharing @nisrulz #fosdem

Slide 14

Slide 14 text

Code Sharing ❏ Only share common code ❏ Data layer → Networking, Caching, Repositories ❏ Domain layer → Entities, Interactors, Use Cases ❏ Presentation layer → ViewModel, Presenter, Controller ❏ Keep the UI separate and native to its respective platform. @nisrulz #fosdem

Slide 15

Slide 15 text

...but not Cross Platform Cross Platform Solutions: ❏ Makes you write code in opinionated way i.e Flutter, dart ❏ Eventually map all the magic back to native (with or without a bridge) i.e React Native, Xamarin, NativeScript @nisrulz #fosdem

Slide 16

Slide 16 text

What’s the secret sauce? @nisrulz #fosdem

Slide 17

Slide 17 text

Expect / Actual Interfaces with SuperPowers ‍♂ ...and more @nisrulz #fosdem

Slide 18

Slide 18 text

Interface interface MyInterface{ fun platformName(): String } class MainActivity : MyInterface { override fun platformName(): String = "Android" fun createApplicationScreenMessage() : String { return "Hello from ${platformName()}" } } @nisrulz #fosdem

Slide 19

Slide 19 text

Interface interface MyInterface{ fun platformName(): String // ← Expecting this to be implemented } class MainActivity : MyInterface { override fun platformName(): String = "Android" // ← Actual implementation fun createApplicationScreenMessage() : String { return "Hello from ${platformName()}" } } @nisrulz #fosdem

Slide 20

Slide 20 text

Expect/Actual // Common module expect fun platformName(): String // ← Expecting this to be implemented fun createApplicationScreenMessage() : String { return "Hello from ${platformName()}" } // Platform specific (Android) module actual fun platformName(): String = "Android" // ← Actual implementation // Platform specific (iOS) module actual fun platformName(): String = "iOS" // ← Actual implementation @nisrulz #fosdem

Slide 21

Slide 21 text

Expect/Actual // Common module expect class Greeting(name: String) { fun greet() } // Platform specific (Android) module actual class Greeting actual constructor(val name: String) { actual fun greet() { println("Hello $name") } } // Usage Greet("FOSDEM").greet() // Hello FOSDEM @nisrulz #fosdem

Slide 22

Slide 22 text

Kickstart KMP Development @nisrulz #fosdem

Slide 23

Slide 23 text

Sharing is Caring Many ways of sharing code ❏ Every target platform’s code lives in the same repo/project along with the shared code. ❏ Harder to work in a large team. @nisrulz #fosdem

Slide 24

Slide 24 text

Sharing is Caring Many ways of sharing code ❏ Everything is in its own repo/project, while Shared code is itself a different project. ❏ Harder to maintain. @nisrulz #fosdem

Slide 25

Slide 25 text

Sharing is Caring @nisrulz #fosdem

Slide 26

Slide 26 text

Sharing is Caring Many ways of sharing code ❏ One of the platform includes the shared code, while others refer to it from outside. ❏ Better change/test cycle @nisrulz #fosdem

Slide 27

Slide 27 text

Sharing is Caring @nisrulz #fosdem

Slide 28

Slide 28 text

Kickstart KMP Development @nisrulz #fosdem

Slide 29

Slide 29 text

Kickstart KMP Development @nisrulz #fosdem

Slide 30

Slide 30 text

Kickstart KMP Development @nisrulz #fosdem

Slide 31

Slide 31 text

Kickstart KMP Development @nisrulz #fosdem

Slide 32

Slide 32 text

Kickstart KMP Development @nisrulz #fosdem

Slide 33

Slide 33 text

Kickstart KMP Development @nisrulz #fosdem

Slide 34

Slide 34 text

Kickstart KMP Development @nisrulz #fosdem

Slide 35

Slide 35 text

Kickstart KMP Development @nisrulz #fosdem

Slide 36

Slide 36 text

Kickstart KMP Development @nisrulz #fosdem // app/build.gradle plugins { id 'org.jetbrains.kotlin.multiplatform' version '1.3.61' } ...

Slide 37

Slide 37 text

Kickstart KMP Development @nisrulz #fosdem // app/build.gradle kotlin { android("android") // This is for iPhone emulator // Switch here to iosArm64 (or iosArm32) to build library for iPhone device iosX64("ios"){ binaries { framework() } } sourceSets{...} }

Slide 38

Slide 38 text

Kickstart KMP Development @nisrulz #fosdem // app/build.gradle kotlin { android("android") // This is for iPhone emulator // Switch here to iosArm64 (or iosArm32) to build library for iPhone device tvosX64("tvos"){ binaries { framework() } } sourceSets{...} }

Slide 39

Slide 39 text

Kickstart KMP Development @nisrulz #fosdem // app/build.gradle kotlin { ... sourceSets { commonMain.dependencies { implementation kotlin('stdlib-common') } commonTest {...} androidMain.dependencies { implementation kotlin('stdlib') } androidTest {...} iosMain {...} iosTest {...} } }

Slide 40

Slide 40 text

Kickstart KMP Development @nisrulz #fosdem

Slide 41

Slide 41 text

Kickstart KMP Development @nisrulz #fosdem

Slide 42

Slide 42 text

Kickstart KMP Development @nisrulz #fosdem

Slide 43

Slide 43 text

Kickstart KMP Development @nisrulz #fosdem

Slide 44

Slide 44 text

Kickstart KMP Development @nisrulz #fosdem

Slide 45

Slide 45 text

Kickstart KMP Development @nisrulz #fosdem // Error line 2: /Users/nishant/Desktop/kmp-examples/BasicKMPProject/iosApp/../gradlew: No such file or directory Command PhaseScriptExecution failed with a nonzero exit code

Slide 46

Slide 46 text

Kickstart KMP Development @nisrulz #fosdem

Slide 47

Slide 47 text

Kickstart KMP Development @nisrulz #fosdem

Slide 48

Slide 48 text

Examples in the Wild @nisrulz #fosdem

Slide 49

Slide 49 text

Apps in Production by ❏ PlanGrid ❏ CashApp ❏ Careem ❏ VMware ❏ Quizlet ❏ Target ❏ SuperAwesome @nisrulz #fosdem

Slide 50

Slide 50 text

Apps in Production by ❏ Infinut ❏ Touchlab/DroidconKotlin ❏ Jetbrains/Kotlinconf ❏ Jetbrain/Spaces ❏ Walpy @nisrulz #fosdem

Slide 51

Slide 51 text

Kotlin MultiPlatform Libraries ❏ Find a list here: https://github.com/AAkira/Kotlin-Multiplatform-Libraries Note: ❏ The ecosystem is still evolving ❏ Not many libraries at disposal ❏ Not all platforms supported in all libraries @nisrulz #fosdem

Slide 52

Slide 52 text

Resources ❏ https://play.kotlinlang.org/hands-on/Targeting%20iOS%20an d%20Android%20with%20Kotlin%20Multiplatform/ ❏ https://github.com/nisrulz/kmp-examples @nisrulz #fosdem

Slide 53

Slide 53 text

Thank You @nisrulz #fosdem twitter.com/nisrulz github.com/nisrulz www.nisrulz.com

Slide 54

Slide 54 text

Into the Multi-Verse Nishant Srivastava @nisrulz otlin MP: twitter.com/nisrulz github.com/nisrulz www.nisrulz.com