What is KMP

What is KMP

Discover Kotlin Multiplatform's powerful structure that supports shared codebases.

Codefy Labs's photo
·

2 min read

A multiplatform Kotlin project is relatively simple to understand, see the structure below:

commonMain:

  • Core Module: commonMain is the central hub where you write code that works across all platforms.

  • Universal Code: It includes business logic, data model definitions, and utility functions, all written in Kotlin.

  • Platform-Neutral: The code is designed to be independent of any specific platform, avoiding platform-specific logic or API calls.

  • Expect/Actual Mechanism: Utilizes Kotlin's "expect/actual" system to outline platform-specific implementations that are fulfilled in other parts of the project.

androidMain:

  • Platform-Specific Module: androidMain houses the code that’s tailored just for Android.

  • Actual Implementations: This module contains the "actual" implementations for the "expect" statements from the commonMain module.

  • Android APIs: It includes code utilizing specific Android APIs, such as activities, services, and intents.

  • Kotlin and Android SDK: The code is written in Kotlin and leverages the full suite of Android SDK libraries and tools.

iosMain:

  • iOS-Specific Module: iosMain is designed specifically for iOS platform code.

  • Actual Implementations: It provides the "actual" implementations for the "expect" statements defined in the commonMain module.

  • Kotlin to Native Code: The code is written in Kotlin and compiled to native code, compatible with iOS applications.

  • Integration with iOS APIs: This module allows interaction with iOS APIs and can integrate seamlessly with Swift or Objective-C if needed.

  • 85% of the codebase is shared across all platforms, focusing on business logic.

  • Each platform (Android, iOS, and Web) requires only 15% platform-specific code, mainly for UI.

  • Total code effort is reduced to 130% (3 platforms x 15% UI code + 85% shared business logic).

  • This approach reduces the duplication of effort, minimizes bugs, and simplifies maintenance.

In summary, KMP promotes the efficiency and reduced complexity of using Kotlin Multiplatform, highlighting significant code sharing and reduced redundancy compared to traditional platform-specific development.