Targeting two JVM versions from the same source with Kotlin MPP

I have a multiplatform library targeting JS and the JVM. I’m currently compiling for JVM9+, but I also want to compile the same source to 1.8 separately for legacy compatibility. It could then be published to Maven with a classifier like “jvm8” or maybe as a separate module.

So how would I go about doing this, if it’s even possible?

I was thinking something with compilations, like this:

kotlin {
    jvm {
        compilations {
            val main by getting {
                kotlinOptions {
                    jvmTarget = "9"
                    // ...
                }
            }

            val jvm8 by creating {
                kotlinOptions {
                    jvmTarget = "1.8"
                    // ...
                }
            }
        }
    }
}

However, I’m unclear on how to share the source between the two. I’m also unclear how I would configure the publishing.

2 Likes

I don’t think that’s possible, I remember some seeing some issues recently with supporting Java and Android in the same Kotlin Multiplatform module, so I assume we would have the same problem producing two JVM ones.

This might help, though:

I see, that’s a shame. I had started looking into multirelease jars, but I ended up just targeting 1.8. I’ll probably try figuring it out again at some point