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

Going Kotlin Multiplatform

Going Kotlin Multiplatform

Selling multi-million burgers with Kotlin Multiplatform.

Talk on GDG Android Stockholm

https://www.meetup.com/gdg-android-stockholm/events/295582535/

Jackson F. de A. Mafra

October 12, 2023
Tweet

More Decks by Jackson F. de A. Mafra

Other Decks in Programming

Transcript

  1. Who am I? My name is Jackson Mafra, I'm a

    brazilian software engineer with 25+ years of experience. I've spent 12 years in Android, 8 years in iOS (Objective-C & Swift), and the last 2 years exploring Kotlin Multiplatform. Passionate about crafting innovative mobile applications.
  2. How Does Multiplatform Differ From Cross Platform? A cross-platform application

    is a system that allows an application to be deployed with the use of the exact same application code. Is a system where a program for different platforms uses both of the codes specific to each platform together with a common code, shared by both the platforms. Multiplatform Cross-platform Like Flutter(Dart) and React Native(JS) , and MAUI(.NET)
  3. Google Workspace Still migrating the Google Docs, Slides, Sheets, Drive,

    and Meet. The existing apps have billions of downloads and Google is in the process of migrating from their in-house J2ObjC transpiler to KMP.
  4. Introduction to Kotlin Multiplatform (KMP): KMP is distinct from Kotlin/JS

    and Kotlin/Native as it specifically focuses on mobile app development. Kotlin Multiplatform Mobile (KMP) is a technology (cross-compiled development framework) developed by JetBrains and uses Kotlin. KMP enables developers to write shared code that can be used across different platforms, such as Android and iOS, making it easier to create cross-platform mobile applications. “Kotlin Multiplatform Mobile” (KMM) is deprecated. JetBrains first introduced the Multiplatform feature in the Kotlin 1.2, released in 2017. The Kotlin/JVM compiler targets the Java Virtual Machine (JVM) and Android. The Kotlin/Native compiler targets the platforms macOS, iOS, tvOS, watchOS, Linux, Windows, and Android NDK.
  5. Kotlin Multiplatform Mobile Is Dead. Long Live Kotlin Multiplatform About

    the deprecation of the Kotlin Multiplatform Mobile (KMM) term. During his keynote at Droidcon Berlin 2023, Hadi Hariri announced an update that has now become the official guideline. From now on, the mobile development aspect of Kotlin Multiplatform will no longer be referred to as a separate product called "Kotlin Multiplatform Mobile (KMM)." Instead, we will use the unified term "Kotlin Multiplatform (KMP)." https://medium.com/tech-takeaways/kotlin-multiplatform-mobile-is-dead-long-live-kotlin-multiplatform-2b107227b2d0
  6. “Kotlin Multiplatform Mobile (KMM) is an older name. It used

    to mean the Android & iOS specific parts of KMP, but JetBrains now says there’s no need to differentiate those front-ends from desktop, web and also back-end. It’s all called KMP from hereon!” —Someone Famous
  7. Compose Multiplatform (CMP): CMP is based on Jetpack Compose and

    developed by JetBrains and open-source contributors. You can choose the platforms across which to share your UIs using Compose Multiplatform: • iOS (Alpha) • Android (via Jetpack Compose) • Desktop (Windows, MacOS, Linux) • Web (Experimental) Compose Multiplatform (CMP) is a part of KMP that deals specifically with creating user interfaces (UIs) that can be shared between Android and iOS. KMP, on the other hand, is a broader framework for sharing business logic and data across platforms. Compose Multiplatform and KMM are often used together, but they serve different purposes.
  8. Why we chose it? This approach allows developers to reuse

    their existing Kotlin skills and tools while leveraging the benefits of native development on each platform. Unlike cross platform solutions out there, KMP is plug and play, so there's not much work needed on the app side, just plug in the KMP code like any other library/SDK/framework. Performance Reuse skills Control your decisions You decide how much code you share across platforms. Since your apps are native, there's no performance or ux downsides. Plug and play
  9. Why we chose it? And companies like @TouchlabHQ and @CashApp

    are pretty bullish on it, and creating lots of new libraries enhancing the ecosystem. Getting #Kotlin developers is an easier task than cross platforms out there. Especially when it comes to senior developers. Productivity Production ready JVM in Backend If your backend is in JVM, you can share the models (pojo / data classes), between your backend and frontend for a seamless development. Speeds up development by allowing teams to work on both platforms simultaneously. Developers
  10. Sharing code Sharing code has always been a trend, it’s

    DRY (Don’t Repeat Yourself). But it’s important to understand what you can share. Kotlin’s philosophy is different from React Native. It aims to replace specific parts of the code, not everything. According to the website, it focuses on networking, data storage/validation, and business logic.
  11. Why we chose it? Native Interoperability You can use any

    native library from any platform, as long as it’s available on the JVM. You can mix and match libraries from different platforms (e.g., Objective-C and Java) without overhead or cost. For example, an Android developer can use a powerful iOS SDK like ARKit without understanding new programming languages or frameworks. Kotlin also makes it possible to work with JavaScript libraries like ReactJS and NodeJS by running code through a JavaScript engine, such as Nashorn.
  12. Use cases You can create a multiplatform library with common

    code and its platform-specific implementations for JVM, web, and native platforms. Once published, a multiplatform library can be used as a dependency in other cross-platform projects. You can build cross-platform mobile applications that share code between Android and iOS to implement networking, data storage and data validation, analytics, computations, and other application logic. Library App
  13. Use cases - Libraries We can use this approach to

    develop the common parts of our libraries or modules. In Android’s case, generate an aar/jar file you can include in your project. If it applies to iOS, too, there is no need to worry about managing frameworks because they will be made available through Swift Package Manager or Cocoapods.
  14. [versions] # Expense Tracker expense-tracker-kmp="1.1.2" [libraries] expense-tracker-kmp = {module =

    "app.umain:expense-tracker", version.ref="expense-tracker-kmp"} gradle/libs.versions.toml dependencies { /* Expense Tracker */ implementation(libs.expense.tracker.kmp) } app/build.gradle.kts
  15. Use cases - Application The shared Kotlin module contains the

    common logic that should be shared between the two target platforms. The androidApp module is a Kotlin module that builds into an Android application with Gradle as the build system. The iosApp module is instead an Xcode project building into an iOS application.
  16. pluginManagement { repositories { google() gradlePluginPortal() mavenCentral() } } dependencyResolutionManagement

    { repositories { google() mavenCentral() } } rootProject.name = "Expense Tracker" include(":androidApp") include(":shared") settings.gradle.kts
  17. Drawbacks Some platform-specific code may still be required for certain

    functionalities. They have to learn Kotlin to be able to use KMP. Platform-Specific Code Learning Curve iOS dev They have to learn iOS/Xcode to make sure the common code they produce works on iOS. Learning Curve Android dev
  18. Drawbacks Poor iOS developer experience - Xcode integration is done

    through unofficial plugins and build scripts. To develop, engineers need both Xcode and Android Studio open at the same time. - Apple does not show any willingness to support KMP officially. - Kotlin builds in Objective-C and doesn’t support Swift yet. Making it harder for the dev to debug. - Generics on Kotlin can not be inferred on iOS - Doesn’t support Codable, Default argument or Swift enums.
  19. Cost of migration Migrating an Android app to KMP is

    straightforward, (especially if the app is already using Kotlin) The good news is that your current codebase and KMP can cohabit. Migration can be done incrementally, one feature at a time using micro-libraries. It’s pretty easy to set up. Migrating an iOS app is more complex. The code in commonMain needs to be able to build for both iOS and Android, and it’s not true by default. • Migrate your Xcode build to the Gradle KMP build. • Move your iOS code to the KMP project. • Update all interfaces that use native inputs (like BigNum) with expect or the @optional annotation, to make sure the common code can compile for both Android and iOS. • Write custom iOS code in Kotlin for parts that can not be shared. • Rewrite some iOS code to use the KMP-generated library instead.
  20. Cost of migration By migrating to KMP: • The business

    logic will be shared and written in Kotlin. • The iOS team will have less code to write. • Better testing of the business logic shared in KMP • Client PR reviews can be faster and knowledge shared between teams.
  21. Awesome Kotlin Multiplatform This list contains libraries which support iOS

    and Android targets in first place. 🛠 Tooling 📋 Log 🌎 Network 📦 Storage 📱 Device 💉 Dependency Injection 🏗 Architecture 🔍 Analytics 🩺 Test 🔑 Crypto 📁 File 🚀 Language extensions 🗃 Serializer ⏰ Date-Time ➿ Asynchronous 🍎 Compose UI 🎨 Graphics 🧩 Service SDK 🧮 Arithmetic 🛢 Resources 🔧 Utils https://github.com/terrakok/kmp-awesome
  22. Revolver Revolver is a Kotlin Multiplatform State Management solution that

    enforces one single immutable state. Information is passed from clients to KMM by emitting Events to a ViewModel. This ViewModel will have read only State and Effect flows that the clients can subscribe to for updates. https://github.com/apegroup/revolver
  23. An Compose Multiplatform project will always will use Kotlin Multiplatform.

    But an Kotlin Multiplatform project not necessary make use of Compose Multiplatform. —Another Famous Person
  24. Compose Multiplatform Wizard Images reveal large amounts of data, so

    remember: use an image instead of a long text. Your audience will appreciate it https://terrakok.github.io/Compose-Multiplatform-Wizard/