Search code examples
kotlinkotlin-multiplatform

JVM target unable to import common classes in Kotlin multiplatform project


After creating a KMM project with both Android and iOS apps working without issues. I tried adding a new target JVM to the project to implement a basic backend service.

I have seen this working in other projects such as https://github.com/joreilly/PeopleInSpace but even if I think it must be right in front of me, I am not able to tell the difference with this one.

I created a sample project to reproduce it: https://github.com/RafaO/Surf you can check it out and see that inside the JVM client the class Greeting is not found.

Any ideas why this is happening are appreciated.

Thank you very much!


Solution

  • I'm not sure if that's the only way, but I'm using multiplatform backend module with single jvm source set.

    Example I've being using as a reference: https://github.com/JetBrains/kotlinconf-app

    my own setup:

    plugins {
        kotlin("multiplatform")
        kotlin("plugin.serialization")
        id("com.squareup.sqldelight")
        application
        id("com.github.johnrengelman.shadow") version "7.0.0"
    }
    
    application {
        mainClass.set("io.ktor.server.netty.EngineMain")
        @Suppress("DEPRECATION")
        mainClassName = mainClass.get()
    }
    
    sqldelight {
        database("Database") {
            packageName = "com.app.server"
            dialect = "mysql"
        }
    }
    
    kotlin {
        jvm {
            withJava()
        }
        sourceSets {
            val jvmMain by getting {
                dependencies {
                    implementation(project(":common"))
                    // ...
                }
            }
        }
    }
    
    tasks.withType<Jar> {
        manifest {
            attributes(
                mapOf(
                    "Main-Class" to application.mainClass.get()
                )
            )
        }
    }
    
    tasks.named<AbstractCopyTask>("jvmProcessResources") {
        duplicatesStrategy = DuplicatesStrategy.WARN
    }
    

    target run for debug and shadowJar for making an executable.