Search code examples
gradleintellij-ideakotlin-multiplatform

Why I need to place main function in jvmMain in my kotlin multiplatform project?


I decided to rename all of my src/main/java directories to src/main/kotlin, and while doing so, I noticed that the code of my main module is in src/jvmMain/java, so I moved it to src/commonMain/kotlin (why not?, I thought), and all was fine, all worked out. But after restarting the IDE, when starting my project, something went wrong:

Error: Could not find or load main class MainKt
Caused by: java.lang.ClassNotFoundException: MainKt

build.gradle.kts

I solved it by moving the main function to jvmMain; now it looks like this:

fun main() = application { App() }

, why placing this in commonMain has broken everything?

Pure question: Why placing main function for app in commonMain module instead of jvmMain module was not worked out?


Solution

  • After more research, I have come to conclusion about these modules structure, commonMain module contains shared code (like shared module in kmm) that will be used by platform-specific modules of project and by jvmMain module and jvmMain module is used to call the main() function of the app and thats why main() function must be placed in jvmMain module.

    This means that it was the wrong decision to put All my program code inside the commonMain module; instead, I must put there only shared components, like MaterialTheme colors and paddings, for example.

    dependencies diagram