How can I add a target for jvm on windows and one ...
# multiplatform
z
How can I add a target for jvm on windows and one for linux that both use the common code?
j
build.gradle.kts
Copy code
kotlin {
    jvm()
    linuxX64()
    ...
}
JVM target isn't specific to Windows, but will run on all JVM compatible machines.
z
is there no way to make a seperate jvm target for each os?
j
No, but you can programmatically check the OS at runtime and call separate routines depending on the OS. What's your requirement for this?
z
I want to be able have
expect
functions in my common code then the actual declarations in the specific platform that uses platform apis
j
What OS-specific platform APIs are you requiring in JVM code?
z
how windows handles storage devices, and the way paths are displayed
j
You'll want to handle this in the typical way for any Java app, i.e.
<http://java.io|java.io>.File.separator
(more details).
My example code above creates a JVM target and a Kotlin/Native Linux target. If your goal is to create a single JVM application, which runs on all JVM-compatible machines, but with some platform-specific nuances, expect/actual with separate targets won't accomplish this goal. Separate targets will each produce their own unique binary output which is specific to that platform. A separate Windows and Linux Kotlin/Native target would create a platform-specific native binary for each target. E.g.:
Copy code
kotlin {
    mingwX64()
    linuxX64()
    ...
}
z
I can only use JVM, so native wouldnt work for me
j
In that case, it sounds like you're going to want to create a single-target Kotlin JVM app and then use the typical Java APIs to flesh out the platform-specific behaviors.
The way expect/actual works, common expect code will have a single actual implementation per target output. Technically you could accomplish a separate JVM output that you would only run on a specific platform. But that would break the portability of the JVM binary.
Copy code
kotlin {
    jvm("jvmWindows")
    jvm("jvmLinux")

    sourceSets {
        val jvmWindowsMain by getting {
            ...
        }
        val jvmLinuxMain by getting {
            ...
        }
    }
}
This would create two separate JVM source sets, where you could implement actuals for common expects on two separate JVM platform targets.
e
I think the jvm was made to handle the case where you can't use native and want common code on different platforms 😅