【问题标题】:Cannot use dependencies in commonMain for kotlin multiplatform无法在 commonMain 中为 kotlin 多平台使用依赖项
【发布时间】:2020-04-30 08:46:02
【问题描述】:

我无法弄清楚如何让 commonMain 依赖项在 kotlin 多平台项目中工作。我已多次阅读并重新阅读文档并查看了许多示例,但它只是不起作用。这是我认为应该起作用的最小示例。我做错了什么?

多平台库

plugins {
    kotlin("multiplatform") version "1.3.61"
    `maven-publish`
}

group = "github.fatalcatharsis"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
    mavenLocal()
}

kotlin {
    /* Targets configuration omitted. 
    *  To find out how to configure the targets, please follow the link:
    *  https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#setting-up-targets */

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(kotlin("stdlib-common"))
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
    }
}

src/commonMain/kotlin/Test.kt

data class Test (
    val test : Int
)

多平台测试

plugins {
    kotlin("multiplatform") version "1.3.61"
}

group = "github.fatalcatharsis"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
    mavenLocal()
}

kotlin {
    /* Targets configuration omitted. 
    *  To find out how to configure the targets, please follow the link:
    *  https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#setting-up-targets */

    js {
        browser()
    }

    jvm()

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(kotlin("stdlib-common"))
                implementation("github.fatalcatharsis:multiplatform-lib-metadata:1.0-SNAPSHOT")
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
    }
}

src/commonMain/kotlin/Tester.kt

import github.fatalcatharsis.Test

fun test() {
    val meh = Test()
}

intellij 说它在项目菜单上很好地解决了依赖关系,但将 github 突出显示为红色。 “测试”没有可用的自动完成功能。 multiplatform-test\src\commonMain\kotlin\Tester.kt: (1, 8): Unresolved reference: github 的错误。只是看起来依赖内容在 commonMain 中不可用。我觉得我错过了一些微妙而明显的东西。有什么想法吗?

编辑:如果我将依赖项更改为:

implementation("github.fatalcatharsis:multiplatform-lib:1.0-SNAPSHOT")

它会产生错误:

Could not determine the dependencies of task ':jsPackageJson'.
> Could not resolve all dependencies for configuration ':jsNpm'.
   > Could not resolve github.fatalcatharsis:multiplatform-lib:1.0-SNAPSHOT.
     Required by:
         project :
      > Unable to find a matching variant of github.fatalcatharsis:multiplatform-lib:1.0-SNAPSHOT:
          - Variant 'metadata-api':
              - Found org.gradle.status 'integration' but wasn't required.
              - Required org.gradle.usage 'kotlin-runtime' and found incompatible value 'kotlin-api'.
              - Required org.jetbrains.kotlin.platform.type 'js' and found incompatible value 'common'.

【问题讨论】:

    标签: kotlin kotlin-multiplatform


    【解决方案1】:

    假设你已经在本地发布,并且成功了,那么首先要改变的是依赖:

    implementation("github.fatalcatharsis:multiplatform-lib:1.0-SNAPSHOT")
    

    您可能不想要 metadata 工件。

    接下来,将以下内容添加到您的测试应用的 settings.gradle 文件中。

    enableFeaturePreview("GRADLE_METADATA")
    

    之后,尝试在命令行上构建。有时 intellij 确实可以看到一切。

    如果仍然无法正常工作,我会开始查看您的发布配置。

    【讨论】:

    • 我的第一个项目没有定义目标,只有公共源集,唯一产生的工件是多平台库元数据,这很好解决。在 multiplatform-test 中删除 dep 会产生未解决的 dep 错误,因此它可以毫无问题地找到 -metadata。我的 .m2 文件夹包含我所期望的 multiplatform-lib-metadata.jar 。我已将 enableFeaturePreview 添加到我的多平台测试和多平台库中,但没有任何变化。从命令行构建相同的结果。
    • 暂别我所说的,没有看到 multiplatform-lib 文件夹。生成了一个没有 jar 的文件夹,并带有一个用于 multiplatform-lib 的“.module”文件。当我将我的 dep 更改为 implementation("github.fatalcatharsis:multiplatform-lib:1.0-SNAPSHOT") 时,它会产生一个错误,我将把它添加到我的问题描述中。
    • 接受这个作为答案,因为它最终导致了我的解决方案。关闭元数据首先向我展示了 lib 必须实现消费者实现的目标(js 和 jvm)。其次,我的项目结构错误(没注意,但我的源实际上在,src/commonMain/**main**/kotlin,而它应该在src/commonMain/kotlin。在我的实际项目中,这揭示了很多以前没有出现的错误。一旦解决,我就可以将它包含在测试项目中,没问题。
    猜你喜欢
    • 2020-04-30
    • 2021-04-27
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多