首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能对kotlin多平台使用commonMain中的依赖项

不能对kotlin多平台使用commonMain中的依赖项
EN

Stack Overflow用户
提问于 2020-01-13 15:58:34
回答 1查看 1.9K关注 0票数 3

我不知道如何在kotlin多平台项目中获得commonMain依赖项。我已经看了很多次文档,看了很多例子,但它只是不起作用。下面是我认为应该奏效的最小的例子。我做错了什么?

多平台库

代码语言:javascript
复制
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

代码语言:javascript
复制
data class Test (
    val test : Int
)

多平台试验

代码语言:javascript
复制
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

代码语言:javascript
复制
import github.fatalcatharsis.Test

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

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

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

代码语言:javascript
复制
implementation("github.fatalcatharsis:multiplatform-lib:1.0-SNAPSHOT")

它会产生错误:

代码语言:javascript
复制
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'.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-13 16:15:45

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

代码语言:javascript
复制
implementation("github.fatalcatharsis:multiplatform-lib:1.0-SNAPSHOT")

你可能不想要metadata神器。

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

代码语言:javascript
复制
enableFeaturePreview("GRADLE_METADATA")

然后,尝试在命令行上构建。有时候intellij确实能看到一切。

如果事情仍然不工作,我会开始查看您的发布配置。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59719991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档