Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin multiplatform check execution platform within common code

I have a common module that is consumed by JVM, JS, and Native projects. Within the common module, I would like to do something like the following:

fun currentPlatform(): String {
  // return "JVM", "JS", or "Native" depending on where this code is executing.
}
like image 828
Matt Groth Avatar asked Sep 08 '25 00:09

Matt Groth


1 Answers

In the common module, I have

enum class KotlinPlatform {
 JVM,JS,Native
}
expect val currentPlatform: KotlinPlatform

In the JVM module, I have:

actual val currentPlatform = KotlinPlatform.JVM

And the above can be repeated for JS and any other modules as well.

like image 153
Matt Groth Avatar answered Sep 10 '25 03:09

Matt Groth