-
Woohoo! Awesome #KotlinMultiplatform presentation from @YeaLikeFriend for @WWCodeMobile The stream was recorded, but if you can't wait here are my notes 🧵
-
* Kotlin can compile to multiple targets: JVM, iOS, Desktop, WebAssembly, and more * Share code to reduce redundancy * Platform specific code can interact with the platform
-
Most other cross-platform frameworks share _all_ the code * Missing flexibility * High initial investment and long-term commitment * Dependent on the framework to update for platform changes
-
KMP: share only what you want * Very flexible * Easily adopted into existing apps * Can use newest Android and iOS technologies because you keep the native apps
-
How KMM works * Common Kotlin module generates an Android AAR file and iOS Framework * Android app consumes AAR via Gradle as usual * iOS app consumes framework with SPM or Cocoapods as usual * Can include libraries made for KMM or interface with platform-specific libraries
-
Acronyms: KMP vs KMM * KMP is all the targets * KMM is the mobile targets KMM going beta in the Fall! [Acronyms like this are often confusing. Here's more details touchlab.co/clarifying-kotlin-multiplatform-naming/]
-
Shared code * A lot of code is not platform specific and can be “common” that essentially gives you multiplatform code for free * Android and iOS code has access to the common code and access to platform specific functionality
-
A lot of KMM samples are in the style of "Hello, World!" which is helpful. Sometimes you want more. KaMP Kit is a fully functioning app with networking requests, persistence, and other best practices and preferred libraries github.com/touchlab/KaMPKit
-
Writing Common Code * Anything that doesn’t use platform-specific APIs * Anything that uses a KMP library * Usually business logic, data models, network and databases
-
Sharing Platform Specific Code * Expect/Actual - it works and is great, but can be overused * What’s the screwdriver if you don’t want to be hammering screws into your app? Interfaces!
-
Interfaces let you pass implementations to common module whether a production implementation or test implementation You can even pass in Swift implementations from iOS!
-
Useful KMP Libraries * Ktor by JetBrains for shared networking * SqlDelight by Square for shared database * Kermit by touchlab for shared logging and crash reporting * Koin for shared dependency injection
-
Make your own by leaning into what you already know about interfaces * “How do I do X with KMP” may not return suitable results * “How do I do X on Android” and “How do I do X on iOS” will return results you can use to get started on your own KMM library
-
Multiplatform Settings by Russell Wolf for shared key-value storage is a great example for getting started with building your own libraries github.com/russhwolf/multiplatform-settings
-
Traversing the Kotlin/Swift Barrier * For example: Suspend functions and Flow aren’t easily consumable from iOS * Use callback interface or write a wrapper around the interface * Android consumes as usual * iOS can consume in a more idiomatic way