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

Creating SDKs for Multiple Platforms with KMP |...

Creating SDKs for Multiple Platforms with KMP | Devfest Chennai

Kotlin Multiplatform or KMP is the talk of the town nowadays. Everyone is talking about it, lots of big orgs are using it as well, including Thoughtworks, Shopify, JioCinema, Cred etc.
While making the whole app with KMP (Kotlin Multiplatform) sounds fantastic, another really good use case would be to migrate your SDKs and libraries to KMP.
Whether you're a B2B company that offers SDKs and libraries to external clients or creating and using SDKs internally within your company, making it in KMP can help ensure consistency among different platforms, less time and effort spent, and in the end lower cost of maintenance.
In this talk, we'll discuss why and how you should create SDKs and migrate existing SDKs to KMP, and some best practices regarding the same, from a person who has done it in production, affecting millions of users.
In this session, we'll learn the following
- Why Kotlin Multiplatform for SDKs
- Difference between multiplatform and cross-platform
- How to support different platforms (Android, iOS, JS, Flutter etc.) when creating SDKs
- Performance optimization and handling threading for different platforms
- Best practices

Learn why and how to migrate or create new SDKs / libraries with Kotlin Multiplatform.
In this session, we'll learn the following
- Why Kotlin Multiplatform for SDKs
- Difference between multiplatform and cross-platform
- How to support different platforms (Android, iOS, JS, Flutter etc.) when creating SDKs
- Performance optimization and handling threading for different platforms
- Best practices

Rivu Chakraborty

December 01, 2023
Tweet

More Decks by Rivu Chakraborty

Other Decks in Technology

