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.
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:
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/
Kotlin's multiplatform feature allows you to share code between different targets. Hereβs how you can do it:
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!"
}
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!"
}
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())
}
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.
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
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"
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!"
}
commonMain directory.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.