Search code examples
kotlinkotlin-multiplatform

Include a module as a dependency into a KMM project's shared module


I have a working KMM application, and I have a java module, mymodule, that I created with File->New->Module->Java or Kotlin library.

The module exists at the top level beside androidApp, iosApp and shared. In my settings.gradle.kts I have include(":mymodule").

I want to use mymodule in the shared module. So I go into the shared module's build.gradle.kts and I try to include my module in commonMain:

kotlin {
    ...    
    sourceSets {
        val commonMain by getting {
          dependencies {
              implementation(project(":mymodule"))
          }
        }
        ...
    }
    ...
}
...

And the error is Could not resolve MyKMMApplication:mymodule:unspecified and:

Could not resolve project :mymodule.
Required by:
    project :shared

Things I've tried

  • I can put dependencies { implementation(project(":mymodule")) } at the bottom of shared's build.gradle.kts and but still the same error appears
  • As to test if there's other problems, I can also import mymodule into the Android project without problems
  • I can include implementation("com.squareup.sqldelight:runtime:1.5.3") in commonMain and see those classes in the shared module no problem
  • The docs say you can include another multiplatform module, but nothing about a normal module.

How can I include a modules into KMM's shared module as a dependency?


Solution

  • So, the module you include into shared needs to be a multiplatform module. It's build.gradle.kts file should look something like this:

    plugins {
        id("org.jetbrains.kotlin.multiplatform")
    }
    
    kotlin {
      jvm()
      iosX64()
      iosArm32()
      iosArm64()
    }
    

    And it's project structure should look something like: mymodule/src/commonMain/kotlin/com/example/mymodule/.