I have some problems setting up a dependency in a ...
# kotlin-native
j
I have some problems setting up a dependency in a KMM project from a jvmmodule to the shared code. In short, the attached image shows what I want to do. The jvmmodule has a class called Test with a main method which uses code from the shared module. I tried several solutions, none which are 100% working: 1. (https://github.com/jcraane/kmm-with-jvmmodule/tree/jvm-kotlin-lib) jvmmodule is configured as a regular kotlin jvm module which also has the application plugin enabled for testing purposes. The module has a project dependency on the shared module. When the Test class is ran using the ./gradlew jvmmodulerun command everything works as expected. The output in the console should be ‘Hello from JVM’. In IntelliJ or Android Studio the Greeting class in Test.kt cannot be found (not in the editor, compile error) and not at runtime. When running the classes from the gutter icon the following error occurs:
Copy code
Caused by: java.lang.ClassNotFoundException: Greeting
   at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
   at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
   at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
   ... 1 more
2. (https://github.com/jcraane/kmm-with-jvmmodule/tree/jvm-mp-lib) (inspired by kotlinconf app) jvmmodule is configured as a multiplatform module with a single jvm target and a project dependency on the shared module. In this scenario code completion works in IntelliJ but when the Test class is run from the gutter icon the following error occurs:
Copy code
Error: Could not find or load main class TestKt
Caused by: java.lang.ClassNotFoundException: TestKt
Is this the correct way of using shared code from a plain kotlin (kvm) module?
a
if I were you, I would add “jvmAndAndroid”
j
Where exactly do you mean, in the jvmmodule? The shared code has an android and jvm target.
a
Copy code
val androidAndJvmMain by creating {
    dependsOn(commonMain)
}
val androidMain by getting {
    dependsOn(androidAndJvmMain)
}
val jvmMain by getting {
    dependsOn(androidAndJvmMain)
}
val iOSMain by getting {
    dependsOn(commonMain)
}
j
Yes, I can structure the source sets a little bit better with that but it does not solve my original issue unfortunately.