将模块作为依赖项包含到 KMM 项目的共享模块中

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

我有一个可用的 KMM 应用程序,我有一个 java 模块 mymodule,我用 File->New->Module->Java or Kotlin library.

创建了它

该模块位于 androidAppiosAppshared 旁边的顶层。在我的 settings.gradle.kts 我有 include(":mymodule").

我想在 shared 模块中使用 mymodule。所以我进入 shared 模块的 build.gradle.kts 并尝试将我的模块包含在 commonMain:

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

错误是Could not resolve MyKMMApplication:mymodule:unspecified并且:

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

我尝试过的事情

如何将模块作为依赖包含到 KMM 的共享模块中?

因此,您包含到 shared 中的模块需要是一个多平台模块。它的 build.gradle.kts 文件应如下所示:

plugins {
    id("org.jetbrains.kotlin.multiplatform")
}

kotlin {
  jvm()
  iosX64()
  iosArm32()
  iosArm64()
}

它的项目结构应该类似于:mymodule/src/commonMain/kotlin/com/example/mymodule/.