Kotlin Multiplatform Development Documentation

repository·master·Indexed 41 days ago

https://github.com/jetbrains/kotlin-multiplatform-dev-docs

Official documentation for Kotlin Multiplatform (KMP) development, including guides for creating Compose Multiplatform projects across Android, iOS, Desktop, and Web. Covers project structure, source sets (commonMain, androidMain, iosMain, etc.), state management with recomposition, and platform-specific deployment.

Tokens
224.2K
Snippets
522
Records
864
Agent score
51%

What's inside Kotlin Multiplatform

  1. Overview of Compose Multiplatform desktop-only components

    master

    Compose Multiplatform provides specific components for building macOS, Linux, and Windows desktop applications that are not available in common/shared code. These components include:

    • Images and app icons: Managing assets for desktop environments.
    • Windows and dialogs: Managing top-level windows and system dialogs.
    • Context menus: Implementing right-click menus.
    • Tray and notifications: Creating system tray icons and desktop notifications.
    • Menu bar: Implementing application-level menu bars.
    • Scrollbars: Adding desktop-style scrollbars to scrollable areas.
    • Tooltips: Displaying hover-based information.
  2. What is Kotlin Multiplatform (KMP)

    master

    Kotlin Multiplatform (KMP) is an open-source technology from JetBrains that allows you to share code across multiple platforms, including Android, iOS, desktop, web, and server, while maintaining native performance and access to platform APIs.

    Key capabilities include:

    • Code Sharing: Share business logic, networking, or storage modules across platforms.
    • UI Sharing: Use Compose Multiplatform to share UI code across platforms for maximum reuse, or keep UIs native (using SwiftUI/UIKit on iOS) while sharing logic.
    • Native Performance: Leverages Kotlin/Native to produce native binaries, ensuring near-native performance even on platforms like iOS where a virtual machine is not used.
  3. Use cases for Kotlin Multiplatform by business type

    master

    Kotlin Multiplatform (KMP) and Compose Multiplatform can be applied differently depending on the organization's structure and goals:

    • Startups (Greenfield Projects): Ideal for maximizing efficiency and speed-to-market. Teams can use Compose Multiplatform to share both logic and UI, or mix shared UI with native components for rapid prototyping.
    • Small and Medium Businesses: Useful for sharing core logic while maintaining a native look-and-feel. KMP allows for a gradual introduction of cross-platform capabilities into existing apps.
    • Enterprises: Supports large, complex codebases through incremental adoption. It allows teams to reuse existing Kotlin skills and ensures consistent business logic across all devices without introducing entirely new tech stacks.
    • Agencies/Consultancies: Helps manage multiple client projects under tight timelines by reusing code and accelerating delivery across diverse platform requirements.
    • Market Expansion: Facilitates moving into new platforms (e.g., moving from iOS-only to Android) by leveraging existing code and maintaining native performance.
    • SDK Development: KMP is excellent for building mobile SDKs. Shared code compiles to platform-specific binaries (JVM for Android, native for iOS) and integrates like any regular platform-specific dependency, allowing consumers to use platform-specific APIs without limitations.
  4. Use platform-specific APIs in Kotlin Multiplatform

    master
    When developing Kotlin Multiplatform (KMP) applications or libraries, always check if a multiplatform library already exists for your needs (e.g., networking, logging, analytics) via platforms like klibs.io. If no library exists, you can access platform-specific APIs using the expect/actual mechanism or by using interfaces and dependency injection.
  5. Choose an iOS integration method for Kotlin Multiplatform

    master

    To integrate a Kotlin Multiplatform (KMP) shared module into an iOS app, you must generate an iOS framework from the shared module and add it as a dependency to the iOS project. There are two primary integration strategies:

    1. Local Integration

    Use local integration if you want full control over the entire codebase and want instant updates to your final applications whenever the common code changes.

    Options for Local Integration:

    • Direct integration: Connects the framework directly via an Xcode build phase script. Best if you do not use CocoaPods dependencies in your KMP project. This is the default if using the Kotlin Multiplatform IDE plugin.
    • SwiftPM integration with a local package: Best for monorepo setups using Swift Package Manager and where you don't have irreplaceable CocoaPods dependencies.
    • CocoaPods integration with a local podspec: Best for monorepo setups using CocoaPods or if your KMP project imports CocoaPods dependencies.

    2. Remote Integration

    Use remote integration if you want to explicitly separate the shared code codebase from the final application codebase. The shared code is treated like a regular third-party dependency.

    Options for Remote Integration:

    • SwiftPM with XCFrameworks: Export your KMP project as an XCFramework and distribute it as a remote Swift package.
    • CocoaPods integration with XCFrameworks: Build XCFrameworks using the Kotlin CocoaPods Gradle plugin and distribute them via CocoaPods.
  6. Understand the Kotlin Multiplatform development documentation repository structure

    master

    This repository serves as the source for the official Kotlin Multiplatform development documentation hosted at https://www.jetbrains.com/help/kotlin-multiplatform-dev. The content is organized as follows:

    • topics/: Contains the primary markdown files for documentation topics.
    • snippets/: Contains embedded code snippets used throughout the docs.
    • images/: Contains images and charts.
    • v.list: A file containing variables used within the documentation.
    • mpd.tree: Defines the navigation tree structure used on the website.
    • cfg/ and writerside.cfg: Configuration files for the documentation engine.
  7. Implement Drag-and-Drop in Compose Multiplatform

    master

    Compose Multiplatform provides experimental modifiers to enable drag-and-drop operations, allowing your app to accept data from other applications or allow users to drag data out of your app.

    Note: Both dragAndDropSource and dragAndDropTarget modifiers are experimental and require an opt-in annotation.

    To implement drag-and-drop, you use two primary modifiers:

    1. dragAndDropSource: Applied to the composable that acts as the origin of the data.
    2. dragAndDropTarget: Applied to the composable that acts as the destination for the data.
  8. What is Compose Hot Reload?

    master

    Compose Hot Reload allows you to visualize and experiment with UI changes by directly applying code changes to a running Compose Multiplatform application. Unlike standard Compose previews which view isolated components, Hot Reload updates the live application state.

    Requirements:

    • Kotlin: 2.1.20 or newer.
    • JVM Target: Compatible with Java 21 or earlier.
    • IDE Support: Recommended to use the Kotlin Multiplatform IDE plugin with IntelliJ IDEA (2025.2.2+) or Android Studio (Otter 2025.2.1+).

    Best Practice: Use the Desktop target as a sandbox to quickly experiment with UI changes in common code without interrupting your development flow.