News

Why we want to implement Kotlin Multiplatform in our projects

Kotlin Multiplatform Development
 

Authors: Dr. Rolf Kluge, Christian Amrell (CA), Jakob Ulbrich

The development of Kotlin Multiplatform (KMP) began in 2017. With the release of the stable version in November 2023, the starting signal was given for us to roll out KMP in our projects. Since then KMP has gained a lot of traction and Android and iOS developers are of course excited to steadily adopt it.

Now that the Android and iOS departments are moving closer together, we at Appsfactory have worked on reshaping our ticket workflows and processes to work hand in hand, strengthening our skills in Kotlin and Swift, increasing our knowledge of Kotlin Multiplatform and pushing it to its limits.


KMP Explained: Sharing Logic, Maintaining Native Performance

Kotlin Multiplatform might seem like a true cross-platform solution, but it actually works a little differently. Instead of writing one codebase for all platforms, KMP allows developers to choose which parts of the code can be shared and which need to stay platform-specific.

Source: https://www.jetbrains.com/kotlin-multiplatform/

Since the ability to implement UI with Compose Multiplatform for both platforms is still in an early development stage and recently just left the Alpha state, we decided to go with the “Share logic and keep the UI native”-approach.

While the UI remains native, for Android with Jetpack Compose and for iOS with SwiftUI, KMP excels at sharing the application logic written in Kotlin. This logic resides in a shared Gradle module, essentially a folder containing reusable code.

Source: Appsfactory GmbH 2024

This shared code is further organized into source sets. Each source set focuses on a specific target platform (e.g., Android or iOS) and compilation type (main or test). This allows developers to include platform-specific functionalities within the shared code when needed, using APIs or dependencies available only on that platform.

Source: Appsfactory GmbH 2024

KMP leverages Kotlin’s ability to be translated into native code for each platform. This means there’s no additional runtime needed, keeping things efficient. Because a direct Swift export is not yet available, currently the code for iOS is still translated into Objective-C which has some limitations compared to Kotlin & Swift.

Talking of Limitations — What to look out for?

While Kotlin Multiplatform (KMP) offers many advantages, there are some limitations to consider when sharing code across platforms, mostly due to Objective-C limitations:

  1. Generic Types: Objective-C protocols don’t support Kotlin’s generic types, leading to loss of type information.
  2. Generic Types and Nullability: Nullability is not defined in Objective-C generics, making functions and properties nullable in Swift.
  3. Kotlin Value Classes: Value classes lose their type safety magic in Objective-C/Swift. They unbox to primitives and lose specific type details.
  4. Kotlin Sealed Types: Sealed types become regular class/interface hierarchies in Objective-C/Swift, losing their “sealed” nature.

These limitations can be addressed through workarounds like using classes instead of interfaces for generics and explicitly handling nullability in code. Additionally, libraries like SKIE (which provides Swift support for sealed types) and NativeCoroutines (which extends coroutines to native platforms) can help mitigate some of these issues.

Additionally, Swift export is planned for the Kotlin Roadmap this year, which will hopefully make most limitations obsolete.

The Power of KMP

KMP (Kotlin Multiplatform Mobile) offers a compelling solution for building high-performance, cross-platform mobile applications. Here’s why it is our top choice:

1. Superior Quality

  • Native Performance: No compromises in UI performance, because we can fully rely on the native frameworks Jetpack Compose and SwiftUI. Logic resides in shared KMP code, which is translated to native code.
  • Reduced Errors: Shared code means fewer bugs — you write the logic once and benefit from a single codebase.
  • Seamless Development: Leverage the familiar and robust developer tools you already know, including Android Studio and Xcode.

2. Enhanced Efficiency

  • Streamlined Development: Develop and maintain your app more efficiently, leading to significant cost savings.
  • Native Power: Access native platform APIs for optimal performance and functionality.
  • Leverage Expertise: Apply your existing development knowledge to different platforms.

3. Future-Proof Your App

  • Evolving Technology: KMP is actively supported by JetBrains and benefits from continuous development. Google’s involvement ensures ongoing innovation and a bright future and announced at I/O this year that they are officially supporting KMP for Android to share business logic.
  • Modern Approach: Embrace a modern development paradigm that streamlines mobile app creation.
  • Platform Flexibility: Develop for Android with a near-native experience, while maintaining the option to seamlessly switch to fully native iOS development if needed.

Kotlin Multiplatform — The Future of Mobile App Development at Appsfactory?

We at Appsfactory are actively embracing KMP and integrating it into our projects. The benefits are undeniable: reduced development time, improved code quality, and a more streamlined workflow for our developers. While some limitations exist, particularly when dealing with generics and Kotlin-specific features, the workarounds are manageable.

KMP has high potential to be a game-changer in mobile app development. By enabling us to share core application logic while maintaining native UI experiences on Android and iOS, KMP offers the best of both worlds: efficiency, performance, and a future-proof approach.

And there is even more to come, KMP’s active development ensures a bright future. A direct Swift export, instead of Objective C, is already on the roadmap for 2024. Which would reduce most of the current limitations regarding types, generics, etc. And then there is Compose Multiplatform, which is already in Beta and will enable developers to go even further and implement UI for both platforms in KMP.

Looking ahead, we are confident that KMP will play a pivotal role in our ability to deliver high-quality, cross-platform mobile apps for our clients.


About Appsfactory

Appsfactory Group is a leading digital agency for transformation, design, consulting, and applications. With over 800 completed projects, they serve international clients and partners across various industries. Their 14+ years of full-service expertise covers the entire digital product lifecycle.

 

Image ©Adobe Stock

Araki Illustrations #402390004

Press contact

More articles

Appsfactory Officially Announces New Office in Berlin

Read more

Appsfactory Ranked As #14 Employer in the German IT Sector

Read more
nach oben