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

A hitchhiker's guide to Kotlin MultiPlatform

Adit Lal
November 26, 2022

A hitchhiker's guide to Kotlin MultiPlatform

Where other technologies abstract away or completely replace platform-specific app development, Kotlin Multiplatform is complementary to existing platform-specific technologies and is geared towards replacing platform-agnostic business logic. It’s a new tool in the toolbox as opposed to replacing the toolbox, this talk would take you on a hitch-hiking journey across multiple platforms such as web , desktop and mobile apps all using KMP. We would pickup on where to get started , whats the future like and how to build faster with tips and tricks of the trade.

Adit Lal

November 26, 2022
Tweet

More Decks by Adit Lal

Other Decks in Programming

Transcript

  1. A hitchhiker's guide
    to Kotlin
    MultiPlatform
    Google Developers

    View Slide

  2. Android GDE

    🛠 Individual Consultant

    🔊 Speaker

    🌎 Globe Trotter

    🍻 Beer enthusiast
    🎯https:/
    /androiddev.social/@aditlal
    🔗aditlal.dev

    View Slide

  3. View Slide

  4. a wild idea to build apps pops!
    develop for Android and iOS

    View Slide

  5. ‣ hire a team - Android and iOS
    ‣ de
    fi
    ne requirements
    ‣ plan roadmap for development
    ‣ develop and maintain and test apps
    ‣ production validation
    ‣ cross-check parity among both platforms

    View Slide

  6. native
    ~2x the team
    the cost
    time spent in meetings
    time spent on development
    time spent on testing
    time spent on bug
    fi
    xing

    View Slide

  7. a wild idea to build apps pops!
    develop for Android and iOS

    View Slide

  8. a wild idea to build apps pops!
    and web
    develop for Android and iOS

    View Slide

  9. native + web
    ~3x the team
    the cost
    time spent in meetings
    time spent on development
    time spent on testing
    time spent on bug
    fi
    xing

    View Slide

  10. View Slide

  11. View Slide

  12. let’s
    fi
    nd a solution that works on all
    platforms.

    View Slide

  13. 2006 2008 2011 2012 2013 2015 2017 2018
    . . . . . . .

    View Slide

  14. Cross-platform advantages

    View Slide

  15. Cross-platform advantages
    • smaller team/s
    • typically half of the cost needed on native
    • small learning curve for web developers

    View Slide

  16. Cross-platform advantages
    dis
    • Locked to the framework implementation of UI
    - new updates from the OS will take time to adapt
    • performance is not on par to native
    • One may end up also writing native code alongside
    • OS / device features are dependent on the fw
    support

    View Slide

  17. how do we have the best of
    both worlds?

    View Slide

  18. how do we have the best of
    both worlds?

    View Slide

  19. Kotlin
    • developed by JetBrains
    • open source
    • concise, safe, interoperable, dev tool-friendly
    • Supported/used by Google for Android
    Development
    • It is so much more than “just for Android”

    View Slide

  20. Kotlin
    K
    otlin multiplatform
    Using kotlin in projects to
    target more than one platform

    View Slide

  21. Kotlin multiplatform
    “Shared UI is a history of pain
    and failure. Shared logic is the
    history of computers.”


    - Kevin Galligan

    View Slide

  22. Kotlin multiplatform
    - language features
    - kotlin
    fi
    rst!
    - low risk
    - you decide what’s worth to share across projects
    - interoperability
    - consistency across platforms
    - strong community support

    View Slide

  23. Kotlin multiplatform
    /kot · lin mul · ti · plat · form /


    / kät' lin malti' platfôrm, kät'lin melti'platfôrm/


    noun
    optional, natively-integrated, open-source, code sharing platform, based on
    the popular, modern language kotlin. facilitates non-ui logic availability on
    many platforms.

    View Slide

  24. Kotlin multiplatform

    View Slide

  25. Kotlin multiplatform

    View Slide

  26. Kotlin multiplatform

    View Slide

  27. Kotlin multiplatform
    but how can they expect to communicate?

    View Slide

  28. Kotlin multiplatform
    but how can they expect to communicate?
    expect
    declared at common module 

    actual
    declared at android module
    declared at iOS module
    declared at ...

    View Slide

  29. Kotlin multiplatform
    Platform specif
    i
    c
    names?

    View Slide

  30. Kotlin multiplatform
    Platform | common

    View Slide

  31. Kotlin multiplatform
    commonMain
    src/commonMain/sample/Platform.kt


    expect object Platform {


    val name: String


    }

    View Slide

  32. Kotlin multiplatform
    src/commonMain/sample/Platform.kt


    expect object Platform {


    val name: String


    }

    View Slide

  33. Kotlin multiplatform
    src/commonMain/sample/Platform.kt


    expect object Platform {


    val name: String


    }
    targets: android and iOS or macOS

    de
    fi
    ne actual on android
    de
    fi
    ne actual on iOS

    de
    fi
    ne actual on macOS

    View Slide

  34. Kotlin multiplatform
    src/androidMain/sample/Platform.kt
    actual object Platform {


    actual val name: String = "Android"


    }
    platform-dependent code

    View Slide

  35. Kotlin multiplatform
    platform-dependent code
    actual object Platform {


    actual val name: String = “iOS"


    }
    src/iOSMain/sample/Platform.kt

    View Slide

  36. Kotlin multiplatform
    src/androidMain/sample/Platform.kt
    actual object Platform {


    actual val name: String = "Android"


    }
    platform-dependent code
    actual object Platform {


    actual val name: String = “iOS"


    }
    src/iOSMain/sample/Platform.kt

    View Slide

  37. Kotlin multiplatform
    platform-dependent code
    actual object Platform {


    actual val name: String = “macOS”


    }
    src/macosMain/sample/Platform.kt

    View Slide

  38. Kotlin multiplatform
    platform-dependent code
    import io.ktor.*
    fun main() {
    embeddedServer(Netty, 9090) {
    routing {
    get("/hello") {
    call.respondText("Hello, API!")
    }
    }
    }.start(wait = true)
    }
    src/jvmMain/kotlin/Server.kt

    View Slide

  39. Kotlin multiplatform

    View Slide

  40. Kotlin multiplatform
    Actual needs to match
    because obviously it does

    View Slide

  41. Kotlin multiplatform

    View Slide

  42. Kotlin multiplatform

    View Slide

  43. Common

    Kotlin
    Kotlin/JS
    JS code
    Kotlin/JVM
    JVM code
    Kotlin/Native
    Native code
    Kotlin multiplatform

    View Slide

  44. Kotlin multiplatform
    Kotlin
    How much do we share
    Multiplatform
    What makes most sense

    View Slide

  45. Kotlin multiplatform
    - strong community
    - a lot of people are using kotlin nowadays
    - Google and JetBrains are pushing Kotlin exclusively
    - you can ask questions directly at https://kotlinlang.slack.com
    conclusions

    View Slide

  46. Kotlin multiplatform
    - 2x faster to develop your features business logic
    - 2x faster writing unit tests
    - One tech stack
    - consistency achieved across platforms
    conclusions

    View Slide

  47. Kotlin multiplatform
    - start small, don’t try to reach 100% of shared logic
    - be extremely careful with all updates
    - IDEA, JDK, kotlin, libraries - everything.
    - keep versioning in mind
    Suggestions

    View Slide

  48. Kotlin multiplatform
    Kotlin Slack
    Kotlin Of
    fi
    cial
    Kotlin Training
    Kotlin by:
    - https://jakewharton.com/presentations/
    - http://antonioleiva.com/kotlin
    Resources

    View Slide

  49. Kotlin multiplatform
    https://github.com/joreilly/PeopleInSpace


    About
    Kotlin Multiplatform project with SwiftUI, Jetpack Compose, Wear Compose,
    Compose for Desktop, Compose for Web and Kotlin/JS + React clients along
    with Ktor backend.
    Resources

    View Slide

  50. Thats all folks!
    https://cal.com/adit/30min
    🎯https:/
    /androiddev.social/@aditlal
    🔗aditlal.dev

    View Slide