简体   繁体   English

我可以在纯Java项目中使用由Kotlin协程构建的库吗?

[英]Can I use a library built with Kotlin coroutines in plain java project?

I am planning to build a library which uses Kotlin coroutines to make the HTTP calls to REST services using retrofit2. 我打算建立一个库,该库使用Kotlin协程使用retrofit2对REST服务进行HTTP调用。 The final output jar will include all the retrofit and Kotlin coroutines dependencies, can we use this final jar in plain java projects? 最终输出jar将包含所有改造和Kotlin协程依赖关系,我们可以在普通的Java项目中使用此最终jar吗?

Yes. 是。 You can. 您可以。 You may want to have interface adopted to java guidelines, but there is no technical restriction that prevent it. 您可能希望接口采用Java准则,但是没有技术上的限制。

Your library can internally use coroutines, but you can't expose any suspend fun s to the Java client. 您的库可以在内部使用协程,但不能向Java客户端公开任何suspend fun的功能。 It is almost impossible to call a suspend fun from Java because its signature refers to internal classes. 从Java调用suspend fun几乎是不可能的,因为它的签名指向内部类。

The recommended way to expose non-blocking Kotlin code to Java is via the CompletableFuture , using the kotlinx-coroutines-jdk8 module. 推荐的无阻塞Kotlin代码向Java公开的方法是通过CompletableFuture ,使用kotlinx-coroutines-jdk8模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM