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

Introducing Kotlin Multiplatform in an existing mobile app - Workshop Edition | DevFest Berlin 23

Introducing Kotlin Multiplatform in an existing mobile app - Workshop Edition | DevFest Berlin 23

After discovering a new interesting technology or framework, you will probably start asking yourself how to integrate it into an existing project. That's because, the possibility of starting with a blank canvas is rare (not impossible, but rare).

This is also the case for Kotlin Multiplatform, which is getting more and more hype every day. And now, that the technology has become stable, it's the perfect time to start using it!
In this hands-on session, we will start with an existing Android and iOS application that "lives" in separate repositories, we will extract the business logic from the Android app and share it between the two apps with a Kotlin Multiplatform library. We will also cover how to distribute the library to the existing applications.
By the end of this workshop, you'll have a better understanding of what is needed to start using Kotlin Multiplatform in your existing projects.

Marco Gomiero

November 25, 2023
Tweet

More Decks by Marco Gomiero

Other Decks in Programming

Transcript

  1. 👨💻 Senior Android Developer @ Airalo
    Google Developer Expert for Kotlin
    Marco Gomiero
    @marcoGomier
    Introducing
    Kotlin Multiplatform in
    an existing mobile app
    workshop edition

    View full-size slide

  2. @marcoGomier
    bit.ly/kmp-workshop-devfestberlin

    View full-size slide

  3. @marcoGomier
    Agenda
    • Download Android Studio and the Kotlin Multiplatform Plugin
    • Download Xcode
    • Quick intro about Kotlin Multiplatform
    • Start from an Android and iOS native app - that lives in separate repositories
    • Extract the business logic into a new KMP library
    • [Manually and locally] Deploy the library
    • Replace the business logic on Android and iOS with the KMP library
    • [BONUS] iOS API enhancements
    • [BONUS] How to deploy in a scalable way

    View full-size slide

  4. @marcoGomier
    What is Kotlin Multiplatform?

    View full-size slide

  5. @marcoGomier
    “Classic” Cross Platform Solutions
    • All-in approach
    • Everything is shared, UI included
    • Different platforms have different patterns

    View full-size slide

  6. @marcoGomier
    Kotlin Multiplatform
    • Incremental approach
    • You choose what to share (even UI, if you want)

    View full-size slide

  7. @marcoGomier
    Photo by Erwan Hesry on Unsplash
    💡 Create a library!

    View full-size slide

  8. @marcoGomier
    Common Kotlin
    Kotlin/JVM Kotlin/JS
    Kotlin/Native
    JVM
    Android
    Browser
    NodeJS
    iOS
    macOS
    watchOS
    tvOS
    Linux
    Windows
    Supported Platforms

    View full-size slide

  9. @marcoGomier
    Common Kotlin
    Kotlin/JVM Kotlin/JS
    Kotlin/Native
    JVM
    Android
    Browser
    NodeJS
    iOS
    macOS
    watchOS
    tvOS
    Linux
    Windows
    Mobile

    View full-size slide

  10. @marcoGomier
    How it works?

    View full-size slide

  11. @marcoGomier
    shared
    androidApp iosApp
    Library Library
    iOS
    Android

    View full-size slide

  12. @marcoGomier
    shared
    androidApp
    Gradle Module
    Android
    Same repository

    View full-size slide

  13. @marcoGomier
    shared
    androidApp
    .aar
    Android
    Different repositories

    View full-size slide

  14. @marcoGomier
    iosApp
    iOS
    shared
    XCFramework
    Different repositories
    Same repository

    View full-size slide

  15. @marcoGomier
    XCFramework
    https://developer.apple.com/documentation/xcode/creating-a-multi-platform-binary-framework-bundle

    View full-size slide

  16. @marcoGomier
    NEW
    https://devstreaming-cdn.apple.com/videos/wwdc/2019/416h8485aty341c2/416/416_binary_frameworks_in_swift.pdf

    View full-size slide

  17. @marcoGomier https://devstreaming-cdn.apple.com/videos/wwdc/2019/416h8485aty341c2/416/416_binary_frameworks_in_swift.pdf
    NEW
    iOS mac
    OS
    watch
    OS
    tvOS

    View full-size slide

  18. @marcoGomier
    shared
    androidApp iosApp
    Gradle
    Module
    XCFramework
    Same Repository
    Easier for new projects

    View full-size slide

  19. @marcoGomier
    Common Kotlin
    Android App iOS App
    .aar XCFramework
    Android App Repository
    KMP Repository
    iOS App Repository
    Easier for existing projects

    View full-size slide

  20. @marcoGomier
    Code Time 🧑💻

    View full-size slide

  21. @marcoGomier
    bit.ly/kmp-existing-workshop

    View full-size slide

  22. @marcoGomier
    Code Time 🧑💻

    View full-size slide

  23. @marcoGomier
    XCFramework
    https://developer.apple.com/documentation/xcode/creating-a-multi-platform-binary-framework-bundle

    View full-size slide

  24. @marcoGomier
    iOS Dependency Managers
    • CocoaPods
    • Carthage
    • Swift Package Manager
    💀
    💀

    View full-size slide

  25. @marcoGomier
    Swift Package Manager
    • Configured with a Package.swift file
    • Based on git
    • Semantic versioning (e.g. 1.2.3) with git tags
    • Can contain code or binaries
    • Can be local or remote, it’s git!

    View full-size slide

  26. @marcoGomier
    Code Time 🧑💻

    View full-size slide

  27. @marcoGomier
    Kotlin
    ->
    Obj-c
    ->
    Swift

    View full-size slide

  28. @marcoGomier
    Some gotchas
    • No namespaces
    • No default parameters
    • Enums are not Swift-friendly (no values)
    • Sealed classes are simple classes
    • Coroutines without cancellation
    • Flows

    View full-size slide

  29. @marcoGomier
    Code Time 🧑💻

    View full-size slide

  30. @marcoGomier
    Swift Package Manager
    • Manual is boring and not scalable

    View full-size slide

  31. @marcoGomier
    Code Time 🧑💻

    View full-size slide

  32. @marcoGomier
    Swift Package Manager
    • Manual is boring and not scalable
    • Use some tooling and public artifactories

    View full-size slide

  33. @marcoGomier
    Conclusions
    • You choose what to share (even UI, if you want)
    • Validate the process with “little” effort
    • Then you can go bigger and share more “features”

    View full-size slide

  34. Bibliography / Useful Links
    • https:
    /
    /
    developer.apple.com/documentation/xcode/creating-a-multi-platform-binary-framework-
    bundle
    • https:
    /
    /
    devstreaming-cdn.apple.com/videos/wwdc/
    2019/416h8485aty341c2/416/416_binary_frameworks_in_swift.pdf
    • https:
    /
    /
    kotlinlang.org/docs/multiplatform-build-native-binaries.html
    • https:
    /
    /
    developer.apple.com/documentation/xcode/distributing-binary-frameworks-as-swift-
    packages
    • https:
    /
    /
    github.com/rickclephas/KMP-NativeCoroutines
    • https:
    /
    /
    skie.touchlab.co/
    • https:
    /
    /
    github.com/luca992/multiplatform-swiftpackage
    • https:
    /
    /
    kmmbridge.touchlab.co/
    • https:
    /
    /
    medium.com/geekculture/xcode-13-3-supports-spm-binary-dependency-in-private-github-
    release-8d60a47d5e45

    View full-size slide

  35. @marcoGomier
    Thank you!
    > Twitter: @marcoGomier
    > Github: prof18
    > Website: marcogomiero.com
    > Mastodon: androiddev.social/@marcogom
    👨💻 Senior Android Developer @ Airalo
    Google Developer Expert for Kotlin
    Marco Gomiero
    bit.ly/kmp-workshop-devfestberlin

    View full-size slide