简体   繁体   中英

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.

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.

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)?

Yes.

You can structure your code with a multi project gradle build like this:

  • API module : Put your DTOs, searialization/deserialization and routes as constants here. Define an http client to your api here as well (Ktor has an http client as well). OpenAPI is also something you might look at for defining your http api and then just generating the client.
  • Server module : Depends on the API module to implement the routes with the DTOs defined. And if you defined a client in API, test that your server implementation satisfies it.
  • Android module : Depend on the API module. Use the client, and mock it for testing.

This is a little bit like the Dependency Inversion Principal from SOLID. You define the interface of the data exchange in a seperate module(API module). And then, both the implementation (Server Module) and the higher level code (Android Module) depend on that for its use and implementation.

I had tried to run something like this, it didn't work in my way

Main answer: Currently no.

It's impossible to share data between Ktor and Android in Kotlin Multiplatform paradigm. Let's figure it out:

  • Your sharing library will be multiplatform. It builds in special platforms pieces to support all platforms
  • Android is multiplatform and can obtain your library. When you will add your library Android KMM project will recognize the part that it needs.
  • Ktor (as a server) is using JVM or Kotlin/Native. It means that you can't just provide your KMM library and put it in Ktor. Your library structure is configured for another KMM project and can't be obtained by Ktor, it doesn't know how to handle it.

Multiplatform and Multiplatform are friends. So who is not - eliminated

Thereotically answer: Yes.

Since multiplatform library builds into parts one of them should be.jar and you can share it for Android project and Ktor project. 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. And you have to build and store your.jar separately.

Example of KMM library

There are some facts about build artifacts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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