Transcript

  1. • India’s first GDE (Google Developer Expert) for • More

    than 11 years in the Industry • Mobile Architect @ JioCinema • Previously ◦ Byju’s ◦ Paytm ◦ Gojek ◦ Meesho • Author (wrote multiple Kotlin books) • Speaker • Mentor • Community Person • YouTuber (?) 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Who am I? 󰞦
  2. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social What’s an SDK? 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty

    @rivu@androiddev.social A code block, that helps your or client apps reduce redundancy, by using this library / SDK
  3. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Most Common SDK Use Cases 🌐https://www.rivu.dev/

    youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social • Analytics • Chat / Conversation • Ads • Image Processing • Audio-Video Processing
  4. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social SDK Rules 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social

    0. Should be Plug and Play 1. Should be easy to remove 2. Should not add unnecessary extra dependencies to the client app 3. Interaction should be straightforward for the client
  5. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social SDK Rules 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social

    0. Should be Plug and Play 1. Should be easy to remove 2. Should not add unnecessary extra dependencies to the client app 3. Interaction should be straightforward for the client 4. Data Types should be interoperable
  6. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social SDK Rules 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social

    0. Should be Plug and Play 1. Should be easy to remove 2. Should not add unnecessary extra dependencies to the client app 3. Interaction should be straightforward for the client 4. Data Types should be interoperable 5. [Optional] Should allow client apps to implement own UI
  7. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Building an SDK 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty

    @rivu@androiddev.social • Hire / Allocate Developers • Define Requirements • Plan Roadmap • Develop, maintain and test SDK integration • Cross Check Parity / Consistency across platforms • Setup SDK release • Maintain versioning across platforms
  8. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Building an SDK 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty

    @rivu@androiddev.social • Hire / Allocate Developers • Define Requirements • Plan Roadmap • Develop, maintain and test SDK integration • Cross Check Parity / Consistency across platforms • Setup SDK release • Maintain versioning across platforms Android 1x
  9. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Building an SDK 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty

    @rivu@androiddev.social • Hire / Allocate Developers • Define Requirements • Plan Roadmap • Develop, maintain and test SDK integration • Cross Check Parity / Consistency across platforms • Setup SDK release • Maintain versioning across platforms Android, iOS 2x
  10. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Building an SDK 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty

    @rivu@androiddev.social • Hire / Allocate Developers • Define Requirements • Plan Roadmap • Develop, maintain and test SDK integration • Cross Check Parity / Consistency across platforms • Setup SDK release • Maintain versioning across platforms Android, iOS, Web 3x
  11. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Building an SDK 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty

    @rivu@androiddev.social • Hire / Allocate Developers • Define Requirements • Plan Roadmap • Develop, maintain and test SDK integration • Cross Check Parity / Consistency across platforms • Setup SDK release • Maintain versioning across platforms Android, iOS, Web, tvOS ~4x
  12. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Building an SDK 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty

    @rivu@androiddev.social • Hire / Allocate Developers • Define Requirements • Plan Roadmap • Develop, maintain and test SDK integration • Cross Check Parity / Consistency across platforms • Setup SDK release • Maintain versioning across platforms Android, iOS, Web, tvOS, Tizen & WebOS [HTML5 TV] ~5x
  13. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Building an SDK 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty

    @rivu@androiddev.social • Hire / Allocate Developers • Define Requirements • Plan Roadmap • Develop, maintain and test SDK integration • Cross Check Parity / Consistency across platforms • Setup SDK release • Maintain versioning across platforms Android, iOS, Web, tvOS, Tizen & WebOS [HTML5 TV], WatchOS, WearOS ~7x
  14. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Flutter 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social 0.

    Should be Plug and Play 1. Should be easy to remove 2. Should not add unnecessary extra dependencies to the client app 3. Interaction should be straightforward for the client 4. Data Types should be interoperable 5. [Optional] Should allow client apps to implement own UI
  15. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social React Native 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social

    0. Should be Plug and Play 1. Should be easy to remove 2. Should not add unnecessary extra dependencies to the client app 3. Interaction should be straightforward for the client 4. Data Types should be interoperable 5. [Optional] Should allow client apps to implement own UI
  16. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Kotlin Multiplatform 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social

    0. Should be Plug and Play 1. Should be easy to remove 2. Should not add unnecessary extra dependencies to the client app 3. Interaction should be straightforward for the client 4. Data Types should be interoperable 5. [Optional] Should allow client apps to implement own UI ✓
  17. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social • Developed

    by Jetbrains • Open Source • Supported by Google for Android Development • Supported by Spring for Server • Has own framework (Ktor) for asynchronous client and server applications • Has different targets for Different platforms. e.g. Kotlin/JVM, Kotlin/Native, Kotlin/JS, Kotlin/Wasm etc.
  18. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Kotlin Multiplatform 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social

    • Idiomatic Kotlin Language for all platforms • Low Risk ◦ You decide what to share, what not ◦ Option to use existing 3rd party libraries for each platform • Interoperability • Consistency across platforms • Export platform native SDK builds • Strong Community Support
  19. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social The Structure 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social

    Common JVM JS Native Android Server Apple Linux mingw iOS tvOS watchOS macOS linux_x64 linux_arm64 mingx64 iosX64 iosArm64 iosSimulatorArm64 tvosX64 tvosArm64 tvosSimulatorArm64 macosX64 macosArm64 watchosX64 watchosArm32 watchosArm64 watchosSimulatorArm64 watchosDeviceArm64
  20. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social The Structure Dependency Management val commonMain

    by getting { dependencies { implementation(libs.bundles.common.ktor) implementation(libs.coroutines.core) implementation(libs.kotlinx.datetime) implementation(libs.koin.core) implementation(libs.skie) } }
  21. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social The Structure Dependency Management val jsMain

    by getting { dependencies { implementation(npm("uuid", "9.0.0")) implementation(libs.wire.runtime.js) } }
  22. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social The Structure Dependency Management val androidMain

    by getting { dependencies { implementation(libs.ktor.client.okHttp) implementation(libs.okhttp.loggingInterce ptor) implementation(libs.koin.android) implementation(libs.sqlDelight.android) } }
  23. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social The Structure Dependency Management val iosMain

    by getting { dependencies { implementation(libs.ktor.client.darwin) implementation(libs.sqlDelight.native) } }
  24. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social The Structure Shared Common Code class

    EventsRepository( private val eventsRemoteDS: EventsRemoteDS, private val eventsLocalDS: EventsLocalDS, ) { suspend fun trackEvent( eventData: EventData, eventName: String, ) { //Logic to track Events ... } }
  25. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social The Structure Setting up Distribution val

    xcFramework = XCFramework("IndiaConferences") listOf( iosX64(), iosArm64(), iosSimulatorArm64(), ).forEach { it.binaries.framework { xcFramework.add(this) baseName = SDKConfig.sdkName } }
  26. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social The Structure Setting up Distribution kmmbridge

    { spm() mavenPublishArtifacts() versionPrefix.set(SDKConfig.version) frameworkName.set(SDKConfig.sdkName) } https://kmmbridge.touchlab.co
  27. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social The Structure Taking care of Interoperability

    https://skie.touchlab.co/ data class User @DefaultArgumentInterop.Enabled constructor( val id: String, val firstName: String, val lastName: String, val userType: UserType = UserType.User ) @EnumInterop.Enabled enum class UserType { User, Admin, }
  28. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social The Structure Taking care of Interoperability

    https://skie.touchlab.co/ data class User @DefaultArgumentInterop.Enabled constructor( val id: String, val firstName: String, val lastName: String, val userType: UserType = UserType.User ) @EnumInterop.Enabled enum class UserType { User, Admin, } @JsExport object UserTypeJS { val User = UserType.User val Admin = UserType.Admin }
  29. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Versioning object LibraryConfig { val version

    = "1.0.0" const val groupId = "dev.rivu.kmp.sdkDemo" const val artifactIdSdk = "core" val sdkName = "demo-sdk" }
  30. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Reducing Size in JS js {

    browser { dceTask { keep("shared.dev.rivu.MyClass") } binaries.executable() } }