handstandsam
04/18/2022, 1:26 PMjava-library
to a kotlin("multiplatform")
project configuration? I'm looking to slowly introduce Kotlin Multiplatform on a few modules that currently only target the JVM. I'd like a way to swap between the two so there is less hesitation from other developers to give it a try. Basically it would make the KMP module "backwards compatible" to just a java-library, and completely avoid any references to multiplatform plugins when it was switched off.
Goals:
• Just change "myplugin.jvm.lib" to "myplugin.kmp.lib".
• This would take all dependencies
from the java-library and make them dependencies on commonMain
.
• It would be nice to just change one line and see if it compiles to Kotlin Multiplatform as commonMain
.
I've given this a try, but when trying to map dependencies I get: Unresolved reference: implementation
because it should be a commonMain
dependency instead. I've thought about two ways of approaching this:
• See if I can just iterate through declared dependencies, move them to commonMain, and remove them from the default dependencies block.
• Create a custom "commonDependencies {}" extension that can then map to either.
I'm looking at how I can do this, but wanted to see if any tools or processes currently exist to make a one-line backwards compatible (java-library instead of kotlin("multiplatform")) module?handstandsam
04/18/2022, 1:27 PMsrc/main/java
and src/main/kotlin
to commonMain
so that this could work.handstandsam
04/18/2022, 1:30 PMjava-library
right now that has the issue saying Unresolved reference: implementation
if I replace kotlin("jvm")
with kotlin("multiplatform")
and add the other config in the screenshot above ☝️Javier
04/18/2022, 1:41 PMJavier
04/18/2022, 1:44 PMplugins {
kotlin("multiplatform")
}
kotlin {
jvm()
}
// same as below
kotlin {
sourceSets {
commonMain {
dependencies {
implementation(...)
}
}
}
}
// same as above
dependencies {
commonMainImplementation(...)
}
Javier
04/18/2022, 1:50 PMcommonMain
, if it works, it is probably that your project can be moved to kmp easily (except some JVM APIs like java File which can be accessed via stdlib, but you can move all code into commonMain, so you can check that problem tooephemient
04/18/2022, 2:51 PMkotlin {
targets {
jvm {
withJava()
I haven't tried but does this help? it should apply the standard Java plugin which does act on the top-level dependencies clockhandstandsam
04/18/2022, 2:52 PMJavier
04/18/2022, 2:54 PMJavier
04/18/2022, 2:55 PMJavier
04/18/2022, 2:55 PMhandstandsam
04/18/2022, 2:57 PMhandstandsam
04/18/2022, 2:59 PMJavier
04/18/2022, 2:59 PMhandstandsam
04/18/2022, 3:00 PMephemient
04/18/2022, 3:04 PMephemient
04/18/2022, 3:07 PMhandstandsam
04/18/2022, 3:18 PMhandstandsam
04/18/2022, 4:36 PMcommonMain by getting
because it exists. In the case of KMP, you just have to create the main
sourceset before it is configured. So I added in a main by creating
and then have commonMain { dependsOn(main) }
🎉
Thanks for all the brainstorming @Javier and @ephemient!handstandsam
04/18/2022, 4:38 PMhandstandsam
04/18/2022, 4:52 PMhandstandsam
05/05/2022, 7:00 PMhandstandsam
05/05/2022, 7:01 PMcommonMain
instead of commonMain->main
because that case is not supported. https://github.com/handstandsam/ShoppingApp/pull/50/files