无法针对Kotlin多平台使用commonMain中的依赖项

时间:2020-01-13 15:58:34

标签: kotlin kotlin-multiplatform

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

multiplatform-lib

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
)

multiplatform-test

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突出显示为Red。没有可用于“测试”的自动完成功能。 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'.

1 个答案:

答案 0 :(得分:1)

假设您已在本地发布并且成功发布,那么首先要更改的是依赖项:

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

您可能不希望使用metadata工件。

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

enableFeaturePreview("GRADLE_METADATA")

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

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