Is it possible to have a JVM common module?

To cut to the chase, the issue I’m running into is that I can’t access the Java SDK in my common module because the Kotlin multiplatform gradle plugin assumes that “multiplatform” means “multiple languages/runtimes”, while in my case “multiplatform” means “multiple glue implementations”. I’d love to hear that I’m wrong about this, but if not I’ll probably open an issue.

I’m currently working on setting up a Minecraft mod library that I’d like it to run on two “platforms” (the Minecraft Forge and the Fabric mod loaders) using largely common code and a couple glue modules. This seemed like a perfect place to use a Kotlin multiplatform project, however I ran into the issue above.

The Kotlin IDEA plugin seems to somehow hard-code the lack of a platform SDK, so even if I set the common module’s SDK to a Java one I still can’t access anything from Java. I spent several hours last night trying to get it to work, even going so far as to try and modify the .iml files, but to no avail.

Stripping the problem down to its bare minimum, I want this to compile in the common module:

import com.teamwizardry.librarianlib.core.platform.Platform
import java.io.File

object Common {
    fun printImpl() {
        val file = File(Platform.platformName)
        println(file.absolutePath)
    }
}

Here’s the full project: multiplatform_test.zip (85.7 KB)

Hi there,

If I am not mistaking, Kotlin MPP was not design with this use case in mind and you might not be able to achieve what you are looking for with it. See this forum post for more info.

Common code must be pure kotlin code, without any kind of platform SDK available, so unless you interface the Java SDK with expect/actual semantic you wont be able to use them.

Another way would be to have multiple gradle projects, a shared one containing your code that will be able to use the Java SDK and two others for your Forge and Fabric mod loaders that will use the shared one. This might be your best option in my opinion

Hope this helps.

1 Like

I hope this is in progress. Vote for it https://youtrack.jetbrains.com/issue/KT-28194

There is a more common use case for this which is shared implementation targeting Android and the JVM or different jdk versions. Is works in gradle, but Intellij does not properly support it (you can configure it by hand every time, but that gets old quickly as well).