Nino DELCEY
Nino DELCEY

Reputation: 677

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

Upvotes: 2

Views: 1409

Answers (2)

holdbetter
holdbetter

Reputation: 21

UPD: Yes, it's

There is a nice sample-app https://github.com/joreilly/PeopleInSpace . It uses common module with data for exposing to ktor-jvm-backend-service app. It can be a little slippery to explore since it's uses a lot of gradle stuff, but it's the price you have to pay for crossplatform projects

Also, you are welcome to explore my project: https://github.com/holdbetter/PremierLeague

I'm using shared module for my backend module in KMM project. It uses a little gradle magic too, but it's way more easier to explore. Be free to ask about it

There are some docs about building artifacts: https://kotlinlang.org/docs/multiplatform-dsl-reference.html#targets

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

Upvotes: 1

Laurence
Laurence

Reputation: 1676

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.

Upvotes: 3

Related Questions