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

Creating SDKs for Multiple Platforms with KMP | Devfest Chennai

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. Creating SDKs with Kotlin Multiplatform
    Rivu Chakraborty
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    https://courses.rivu.dev/

    View full-size slide

  2. ● 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
    @[email protected]
    Who am I? 󰞦

    View full-size slide

  3. What’s an SDK?

    View full-size slide

  4. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    What’s an
    SDK?
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Software Development
    Kit.
    Library/SDK

    View full-size slide

  5. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    What’s an
    SDK?
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    A code block, that
    helps your or client
    apps reduce
    redundancy, by using
    this library / SDK

    View full-size slide

  6. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Most Common
    SDK Use Cases
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    ● Analytics
    ● Chat / Conversation
    ● Ads
    ● Image Processing
    ● Audio-Video
    Processing

    View full-size slide

  7. Rules for Developing
    SDK

    View full-size slide

  8. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    SDK Rules
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    0. Should be Plug and Play

    View full-size slide

  9. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    SDK Rules
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    0. Should be Plug and Play
    1. Should be easy to remove

    View full-size slide

  10. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    SDK Rules
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    0. Should be Plug and Play
    1. Should be easy to remove
    2. Should not add
    unnecessary extra
    dependencies to the
    client app

    View full-size slide

  11. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    SDK Rules
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    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

    View full-size slide

  12. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    SDK Rules
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    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

    View full-size slide

  13. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    SDK Rules
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    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

    View full-size slide

  14. Building an SDK

    View full-size slide

  15. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Building an
    SDK
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    ● 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

    View full-size slide

  16. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Building an
    SDK
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    ● 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

    View full-size slide

  17. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Building an
    SDK
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    ● 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

    View full-size slide

  18. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Building an
    SDK
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    ● 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

    View full-size slide

  19. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Building an
    SDK
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    ● 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

    View full-size slide

  20. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Building an
    SDK
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    ● 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

    View full-size slide

  21. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Building an
    SDK
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    ● 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

    View full-size slide

  22. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]

    View full-size slide

  23. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Solution?
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Share Code across
    all Platforms

    View full-size slide

  24. Current Solutions
    for Sharing Code across Platforms

    View full-size slide

  25. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Flutter
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    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

    View full-size slide

  26. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    React Native
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    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

    View full-size slide

  27. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Kotlin
    Multiplatform
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    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

    View full-size slide

  28. Why Kotlin
    Multiplatform
    [KMP/KMM]

    View full-size slide

  29. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    ● 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.

    View full-size slide

  30. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Kotlin
    Multiplatform

    View full-size slide

  31. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Kotlin
    Multiplatform
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Using Kotlin Code in
    projects to target more
    than one platform

    View full-size slide

  32. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Kotlin
    Multiplatform
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    ● 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

    View full-size slide

  33. Understanding Kotlin
    Multiplatform
    [KMP/KMM]

    View full-size slide

  34. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    The Structure
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]

    View full-size slide

  35. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    The Structure
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    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

    View full-size slide

  36. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    The Structure
    expect fun doSomething()
    actual fun doSomething() {
    //Platform specific logic here
    }

    View full-size slide

  37. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    The Structure
    Directory Structure

    View full-size slide

  38. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    The Structure
    expect fun getRandomUUID():
    String
    commonMain

    View full-size slide

  39. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    The Structure
    androidMain
    actual fun getRandomUUID(): String
    = UUID.randomUUID().toString()

    View full-size slide

  40. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    The Structure
    iosMain
    actual fun getRandomUUID(): String
    = NSUUID().UUIDString()

    View full-size slide

  41. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    The Structure
    jsMain
    import uuid
    actual fun getRandomUUID(): String
    = uuid.v4().toString()

    View full-size slide

  42. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    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)
    }
    }

    View full-size slide

  43. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    The Structure
    Dependency
    Management
    val jsMain by getting {
    dependencies {
    implementation(npm("uuid", "9.0.0"))
    implementation(libs.wire.runtime.js)
    }
    }

    View full-size slide

  44. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    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)
    }
    }

    View full-size slide

  45. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    The Structure
    Dependency
    Management
    val iosMain by getting {
    dependencies {
    implementation(libs.ktor.client.darwin)
    implementation(libs.sqlDelight.native)
    }
    }

    View full-size slide

  46. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    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 ...
    }
    }

    View full-size slide

  47. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    The Structure
    Exported SDK
    files Android .aar
    iOS .xcframework
    JS Minified js / NPM

    View full-size slide

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

    View full-size slide

  49. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    The Structure
    Setting up
    Distribution
    kmmbridge {
    spm()
    mavenPublishArtifacts()
    versionPrefix.set(SDKConfig.version)
    frameworkName.set(SDKConfig.sdkName)
    }
    https://kmmbridge.touchlab.co

    View full-size slide

  50. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    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,
    }

    View full-size slide

  51. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    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
    }

    View full-size slide

  52. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Versioning
    object LibraryConfig {
    val version = "1.0.0"
    const val groupId =
    "dev.rivu.kmp.sdkDemo"
    const val artifactIdSdk = "core"
    val sdkName = "demo-sdk"
    }

    View full-size slide

  53. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Reducing Size
    in JS
    js {
    browser {
    dceTask {
    keep("shared.dev.rivu.MyClass")
    }
    binaries.executable()
    }
    }

    View full-size slide

  54. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Using the
    bundle in JS

    View full-size slide

  55. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    Using the
    XCFramework
    in iOS

    View full-size slide

  56. IDEs and Tools

    View full-size slide

  57. 🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    IDEs and
    Tools

    View full-size slide

  58. Resources
    ● https://speakerdeck.com/aldefy/a-hitchhikers-guide-to-kotlin-multiplatfor
    m
    ● https://github.com/joreilly/PeopleInSpace
    ● https://github.com/joreilly/Confetti
    ● https://kotlinlang.org/docs/multiplatform.html
    ● https://courses.rivu.dev

    View full-size slide

  59. Thank You
    Rivu Chakraborty
    🌐https://www.rivu.dev/
    youtube.com/@rivutalks
    @rivuchakraborty
    @[email protected]
    https://courses.rivu.dev/

    View full-size slide