Kotlin本地错误:未解决的引用协程。

3

我正在尝试在Windows上构建本地应用程序。

我不确定在哪里添加实现'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5'的依赖项。

我的当前gradle文件如下所示:

buildscript {
    ext.kotlin_version = '1.3.72'
    repositories {
        mavenCentral()

    }
    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
    }
}

plugins {
    id 'org.jetbrains.kotlin.multiplatform' version '1.3.72'
}

dependencies {
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5'
}

repositories {
    mavenCentral()
    jcenter()
}
kotlin { 
    mingwX64("mingw") {
        binaries {
            executable {
                // Change to specify fully qualified name of your application's entry point:
                entryPoint = 'sample.main'
                // Specify command-line arguments, if necessary:
                runTask?.args('')
            }
        }
    }
    sourceSets { 
        mingwMain {

        }
        mingwTest {
        }

    }
    experimental {
        coroutines 'enable'
    }

}

这个依赖项出现了错误:

dependencies {
        implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5'
    }

错误信息如下:

Could not find method implementation() for arguments [org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

如果我移除该依赖项,则在尝试导入kotlinx.coroutines.*和kotlin.concurrent.thread时会出现“unresolved Reference”的错误提示。
需要在这方面得到一些帮助,感谢。谢谢。
1个回答

1

org.jetbrains.kotlinx:kotlinx-coroutines-core-native

也就是说,Kotlin/Native仅支持Gradle 4.10版本,并且您需要在settings.gradle文件中启用Gradle元数据:
enableFeaturePreview('GRADLE_METADATA')
由于Kotlin/Native通常不提供版本之间的二进制兼容性,因此您应该使用与构建kotlinx.coroutines时使用的Kotlin/Native编译器版本相同的版本。

https://github.com/Kotlin/kotlinx.coroutines/blob/master/README.md


@Dimitri 非常感谢。这完全解决了问题。 - Prana
运行时遇到一个问题:fun main() { GlobalScope.launch { delay(1000L) println("World!") } println("Hello,") Thread.sleep(2000L) //No Thread!! } 包中找不到线程....未解决的引用.... - Prana
这里似乎找不到 kotlin.concurrent 包!! - Prana
是的,目前并发功能只适用于JVM。请访问https://kotlinlang.org/api/latest/jvm/stdlib/,向下滚动到concurrent部分,您将只看到绿色圆圈(JVM)。 - Dmitri
是的,现在可以看到了。谢谢。 - Prana

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接