在项目中未解析`runBlocking`协程生成器(已解析其他生成器) [英] `runBlocking` coroutine builder is not resolved in the project (Other builders are resolved)
问题描述
正如标题所示,我刚刚在build.gradle中添加的协程库中缺少协程生成器runBlocking
.有趣的是,其他所有东西似乎都可用,所有GlobalScope
,CoroutineScope.launch
CoroutineScope.async
都存在. runBlocking
不是.我在做什么错了?
As the title suggest, the coroutine builder runBlocking
is missing in the coroutine liblary I just added in my build.gradle. Funny thing is every other thing appears to be available, GlobalScope
, CoroutineScope.launch
CoroutineScope.async
all present. runBlocking
isn't. What am I doing wrong?
这是我的build.gradle
buildscript {
ext {
ktor_version = "1.1.1"
kotlin_version = "1.3.20-eap-52"
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-frontend-plugin:0.0.44"
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
}
}
plugins {
id 'kotlin-multiplatform' version '1.3.20-eap-100'
}
repositories {
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
maven { url 'https://dl.bintray.com/kotlin/kotlin-js-wrappers' }
maven { url 'https://dl.bintray.com/kotlinx/kotlinx' }
maven { url "https://kotlin.bintray.com/kotlinx" }
jcenter()
mavenCentral()
}
group 'books'
version '0.0.0'
apply plugin: 'maven-publish'
apply plugin: "org.jetbrains.kotlin.frontend"
kotlin {
jvm() {
compilations.all {
tasks[compileKotlinTaskName].kotlinOptions {
jvmTarget = "1.8"
}
}
}
js() {
compilations.all {
tasks[compileKotlinTaskName].kotlinOptions {
def optDir = compileKotlinTaskName.contains("Test") ? "test/${project.name}.test.js" : "main/${project.name}.js"
kotlinOptions.metaInfo = true
kotlinOptions.outputFile = "$project.buildDir.path/js/$optDir"
kotlinOptions.sourceMap = true
kotlinOptions.moduleKind = 'commonjs'
kotlinOptions.main = "call"
}
}
}
sourceSets {
commonMain {
dependencies {
implementation kotlin('stdlib-common')
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$ktor_version"
}
}
commonTest {
dependsOn commonMain
dependencies {
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}
jvmMain {
dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$ktor_version"
implementation kotlin('stdlib-jdk8')
}
}
jvmTest {
dependsOn jvmMain
dependencies {
implementation kotlin('test')
implementation kotlin('test-junit')
}
}
jsMain {
dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$ktor_version"
implementation kotlin('stdlib-js')
}
}
jsTest {
dependsOn jsMain
dependencies {
implementation kotlin('test-js')
}
}
}
}
task runJest(type: Exec) {
group = "verification"
commandLine "sh", "runJest.sh"
}
runJest.dependsOn(jsTest)
task testAll() {
group = "verification"
dependsOn(jvmTest, runJest)
}
kotlinFrontend {
npm {
devDependency("karma")
}
sourceMaps = true
webpackBundle {
bundleName = "main"
host = "0.0.0.0"
contentPath = file("$buildDir.path/resources/main")
}
}
有了该gradle配置,我已经能够使用kotlin-multiplatform很好地编写测试(学习TDD).这是我的以下示例
With that gradle configuration, I have been able to write tests well (Learning TDD) with kotlin-multiplatform. And here is my sample below
import kotlin.test.*
import com.luge.books.*
import kotlinx.coroutines.*
class BookTest {
@BeforeTest
fun setup() {
val book = Book()
}
@Test
fun testingInstantiation() {
val book = Book()
assertEquals(book.year, 1990, "Books do match the year")
}
@Test
fun willFail() {
assertFalse(false)
}
@Test
fun testingCoroutines() {
val job = GlobalScope.launch {
delay(5000)
println("Doing stuff")
assertTrue(false)
}
}
}
如果您仔细观察,则测试testingCoroutines
通过,但是由于我是从GlobalScope
启动的,因此它会触发并遗忘,并且测试将返回而不会引发任何错误.如果我将runBlocking
合并,IDE会用红色突出显示它(您知道,因为它并不算什么),甚至要结束kotlin编译器的喊叫unresolved reference runBlockin
.请帮助....
If you look closely, the test testingCoroutines
passes, but since I am launching from the GlobalScope
, it just fires and forgets and the test returns without throwing any error. If I incoporate runBlocking
, the IDE highlights it with red color (you know, as something it doesn't understant), end even the kotlin compiler shouts, unresolved reference runBlockin
. Help please....
推荐答案
在这里和那里苦苦挣扎之后,我终于知道runBlocking仅在kotlin/jvm中可用.因此,它不在kotlin/js或kotlin/common中.
After struggling here and there, I finally knew that runBlocking is only available in kotlin/jvm. So, it is not in kotlin/js or kotlin/common.
仅供以后参考,如果要运行多平台测试,请使用此解决方法
Just for future references, if you want to run multiplatform tests, then use this work around
这篇关于在项目中未解析`runBlocking`协程生成器(已解析其他生成器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!