https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
g

Gamadril

06/04/2021, 8:37 AM
How can I solve the "The 'java' plugin has been applied, but it is not compatible with the Android plugins." problem if my KMM module has to target both platforms: jvm and android
a

andylamax

06/04/2021, 9:21 AM
Copy code
plugins {
  kotlin("multiplatform")
  // java - dont do this
}
Is enough to target both jvm and android
g

Gamadril

06/04/2021, 9:31 AM
if I have java code in my jvmMain and provide
Copy code
kotlin {
    jvm {
        withJava()
    }
}
the java plugin is always applied automatically by kotlin.multiplatform plugin
m

matej

06/04/2021, 9:41 AM
@Gamadril I don't think you can have both a
jvmMain
and an
androidMain
in the same project yet, you have to pick one.
g

Gamadril

06/04/2021, 10:07 AM
what's the sense of a Kotlin multiplatform module then? my jvmMain code is based on Springboot, androidMain is using android-only libraries. They are not compatible at all..
a

andylamax

06/04/2021, 10:09 AM
You van have
jvmMain
and
androidMain
Copy code
plugins {
  id("com.android.library")
  kotlin("multiplatform")
}

kotlin {
  android {}
  jvm {
    // withJava() - not allowed if android {} is present
  }
}
hope that helps
You also can have a common sourceSet for android and jvm
c

Casey Brooks

06/04/2021, 1:51 PM
YOu can definitely have both JVM and Android targets, and optionally make Android extend JVM. Here’s the build script for a logging library I maintain that has separate JVM/Android sourcesets if you want a working example https://github.com/copper-leaf/clog/blob/master/clog-core/build.gradle.kts
g

Gamadril

06/07/2021, 5:01 AM
@Casey Brooks: thanks, my build.gradle file looks similar to yours. However if you add withJava() inside jvm {} you will get the same error
@andylamax: cannot find any note in official docs (https://kotlinlang.org/docs/mpp-configure-compilations.html#include-java-sources-in-jvm-compilations) that android {} is not allowed if jvm {} is used in combination with
withJava()
c

Casey Brooks

06/07/2021, 4:38 PM
That error is correct, he Android plugin is not compatible with the normal Java gradle plugin. It’s always been this way, even outside of Kotlin, and is not going to change. But you only need the java plugin applied if you’re using actual Java source files. If you’re writing the library in pure Kotlin source, it will still work just fine without using
withJava()
, you’ll still have access to all the Java stblib classes and any Java dependencies in your
jvmMain
sourceset
If you do need to compile Java source files alongside your Kotlin ones, you’ll have to make separate Gradle modules for your Java and Android features
70 Views