Hi all, right now I have a kmm mobile app and a kt...
# multiplatform
j
Hi all, right now I have a kmm mobile app and a ktor server project in separate repositories that I want to combine, but when I put ktor in a jvmMain source set I get
The 'java' plugin has been applied, but it is not compatible with the Android plugins.
. Is there a better way to have a single client/server app in kmm mobile?
v
I'm not sure about the structure you're using, is it something like below?
Copy code
-projectRoot
|-shared
  |-commonMain
  |-jvmMain
  |-androidMain
  |-iOSMain
|-server
|-androidApp
|-iOSApp
shared
would have all the code that can be shared across platforms.
server
is a JVM / Ktor server module just like the one you had in a different repository. It depends on
shared
to get shared code
androidApp
is a pure Android app, with
shared
as a dependency
iOSApp
is a pure iOS app, with
shared
as a dependency
m
I have it here a KMP repo that has
android
ios
and
backend
the module
common
is used all 3 targets while module
client
is only for mobile you can find example of ktor usage in
common
module: https://github.com/Oztechan/CCC
j
@Vitor Hugo Schwaab Thanks for the detailed reply. Yes, my project structure is similar, except my server code is its own standalone repository. But it seems like I'll be able to move it into my kmp repository and have some dependencies in 'shared'?
v
@Justin Xu you should be able to. The project sent by @Mustafa Ozhan is a nice example. It's not recommended to use the same module as a shared-code library and server executable at the same time 😅 It's probably not possible either and I'd expect an error similar to what you shared in the beginning. So, if you want the shared code, the apps, and the server in the same Gradle project: • a subproject (module) dedicated to the writing of common code. • a subproject (module) for each executable (one for server, one for Android, one for iOS, etc.)
j
I see. The only thing I would share between server module and shared module is maybe the data models. This would be a design question, but should data model classes be visible to my application? Or should my application just directly deal with JSON response from api
v
I've done something similar here. If you see the
shared
module, the only thing it contains are the serialization models, so both Server and Clients can use it. The
server
module defines how the API works, and it uses uses the models in
shared
to serialize results. The
shared-clients
module consumes the API and uses the models in
shared
to parse what the server responds. android and JS clients use the
shared-clients
to have the same logic when it comes to consuming the API