Introduction to Kotlin Multiplatform Development

Introduction to Kotlin Multiplatform Development

Kotlin Multiplatform is indeed a new feature in the Kotlin programming language where developers can author applications targeting multiple platforms like Android, iOS, web, desktop,

and so on, all from a common codebase. Kotlin Multiplatform allows developers to write code shared across different platforms and lower the duplication of code, making them more productive. This enables the developer to spend more time on developing core functionality instead of bothering with platform specifics. In this article, I will briefly outline the basic principles of Kotlin Multiplatform, its architecture, and how this is simplifying cross-platform development.

What is Kotlin Multiplatform?

Kotlin Multiplatform is a Kotlin extension that enables shared code between different platforms: mobile (Android and iOS), desktop applications, as well as web applications. It gives developers a capability to reuse and share common business logic on different platforms while using any necessary platform-specific code.

Kotlin Multiplatform does not replace native code but instead encourages the reuse of core logic while keeping the openness of flexibility for platform-specific implementations.

Key Advantages of Kotlin Multiplatform

1. Code Reusability:

Sharability of codes The primary key advantage is reusability. You can share common code-the business logic, data models, algorithms, and so on-across platforms, and significantly lessen the effort of maintaining separate codebases for Android, iOS, and so on.

2. Flexibility:

With the possibility to share code between platforms, Kotlin Multiplatform still allows you to write platform-specific code if the features required are native. This means that you’ll be able to see native APIs when that is required for something.

3. Eliminating Duplication:

By sharing significant portions of the codebase, Kotlin multi-platform minimizes duplication and reduces the cost of maintenance overall while also accelerating development.

4. Integration into Existing Code:

Kotlin Multiplatform can easily be integrated with an existing Android and iOS project, which will make the transition to KMP gentle and non-disruptive.

How Kotlin Multiplatform Works

Kotlin Multiplatform leverages a shared module and platform-specific modules that retain platform-specific capabilities, which are separate from each other.

1. Common Module

Common Module ALL of the shared code resides in this module that encompasses business logic, data models, utilities, or anything that is not tied to the system being built for. This module itself relies on absolutely no target platform and commonly applies to business logic that is generally applicable.

For example, in the common module, you might define:

  1. Shared utility functions
  2. Data models
  3. Repository classes
// Common code example
class Greeting {
    fun greet(): String {
        return "Hello from shared code!"
    }
}

2. Platform-Specific Modules

What do you know? In addition to the common module, Kotlin Multiplatform allows you to create specific modules for each platform: Android, iOS, and so on. These modules live where platform-specific code goes: integration with APIs, UI handling, or even using the libraries native to a certain platform.

For example, in an iOS module, you’ll probably include the code for the interaction with UIKit, and in an Android module, you’ll use Android’s Jetpack libraries.

Kotlin Multiplatform makes use of the expect/actual keywords for the implementation of the specific module of a platform. You declare the function or class in the common module by the expect keyword and, using the actual keyword, specify the actual implementation in the platform-specific modules.

Example:

// Common module
expect fun getPlatformName(): String

// Android module
actual fun getPlatformName(): String {
    return "Android"
}

// iOS module
actual fun getPlatformName(): String {
    return "iOS"
}

In this sample, the getPlatformName() method is defined in common module, where has different implementations for Android and iOS.

 3. Gradle and Project Setup

 Kotlin Multiplatform is based on Gradle to configure project settings. A typical Kotlin Multiplatform project consists of modules for shared code between the platforms and the platform specific. The contents of the build.gradle.kts file define how the common and platform-specific modules should be organized:

A typical simple structure of how you might set up your Kotlin Multiplatform project:

kotlin {
    android()
    ios()

    sourceSets {
        val commonMain by getting
        val androidMain by getting
        val iosMain by getting
    }
}

In the above example, android() and ios() are used to determine the targets of the platforms, and sourceSets is used to configure common source set commonMain and platform-specific source sets androidMain and iosMain.

Kotlin Multiplatform for Mobile (KMM)

Kotlin Multiplatform for Mobile is a specialisation of the Kotlin Multiplatform, but this time focused on mobile apps for Android and iOS. With KMM you can share core logic between an Android and an iOS app, each continuing to use native UIs. This will allow you to maintain the native experience of the user but reuse most, if not all, of your codebase.

This is an ideal solution for teams looking to unite efforts in their Android, as well as iOS development, without sacrificing the performance and user experience native to each of them.

Usages of KMM

1. Ordinary Use Cases of KMM

Sharing the Business Rules Logic. You can share the code that pertains to all the business rules, validation, as well as manipulation of data.

2. Networking Request

All the networking logic can contain an API request or even the data access management. In this respect, you can have some shared code between Android and iOS platforms for the following reasons: you have uniform ways to save and retrieve data in both platforms.

4. Database access.

Kotlin Multiplatform does divide the concerns between UI and isolates shared code from platform-specific UI frameworks in one shot, hence it does take the best of both worlds- shared logic and native UI experiences.

By separating UI concerns and keeping shared code isolated from platform-specific UI frameworks, Kotlin Multiplatform provides the best of both worlds: shared logic and native UI experiences.

Tools and Libraries for Kotlin Multiplatform

There are many useful libraries and tools that help to build more effective cross-platform applications with Kotlin Multiplatform:

  1. Ktor: a Kotlin framework which lets one develop asynchronous servers and clients. You can share indeed Ktor networking logic in projects of Kotlin Multiplatform.
  2. SQLDelight: a library to interact with the SQL databases, multiplatform support in Kotlin.
  3. Kotlinx Serialization: a Kotlin library for serialization and deserialization that you can use in the shared code.
  4. Kotlinx Coroutines: Support for asynchronous programming cross-platforms, thus allowing code shared coroutine in your Kotlin Multiplatform projects.

