[英]Is it possible to use Ktor and / or Kotlin multiplatform to "share" the exposed data between a Rest API and an Android Application?
The main idea is to have a sort of "serialization / deserialization module" that could be shared between a backend (using Ktor) and an Android application.主要思想是拥有一种可以在后端(使用 Ktor)和 Android 应用程序之间共享的“序列化/反序列化模块”。
I don't know how "far" this can go, but I would imagine that the routes are "exposed" for the frontend app to consume (like Retrofit does with its interface with annotations) and the (de-)serialization logic is shared too.我不知道 go 能“走多远”,但我想路由是“公开”供前端应用程序使用的(就像 Retrofit 对其带注释的接口所做的那样)并且(反)序列化逻辑是共享的也。
So basicaly, is it possible to "share" not only the "POJO / POKO" representations of API responses, but also the possible endpoints with the expected parameters, between a server (I guess using Ktor) and an Android application (Java / Kotlin)?所以基本上,是否可以在服务器(我想使用 Ktor)和 Android 应用程序(Java / Kotlin)之间“共享”不仅是 API 响应的“POJO / POKO”表示,还可以“共享”具有预期参数的可能端点)?
Yes. 是。
You can structure your code with a multi project gradle build like this: 您可以使用如下所示的多项目gradle构建来构建代码:
This is a little bit like the Dependency Inversion Principal
from SOLID. 这有点像SOLID的
Dependency Inversion Principal
。 You define the interface of the data exchange in a seperate module(API module). 您可以在单独的模块(API模块)中定义数据交换的接口。 And then, both the implementation (Server Module) and the higher level code (Android Module) depend on that for its use and implementation.
然后,实现(服务器模块)和更高级别的代码(Android模块)依赖于它的使用和实现。
I had tried to run something like this, it didn't work in my way我试过运行这样的东西,但它没有按我的方式工作
It's impossible to share data between Ktor and Android in Kotlin Multiplatform paradigm.在 Kotlin Multiplatform 范式中,Ktor 和 Android 之间不可能共享数据。 Let's figure it out:
让我们弄清楚:
Multiplatform and Multiplatform are friends.多平台和多平台是朋友。 So who is not - eliminated
那么谁不-淘汰
Since multiplatform library builds into parts one of them should be.jar and you can share it for Android project and Ktor project.由于多平台库构建成部分,其中之一应该是 .jar,您可以将其共享给 Android 项目和 Ktor 项目。 I don't know where you can find them, but i think it's possible.
我不知道你在哪里可以找到它们,但我认为这是可能的。
In my case it's not appropriate because it's not cross-platform way.就我而言,这是不合适的,因为它不是跨平台的方式。 You can't have iOS, macOS in this scenario.
在这种情况下,您不能拥有 iOS,macOS。 And you have to build and store your.jar separately.
你必须单独构建和存储 your.jar。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.