Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin MP: Into the Multi-Verse

Kotlin MP: Into the Multi-Verse

Kotlin Multiplatform is the new kid on the cross-platform block. The approach although is very different from what you have seen in the past. The new approach utilizes Kotlin Native to compile Kotlin language to native binaries for specific target platform which can run without a virtual machine. Thus enabling simplified code sharing across multiple platforms. In this talk, you will be introduced to Kotlin/Native and demonstrate how to build a Kotlin Multiplatform app that runs on both iOS and Android using shared Kotlin code.

Event Link: https://fosdem.org/2020/schedule/event/kotlin_mp_into_the_multi_verse/

Nishant Srivastava

February 02, 2020
Tweet

More Decks by Nishant Srivastava

Other Decks in Technology

Transcript

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

    View Slide

  2. Kotlin MP?
    @nisrulz #fosdem

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  6. But why?
    @nisrulz #fosdem

    View Slide

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

    View Slide

  8. But why?
    @nisrulz #fosdem

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  13. Code Sharing
    @nisrulz #fosdem

    View Slide

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

    View Slide

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

    View Slide

  16. What’s the secret
    sauce?
    @nisrulz #fosdem

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  22. Kickstart KMP
    Development
    @nisrulz #fosdem

    View Slide

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

    View Slide

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

    View Slide

  25. Sharing is Caring
    @nisrulz #fosdem

    View Slide

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

    View Slide

  27. Sharing is Caring
    @nisrulz #fosdem

    View Slide

  28. Kickstart KMP Development
    @nisrulz #fosdem

    View Slide

  29. Kickstart KMP Development
    @nisrulz #fosdem

    View Slide

  30. Kickstart KMP Development
    @nisrulz #fosdem

    View Slide

  31. Kickstart KMP Development
    @nisrulz #fosdem

    View Slide

  32. Kickstart KMP Development
    @nisrulz #fosdem

    View Slide

  33. Kickstart KMP Development
    @nisrulz #fosdem

    View Slide

  34. Kickstart KMP Development
    @nisrulz #fosdem

    View Slide

  35. Kickstart KMP Development
    @nisrulz #fosdem

    View Slide

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

    View Slide

  37. 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{...}
    }

    View Slide

  38. 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{...}
    }

    View Slide

  39. 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 {...}
    }
    }

    View Slide

  40. Kickstart KMP Development
    @nisrulz #fosdem

    View Slide

  41. Kickstart KMP Development
    @nisrulz #fosdem

    View Slide

  42. Kickstart KMP Development
    @nisrulz #fosdem

    View Slide

  43. Kickstart KMP Development
    @nisrulz #fosdem

    View Slide

  44. Kickstart KMP Development
    @nisrulz #fosdem

    View Slide

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

    View Slide

  46. Kickstart KMP Development
    @nisrulz #fosdem

    View Slide

  47. Kickstart KMP Development
    @nisrulz #fosdem

    View Slide

  48. Examples in the Wild

    @nisrulz #fosdem

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide


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

    View Slide

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

    View Slide