I have a Compose multiplatform app (desktop,androi...
# compose-desktop
m
I have a Compose multiplatform app (desktop,android,ios). In the desktopMain source set I’d like to use some Java code together with some Kotlin code. I can place a Java file in the corresponding Kotlin source folder and use it from the Kotlin code in the same folder. There are no complaints from IntelliJ. The app also compiles and runs fine until it reaches the point where the Java code is used. At that point I get a class not found exception. Can anybody tell me how I can configure that properly and why there are no indications of any problem at build and launch time?
m
You need to add withJava() in your module build.gradle jvm() { withJava() } And put you java files under src/java not src/kotlin
m
Is there also a way to mix Java and Kotlin in the same src/kotlin folder? The reason is because I want to later convert these classes to Kotlin and I do not want to move them around one by one.
m
AFAIK it's possible to mix Java and Kotlin in src/java but not in src/kotlin.
m
I now have this
Copy code
kotlin {
   jvm("desktop") {
      withJava() // In order to be able to compile Java code too.
   }
...
in my build.gradle.kts but I only get a
Copy code
Caused by: org.gradle.api.InvalidUserCodeException: 'withJava()' is not compatible with Android Plugins
when I sync it.
h
You don’t need
withJava
to compile Java code. The
withJava
applies the java Gradle plugin and this plugin is incompatible with the Android plugin.
m
Then I am back to square one. See my original question.
e
if you have both Kotlin Android and JVM multiplatform targets in a project, Java code will need to move to a separate project
🙏 1
😢 1