Kotlin Multiplatform VS Other Cross-Platform Solutions

Kotlin Multiplatform is different from other cross-platform frameworks like Flutter and React Native. While Flutter is designing the whole application with only one codebase – even the UI, Kotlin Multiplatform is concentrated on sharing business logic and core functionality while giving you a possibility of maintaining your separate UIs for each platform.

Major Differences:

1. UI Flexibility

Kotlin Multiplatform gives you the possibility of writing native UIs both for Android and iOS that provide more flexibility in crafting platform-specific user experiences.

2. JavaScript and Web Support:

Unlike other frameworks that are exclusively targeting mostly mobile, Kotlin Multiplatform supports both web and desktop development also, thereby more or less making it another multi-purpose alternative for the development of the application across different platforms.

Disadvantages of Kotlin Multiplatform

One of the most powerful tools is Kotlin Multiplatform, allowing one to share code across multiple platforms, including Android, iOS, and web. While it offers various advantages during cross-platform development, it has a few disadvantages that developers should consider while adopting this approach.

1. Immature Ecosystem

Although Kotlin Multiplatform has some substantial steps forward in terms of the ecosystem maturity, it is still pretty young compared to a more better-established solution.

  • Limited Third-Party Libraries: Not all third-party libraries are available on all platforms. Depending on the third-party libraries in use, developers may sometimes face situations where a library available on Android is not available or partially supported on iOS or web.
  • Incoherent Tooling Support: Tooling around Kotlin Multiplatform especially related to iOS development is immature. Debugging, profiling, and build tools for non-Android platforms might not be as mature or highly integrated into the workflow as used to.

2. More Complexity for the Code in Platform-Specific

At the same time, as much as Kotlin Multiplatform is pushing the envelope in trying to make shared code possible, complexity is sometimes introduced by where it does require platform-specific code.

  • Need for Platform-Specific Implementations: Specific capabilities rely on platform-specific functionality, such as UI, interaction with hardware, which still needs to be coded in a particular platform. In this case, there is native Android development know-how – Java/Kotlin and native iOS development know-how – Swift/Objective-C.
  • Maintenance Overhead: Even if shared business logic is held in common, platform-specific code has to be maintained. This could shift the burden of maintenance, in that changes to platform-specific APIs have to be dealt with separately on each platform.

3. Limited UI Code Sharing

Kotlin Multiplatform focuses on shared business logic but the UI code remains generally different for each platform.

  • Next, separate UI development: The developer must then write separate UI code for Android in XML or Jetpack Compose and for iOS in SwiftUI or UIKit. This is because Kotlin Multiplatform does not support a unified UI framework at this time. That can be some limitation on the amount of time savings achieved through code sharing.
  • Splintered UI Codebases: Since the UI is developed independently for each platform, ensuring consistency between platforms can become difficult. UI behavior and look-and-feel may have to be synchronized between Android and iOS additionally.

4. Inadequate Official iOS Tooling Support

While Kotlin Multiplatform does support iOS, the tooling and the development experience might not be as smooth for an iOS developer.

  • Integration with Xcode: Xcode is still the top IDE for iOS development. However, in its integration with Kotlin Multiplatform, it has still not matched the degree of fluidity and ease for developers of Android using Android Studio. iOS developers may even find it tougher to navigate Kotlin codebases and set up their development environment.
  • Swift Interoperability Problems: Although Kotlin Multiplatform allows Kotlin code to be shared in iOS projects, interoperability with Swift and Objective-C makes it more cumbersome. More boiler-plate code for mapping the data structures between Kotlin and Swift will increase the level of code not shared.

5. Learning Curve

Moving to Kotlin Multiplatform means developers must learn about Kotlin as well as native development on each platform.

  • Mastery on Multiple Platforms: The developers must be proficient in Kotlin as well as native development for both Android and iOS. Mastery over platform-specific concepts, APIs, and tools pose a challenge to developers who focus only on one platform.
  • Cross-Platform Domain Knowledge: Beyond learning Kotlin, a developer should understand how to develop cross-platform and efficiently manage shared and platform-specific code. This adds to the overhead and is more significant for teams who are new to cross-platform approaches.

6. Performance Overhead

Even though Kotlin Multiplatform is designed to deliver near-native performance, there are sure to be scenarios where performance issues creep in.

  • Interop Performance Cost: Shared Kotlin code of communicating with native platform-specific code would likely incur a little performance overhead, mainly when type conversions are applied or calls are made very frequently between inter-platform interoperabilities.
  • Larger Binary Size: Including Kotlin libraries into both Android and iOS builds can add size to the binaries. Considering that the size of an application is strictly regulated on iOS this can become an issue in particular for smaller applications.

7. Complex build configuration

Kotlin Multiplatform implies additional build configuration for projects to be built and run on different platforms.

  • Complex Gradle setup: Going multi-platform introduces complexity also in the Gradle setup-itself, since you need to define different build targets for Android, iOS, and further platforms. In larger projects, it becomes more complicated to hold the build under your control.
  • Managing dependencies: Adding and managing dependencies in a multi-platform project is hard-nose labor, especially when libraries are just available on one platform or their cross-platform versions differ in functionality.

8. Debugging Complexity

Debugging also poses a few additional problems in cross-platform projects.

  • Debugging Tools per Platform: While debugging Kotlin on Android is now pretty robust in Android Studio, debugging shared code running on iOS may require alternation between two different development environments-for example, one uses Xcode for the iOS-specific issues-resulting in a very disjointed debugging process.
  • Complex Error Tracing: In cases involving errors for both platform-specific and shared code, tracing and debugging might be slightly tougher because this needs a far better insight in the Kotlin Multiplatform runtime and the behavior in place for the native platform.

Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading