Kotlin Multiplatform project structure production: best practices

Learn how to structure a production-ready Kotlin Multiplatform project using KMPShip as a concrete example, including best practices and module architecture.

Posted by

Kotlin Multiplatform project structure production: best practices
TL;DR:
  • Structuring a Kotlin Multiplatform project requires careful organization of modules and layers.
  • Clean architecture ensures maintainability and scalability for features.
  • KMPShip serves as a practical example of these best practices in action.

How should you structure a Kotlin Multiplatform project for production?

A production-ready Kotlin Multiplatform (KMP) project should be structured in a way that promotes maintainability, scalability, and clarity. The organization of modules, the dependency graph, and the separation of shared and platform-specific code are crucial for achieving these goals. In this article, we will explore how to effectively structure a KMP project, using KMPShip as a concrete example.

What is the ideal KMP module structure?

The ideal KMP module structure divides the project into layers, each serving distinct purposes. This modular approach enhances code reusability and separation of concerns. In KMPShip, the module structure typically includes core modules such as shared, android, and ios, along with feature-specific modules like auth, payments, and networking.

Example of directory structure

KMPShip/
├── shared/
│   ├── auth/
│   ├── payments/
│   ├── networking/
│   ├── database/
│   └── notifications/
├── android/
│   └── app/
└── ios/
    └── app/
Each feature is encapsulated within its own module, allowing for clear boundaries and easier management.

How can you visualize the dependency graph in a KMP project?

Visualizing the dependency graph in a KMP project helps understand how different modules interact with one another. In KMPShip, the shared module serves as the core layer, with all feature modules depending on it. Platform-specific modules, such as android and ios, depend on the shared code for common functionality while implementing platform-specific features.

Dependency graph description

  • shared module contains common business logic and data models.
  • Feature modules like auth, payments, and networking depend on shared.
  • android and ios modules depend on the shared and respective feature modules.

How do you configure builds in a Kotlin Multiplatform project?

Build configuration in a Kotlin Multiplatform project is managed through Gradle. A well-structured build.gradle.kts file is essential for defining dependencies, plugins, and build tasks. In KMPShip, we ensure that the shared and platform-specific modules are configured correctly to compile with their necessary dependencies. Here is an example snippet:
kotlin
kotlin { jvm() // Android target ios() // iOS target sourceSets { val sharedMain by getting { dependencies { implementation(

Build your KMP app faster

Skip the setup and start shipping with a production-ready Kotlin Multiplatform starter kit.