项目中未解析`runBlocking`协程构建器(其他构建器已解析)
`runBlocking` coroutine builder is not resolved in the project (Other builders are resolved)
如标题所示,协程构建器 runBlocking
在我刚刚添加到 build.gradle 的协程库中丢失了。有趣的是,所有其他东西似乎都可用,GlobalScope
、CoroutineScope.launch
CoroutineScope.async
都存在。 runBlocking
不是。我做错了什么?
这是我的 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)。下面是我的示例
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
启动的,它只是触发并忘记了,测试 returns 没有抛出任何错误。如果我合并 runBlocking
,IDE 会用红色突出显示它(你知道,因为它不理解),甚至 kotlin 编译器都会大喊 unresolved reference runBlockin
。请帮助....
折腾了一番,终于知道运行只有kotlin/jvm才有Blocking。所以,它不在 kotlin/js 或 kotlin/common.
中
仅供将来参考,如果您想 运行 多平台测试,请使用此 work around
如标题所示,协程构建器 runBlocking
在我刚刚添加到 build.gradle 的协程库中丢失了。有趣的是,所有其他东西似乎都可用,GlobalScope
、CoroutineScope.launch
CoroutineScope.async
都存在。 runBlocking
不是。我做错了什么?
这是我的 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)。下面是我的示例
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
启动的,它只是触发并忘记了,测试 returns 没有抛出任何错误。如果我合并 runBlocking
,IDE 会用红色突出显示它(你知道,因为它不理解),甚至 kotlin 编译器都会大喊 unresolved reference runBlockin
。请帮助....
折腾了一番,终于知道运行只有kotlin/jvm才有Blocking。所以,它不在 kotlin/js 或 kotlin/common.
中仅供将来参考,如果您想 运行 多平台测试,请使用此 work around