Search code examples
kotlingradlekotlin-multiplatformkotlin-js

Koltin Multiplatform Multi Module JVM/JS Project Setup


I need to convert existing multi-module jvm project into a multiplatform project.

//Exisiting Modules: (JVM Project)
core
data
app
app_server
utils
db
//Need to add:
app_frontend (Kotlin/JS)

Need to share data module between JVM and JS Thanks in advance.


Solution

  • I'm assuming you want to share your data module between JVM and JS. For this, your data module has to be a multiplatform project targeting JVM and JS. The most basic setup would be:

    // build.gradle.kts
    
    plugins {
        kotlin("multiplatform")
    }
    
    group = "data"
    
    kotlin {
        jvm()
        js { browser { binaries.executable() } }
    
        sourceSets["commonMain"].dependencies {
            // Your dependencies...
        }
    }
    

    Other modules can be built with "regular" plugins targeting a specific platform, so your JVM modules won't need any adjustments. The basic setup for JS would be:

    plugins {
        id("org.jetbrains.kotlin.js")
    }
    
    group = "app_frontend"
    
    dependencies {
        implementation(project(":data"))
        implementation(kotlin("stdlib-js"))
    }
    kotlin {
        js {
            browser { binaries.executable() }
        }
    }
    

    Also checkout the official documentation about multiplatform programming: