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

Kotlin Multiplatform Mobile for Skeptics

pahill
June 17, 2022

Kotlin Multiplatform Mobile for Skeptics

Kotlin Multiplatform Mobile (or KMM) is a new SDK from JetBrains that allows iOS and Android developers to share code between the two platforms. The shared code is written in Kotlin. Typical use cases include: sharing networking, data storage, internal libraries, and algorithms, but the choice is yours!

While most Android developers love writing their apps in Kotlin, iOS developers are understandably wary of adopting KMM. This talk will give both iOS and Android developers the tools to confidently get started with KMM. This talk will include:
- An introduction of KMM and its benefits.
- A demo showing how to create a basic app with KMM.
- An argument for KMM, based on my research and journey from KMM skeptic to realist. I’ll also share the thoughts and experiences of others.

By the end of this talk, I will have covered enough content for teams to get started with KMM in their existing codebases and start reaping the rewards of this exciting new technology.

pahill

June 17, 2022
Tweet

More Decks by pahill

Other Decks in Programming

Transcript

  1. – Table of contents – 01 - Introduction 02 -

    How it works 03 - Convincing me (and you) 04 - Resources and closing
  2. What is Kotlin Multiplatform Mobile? • SDK by JetBrains •

    Allows development teams to share common code between iOS and Android platforms • Doesn't impose restrictions on non-shared code
  3. How can you share code? Business / Domain (Entities, Use

    Cases, Interactors) Data / Core (Repositories, HTTP Clients, Cache) UI (Views) Presentation (Presenters, View Models, Controllers) iOS App UI (Views) Presentation (Presenters, View Models, Controllers) Android App Multiplatform Mobile Module (Shared code)
  4. How can you share code? Business / Domain (Entities, Use

    Cases, Interactors) Data / Core (Repositories, HTTP Clients, Cache) UI (Views) iOS App UI (Views) Presentation (Presenters, View Models, Controllers) Android App Multiplatform Mobile Module (Shared code)
  5. What are the benefits of Kotlin Multiplatform Mobile? • Flexibility

    to choose what is shared and what not • Shared code lowers the effort, cost per feature • Shared code ensures consistency amongst platforms
  6. Tools needed • Android Studio with Kotlin Multiplatform Mobile plugin

    • Java Development Kit (JDK) • Xcode on MacOS from App Store
  7. Kotlin Multiplatform Mobile Plugin • Create a new multiplatform project/library

    • Write shared code once, share on Android and iOS • Run and debug your app on iOS targets from Android Studio
  8. iOS APIs iOS App (ContentView.swift) Android App (MainActivity.kt) Shared code

    (Greeting) Android APIs iOS shared code (Platform) Android shared code (Platform) Uses Uses Calls Calls Calls
  9. class Greeting { private val platform: Platform = getPlatform() fun

    greeting(): String { return "Hello, ${platform.name}!" } } shared/commonMain/…/Greeting.kt
  10. interface Platform { val name: String } expect fun getPlatform():

    Platform shared/commonMain/…/Platform.kt
  11. class AndroidPlatform : Platform { override val name: String =

    "Android ${android.os.Build.VERSION.SDK_INT}" } actual fun getPlatform(): Platform = AndroidPlatform() shared/androidMain/…/Platform.kt
  12. class IOSPlatform : Platform { override val name: String =

    UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion } actual fun getPlatform(): Platform = IOSPlatform() shared/iosMain/…/Platform.kt
  13. class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContent { MyApplicationTheme { Surface( modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background ) { Text(Greeting().greeting()) } } } } } androidApp/…/MainActivity.kt
  14. @main struct iOSApp: App { var body: some Scene {

    WindowGroup { ContentView() } } } iosApp/…/iOSApp.swift
  15. struct ContentView: View { let greet = Greeting().greeting() var body:

    some View { Text(greet) } } iosApp/…/ContentView.swift
  16. What did we just do? • Wrote some shared code

    (Greeting) • Used shared code in Android, iOS (MainActivity, ContentView) • Used platform-specific functionality from the shared code (Platform)
  17. What I thought before investigating • “iOS developers won’t want

    to adopt this!” • “Will the Swift to Kotlin, Kotlin to Swift interop not be clunky?” • “Will it really lower the cost of development?” • “Won’t it be more frustrating than writing two native apps? Or slower than using Flutter/React Native?”
  18. My process of investigating (Pt. 1) • Kotlin Youtube channel

    - Series by Kate Petrova (JetBrains) • Kotlin official site - “Case studies” • Medium - “Kotlin Multiplatform Mobile - An iOS engineer’s hot take” by Malcolm Kumwenda • Talking Kotlin podcast - “Why iOS developers at Todoist wanted Kotlin Multiplatform”
  19. My process of investigating (Pt. 2) • Book 1: Simplifying

    application development with KMM by Robert Nagy • Book 2: Kotlin Multiplatform by Tutorials by Ray Wenderlich tutorial team
  20. What changed my mind (Pt. 2) • It’s not about

    taking the fun out of native development • Flexibility to choose what you share and what not • Consistency across platforms • Lower cost of development - although maybe not initially
  21. What changed my mind (Pt. 3) • Quite a number

    of mature libraries already supporting KMM • It will help under-resourced teams • JetBrains’ history and dedication to the platform
  22. What I still have doubts about • Is there a

    wide-enough ecosystem to support my app’s needs? • Is there enough support to answer my questions? • What is the iOS developer experience?
  23. Pamela Hill @pamelaahill - Jun 10 Hey Mobile Dev Twitter

    Have you used #kotlin multiplatform mobile (#kmm) before? What convinced you to give it a try? What convinced you to use it in more than just a play app? Thanks! #AndroidDev #iosdev https://twitter.com/pamelaahill/status/1535151498958618625
  24. Take it to Twitter (Pt. 1) - Anonymous Flutter Guy

    • Not sharing UI is a huge con, that’s the real time saver • Language shouldn’t be the centre of a tech decision • Resources behind Kotlin Multiplatform Mobile can never match the resources Google puts into Flutter • In summary: Kotlin Multiplatform Mobile has a questionable advantages and a questionable future
  25. Take it to Twitter (Pt. 2) - Anonymous Multiplatform Mobile

    Guys • Writing and testing business logic once is a huge advantage • Don’t have to commit your entire codebase to a new technology, you can start small and keep adding • Better looking native UIs than Material Design-first approach of Flutter • Some of your cross-functional team are already familiar with the tools and language
  26. Take it to Twitter (Pt. 3) - Anonymous Googler •

    Agreed - language isn’t enough to make a tech decision • Kotlin Multiplatform Mobile is different from other cross-platform technologies • Kotlin/Native works directly with native libraries - no waiting for plugins • Adoption is not all or nothing, spectrum based on use case, needs of the team
  27. CREDITS: This presentation template was created by Slidesgo, and includes

    icons by Flaticon, and infographics & images by Freepik Pamela Hill @pamelaahill pamelaahill.com Thanks!