Compilation error: "-Xcoroutines has no effect: coroutines are enabled anyway in 1.3 and beyond"

Compilation error: "-Xcoroutines has no effect: coroutines are enabled anyway in 1.3 and beyond"

当我尝试编译用 Kotlin 编写的 Android 应用程序时,出现以下编译错误,并且构建失败:

w: -Xcoroutines has no effect: coroutines are enabled anyway in 1.3 and beyond

我该如何解决这个问题?

事实证明,解决这个问题其实很容易,我想我应该把我的解决方案记录下来,供将来无疑会遇到同样问题的人使用。

只需从您的应用级 build.gradle 中删除以下块,在 Android Studio 中点击 "Sync Now" 并重建:

kotlin {
    experimental {
        coroutines "enable"
    }
}

使用 Kotlin 1.3 时不再需要它,因为它删除了 "experimental" 协程的性质。见 here:

Coroutines are now stable

Coroutines are a modern way to write non-blocking asynchronous code that’s easy to understand and evolve. It’s a powerful tool for anything from offloading work onto background workers to implementing complicated network protocols. The kotlinx.coroutines library hits 1.0 release and provides a solid foundation for managing asynchronous jobs at any scale including composition, cancelation, exception handling and UI-specific use cases.

您的构建脚本中可能缺少引用,例如:

kotlin {
    experimental {
        coroutines "enable"
    }
}