I have a multiplatform kotlin project for jvm/js. I'd like to extract some common code that's not compile target specific to reuse in both applications.
Following is my project structure:
├── api
│ ├── build.gradle
│ └── src
├── server
│ ├── build.gradle
│ └── src
├── client
│ ├── build.gradle
│ └── src
├── build.gradle
├── gradle.properties
└── settings.gradle
api/build.gradle:
plugins {
id 'org.jetbrains.kotlin.multiplatform'
}
group 'io.codegoblin'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
kotlin {
jvm()
js {
useCommonJs()
browser()
}
sourceSets {
commonMain {
dependencies {
implementation kotlin('stdlib-common')
}
}
}
}
client/build.gradle
plugins {
id("org.jetbrains.kotlin.js")
}
repositories {
mavenCentral()
maven { url = "https://dl.bintray.com/kotlin/kotlin-eap" }
maven { url = "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/kotlin-js-wrappers" }
maven { url = "https://kotlin.bintray.com/kotlin-js-wrappers/" }
}
kotlin {
js {
browser {
commonWebpackConfig {
cssSupport.enabled = true
}
}
binaries.executable()
}
}
dependencies {
implementation project(":api")
implementation("org.jetbrains:kotlin-react:17.0.2-pre.154-kotlin-$kotlin_version")
implementation("org.jetbrains:kotlin-react-dom:17.0.2-pre.154-kotlin-$kotlin_version")
implementation(npm("react", "$react_version"))
implementation(npm("react-dom", "$react_version"))
}
tasks.register("stage") {
dependsOn("build")
}
kotlin_version is 1.5.0
gradle is 7.1
With this setup project compiles the the api server and client modules with appropriate compile target, however the code from :api is not visible in the :client module nor it is present in the compiled :client js jar. I suppose I need to set up this dependency at the javascript level?
How do I make multiproject module dependency work in a javascript module?
CodePudding user response:
Your client
module should be multiplatform too, it can have only one platform (js
), e.g.:
- add
kotlin("multiplatform")
plugin - declare dependencies inside
kotlin.sourceSets.jsMain
CodePudding user response:
Echoing Philip Dukhov here, so your client needs to know how to consume that KMP-library, for which you have a couple of options:
- Probably the easiest is to let the
kotlin-multiplatform
gradle plugin do the work (as Philip suggested), which takes care of getting the right target and building the correct output to be consumed by your client module. - Alternatively, you might not want to introduce kmp specific logic in your client module, in that case you need to build the js specific output and provide it to your client module (since your client module lives already in the Kotlin world, I suppose this is not really what you want)
Hopefully this gives you the full picture