codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

Β© 2026 codingstuff.io. All rights reserved.

Built with ❀️ for developers everywhere

/
/
All Tutorials
🎯

Kotlin

54 / 68 topics
54Multiplatform Programming55Shared Codebases
Tutorials/Kotlin/Multiplatform Programming
🎯Kotlin

Multiplatform Programming

Updated 2026-04-20
3 min read

Introduction

Multiplatform programming is a powerful feature of Kotlin that allows you to write code once and run it on multiple platforms, including JVM (Java Virtual Machine), JavaScript, and Native. This makes Kotlin an ideal choice for building cross-platform applications that can target different operating systems with minimal effort.

In this section, we will explore the advanced features of Kotlin's multiplatform programming capabilities, focusing on how to set up a project, share code across platforms, and handle platform-specific logic.

Setting Up a Multiplatform Project

To start a new multiplatform project in Kotlin, you can use IntelliJ IDEA or the command line. Here’s how to do it using IntelliJ IDEA:

  1. Open IntelliJ IDEA and select "Create New Project."
  2. Choose "Kotlin" from the list of available project types.
  3. Select "Multiplatform" under the Kotlin section.
  4. Configure your project settings, such as the name, location, and target platforms (JVM, JS, Native).
  5. Click "Finish" to create the project.

Project Structure

A typical multiplatform project structure looks like this:

my-kotlin-multiplatform-project/
β”œβ”€β”€ build.gradle.kts
β”œβ”€β”€ gradle.properties
β”œβ”€β”€ settings.gradle.kts
β”œβ”€β”€ shared/
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ commonMain/
β”‚   β”‚   β”‚   └── kotlin/
β”‚   β”‚   β”œβ”€β”€ jvmMain/
β”‚   β”‚   β”‚   └── kotlin/
β”‚   β”‚   β”œβ”€β”€ jsMain/
β”‚   β”‚   β”‚   └── kotlin/
β”‚   β”‚   └── nativeMain/
β”‚   β”‚       └── kotlin/
β”œβ”€β”€ jvmApp/
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   └── main/
β”‚   β”‚       └── kotlin/
β”œβ”€β”€ jsApp/
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   └── main/
β”‚   β”‚       └── kotlin/
└── nativeApp/
    β”œβ”€β”€ src/
    β”‚   └── main/
    β”‚       └── kotlin/
  • shared/: Contains shared code that can be used across all platforms.
  • jvmApp/, jsApp/, and nativeApp/: Each contains platform-specific code.

Sharing Code Across Platforms

Kotlin's multiplatform feature allows you to share code between different targets. Here’s how you can do it:

Common Code

Place your common code in the commonMain directory under the shared module. This code will be compiled for all target platforms.

// shared/src/commonMain/kotlin/com/example/shared/Greeting.kt
package com.example.shared

class Greeting {
    fun greet(): String = "Hello, from Kotlin Multiplatform!"
}

Platform-Specific Code

You can also write platform-specific code by placing it in the respective target directories (jvmMain, jsMain, nativeMain).

// shared/src/jvmMain/kotlin/com/example/shared/Greeting.kt
package com.example.shared

actual class Greeting {
    actual fun greet(): String = "Hello, from JVM!"
}
// shared/src/jsMain/kotlin/com/example/shared/Greeting.kt
package com.example.shared

actual class Greeting {
    actual fun greet(): String = "Hello, from JS!"
}
// shared/src/nativeMain/kotlin/com/example/shared/Greeting.kt
package com.example.shared

actual class Greeting {
    actual fun greet(): String = "Hello, from Native!"
}

Using Common Code in Platform-Specific Apps

You can use the common code in your platform-specific applications.

// jvmApp/src/main/kotlin/com/example/jvmapp/Main.kt
package com.example.jvmapp

import com.example.shared.Greeting

fun main() {
    println(Greeting().greet())
}

Handling Platform-Specific Logic

Sometimes, you need to handle platform-specific logic that cannot be shared. Kotlin provides a way to do this using the expect and actual keywords.

Expect Keyword

Use the expect keyword to declare a function or property in common code that must be implemented on each platform.

// shared/src/commonMain/kotlin/com/example/shared/PlatformUtils.kt
package com.example.shared

expect fun getPlatformName(): String

Actual Keyword

Implement the expected function or property using the actual keyword in each target-specific module.

// shared/src/jvmMain/kotlin/com/example/shared/PlatformUtils.kt
package com.example.shared

actual fun getPlatformName(): String = "JVM"
// shared/src/jsMain/kotlin/com/example/shared/PlatformUtils.kt
package com.example.shared

actual fun getPlatformName(): String = "JS"
// shared/src/nativeMain/kotlin/com/example/shared/PlatformUtils.kt
package com.example.shared

actual fun getPlatformName(): String = "Native"

Using Platform-Specific Logic

You can use the platform-specific logic in your common code.

// shared/src/commonMain/kotlin/com/example/shared/Greeting.kt
package com.example.shared

import com.example.shared.getPlatformName

class Greeting {
    fun greet(): String = "Hello, from $platformName!"
}

Best Practices

  1. Keep Common Code DRY: Avoid duplicating code across platforms by keeping as much logic as possible in the commonMain directory.
  2. Use Gradle for Dependency Management: Leverage Gradle to manage dependencies and build configurations for each platform.
  3. Test Across Platforms: Write tests for your shared code to ensure it works correctly on all target platforms.
  4. Document Platform-Specific Code: Clearly document any platform-specific logic or dependencies to help other developers understand the project structure.

Conclusion

Kotlin's multiplatform programming capabilities provide a powerful way to build cross-platform applications with minimal effort. By sharing common code and handling platform-specific logic effectively, you can create robust applications that run seamlessly on multiple platforms.

In this tutorial, we covered how to set up a multiplatform project, share code across platforms, and handle platform-specific logic using Kotlin's advanced features. With these tools at your disposal, you can build efficient and maintainable cross-platform applications.


PreviousKotlin for Native DevelopmentNext Shared Codebases

Recommended Gear

Kotlin for Native DevelopmentShared Codebases