KMP: Mobile Development with Shared Code, Improved Swift & UI Integration

Paul Hackenberger
Axel Springer Tech
Published in
6 min readMay 2, 2024

--

Kotlin Multiplatform Mobile (KMP) is a powerful technology that allows developers to share code between Android and iOS projects. This approach offers significant benefits in development speed, consistency, and maintainability. This article explores the core concepts of KMP and dives into SKIE, a tool that can further enhance the KMP development experience, specifically for iOS integration.

Why KMP?

Traditional native app development with separate codebases for Android and iOS can be cumbersome and time-consuming. KMP addresses this challenge by enabling developers to write code in Kotlin that can be shared across both platforms. This reduces code duplication and simplifies the development process. Here are some key benefits of using KMP:

  • Reduced Codebase: Share common logic across Android and iOS, leading to faster development and easier maintenance.
  • Improved Consistency: Ensure a consistent user experience across platforms by sharing core functionalities.
  • Faster Development Cycles: Focus on writing code once and reusing it, accelerating development timelines.

Other Options

Flutter, React Native, and Xamarin are popular frameworks for building cross-platform mobile applications, each with distinct characteristics.

Flutter, developed by Google, uses the Dart programming language and is renowned for its high performance and beautiful, natively compiled applications. It offers a rich set of widgets and a robust rendering engine, that uses a custom rendering engine to mirrow the UI-specific styles.

React Native, created by Facebook, leverages JavaScript and React to enable developers to build apps that feel native while sharing code across platforms. It emphasizes a vibrant community and extensive library ecosystem.

Xamarin, a Microsoft-supported framework, allows developers to use C# and .NET to create apps that can run on multiple platforms, promoting code reuse and integrating tightly with the Microsoft stack.

Challenges and Considerations with KMP for iOS

While KMP offers numerous advantages, there’s a specific challenge to consider for iOS integration:

  • Interoperability with Objective-C: Kotlin and Swift have distinct language features. Previously, shared KMP code was translated to Objective-C for iOS, which can be difficult for Swift developers to work with. Objective-C has a different syntax and paradigm compared to Swift, making it less familiar and potentially less efficient to maintain for iOS developers.

Overcoming the Challenge with SKIE

SKIE is a Kotlin compiler plugin designed to improve the interoperability between Kotlin and Swift in KMP projects. It tackles the challenge of working with Objective-C generated code by:

  • Generating Swift Wrappers: SKIE creates Swift wrappers for the Objective-C headers that are produced during the KMP compilation process. This makes the Kotlin code more readable and maintainability for Swift developers. Swift wrappers provide a layer of abstraction between the Kotlin code and the underlying Objective-C implementation. These wrappers are written in Swift, making the code easier to understand and use for iOS developers familiar with the language.
  • Preserving Language Features (Optional): In some cases, SKIE can also restore some language features that might be lost during the translation from Kotlin to Objective-C and then to Swift. This allows developers to leverage more Kotlin-specific functionalities within their Swift code. This functionality might not be available in all cases, but it can further improve the developer experience by allowing for a more natural use of Kotlin concepts within the Swift environment.
  • Enhanced Developer Experience: Overall, SKIE simplifies working with Kotlin code in Swift projects, leading to a more efficient development workflow for iOS integration. By providing Swift wrappers and potentially restoring some Kotlin features, SKIE bridges the gap between the two languages, making it easier for Swift developers to understand and maintain the shared Kotlin codebase.

Benefits of Using SKIE

Here’s a closer look at the advantages of using SKIE for KMM projects:

  • Improved Maintainability: Swift wrappers make the Kotlin code more understandable and maintainable for iOS developers, reducing the cognitive load of working with a different language.
  • Smoother Development Workflow: By simplifying the interaction between Kotlin and Swift code, SKIE contributes to a more efficient development process, especially for the iOS side of the project.
  • Reduced Context Switching: Developers can focus on writing clean Kotlin code without needing to worry about the intricacies of Objective-C.
  • Better Leverage of Kotlin Features: In some cases, SKIE can help preserve Kotlin-specific features within the Swift codebase, allowing developers to take full advantage of the Kotlin language.

Beyond SKIE: Additional Considerations for Successful KMM Adoption

While SKIE addresses a major hurdle in KMP development for iOS, there are other factors to consider for a successful KMP project:

  • Project Structure: Carefully plan the project structure to ensure clear separation between shared code, platform-specific code, and resources. This promotes better organization and maintainability.
  • Testing Strategy: Develop a comprehensive testing strategy that covers both the shared codebase and the platform-specific implementations. This ensures that the application functions correctly across all platforms.
  • Team Communication: Effective communication between Android and iOS developers is crucial for understanding shared code usage and potential platform-specific considerations.
  • Design System: Establish a consistent design system to ensure a unified user experience across platforms, even when the codebase is partially shared.

KMP and UIs for iOS and Android

The primary approach for UI development in KMP involves creating separate UIs for each platform (Android and iOS). This leverages the strengths of each platform’s native UI toolkits:

  • Android: Use Jetpack Compose or XML layouts with views to build the UI.expand_more
  • iOS: Utilize SwiftUI or UIKit to create the user interface.

Benefits:

  • Native Look and Feel: Each platform’s UI adheres to the native design guidelines, ensuring a familiar and optimized user experience.
  • Access to Platform-Specific Features: Developers can leverage platform-specific UI components and functionalities for a richer user experience.
  • Mature Toolsets: Both Android and iOS offer mature UI toolkits with extensive features and functionalities.

Shared UI Components (Experimental):

While the core UI development remains platform-specific, KMP can be used to share some basic UI components across platforms. Here’s an approach to consider:

  • Compose Multiplatform (Experimental): Utilize Compose Multiplatform, a project still under development, to share basic UI components like buttons or layouts. This approach allows for some code reuse in the UI layer.expand_more

Benefits:

  • Reduced Code Duplication: Share the code for simple UI components across platforms.
  • Consistent Look and Feel (For Simple Components): Maintain a consistent visual style for basic UI elements across platforms.

Important Considerations

  • Compose Multiplatform is still evolving, and the complexity of UI components that can be effectively shared might be limited.
  • The primary focus of KMP remains on sharing business logic, with UI development remaining largely platform-specific.

Design System Integration

A crucial aspect of maintaining a consistent user experience across platforms with separate UIs is a well-defined design system. Here’s how it helps:

  • Design Guidelines: Establish clear design guidelines that outline the visual style, components, and usage patterns for the application.exclamation
  • Style Guide and Assets: Create a style guide and provide platform-specific assets (icons, fonts) to ensure consistency across UIs.
  • Development Tools: Utilize tools that enforce design system rules within the development environment for both Android and iOS development.

Benefits:

  • Unified User Experience: Ensures a consistent look and feel across platforms despite separate UIs.
  • Reduced Design Drift: Mitigates the risk of design inconsistencies between platforms over time.
  • Improved Developer Efficiency: Provides clear guidelines for developers working on the UI for each platform.exclamation

By carefully considering these additional factors alongside the benefits of SKIE, development teams can leverage KMP to its full potential and achieve significant improvements in development efficiency and code maintainability for their cross-platform mobile applications.

Thanks to Bo Liu from my team for writing this article:

Thanks to Alek Zubala for your inspiration!

--

--