How would I got about adding a desktop module to a...
# multiplatform
m
How would I got about adding a desktop module to an Android project? I am unclear whether that counts as KMM or not, or what I need to change about my project structure. All the guides I'm finding are basically "create a new project using this template", but I don't want a new project.
Maybe I should follow this: https://kotlinlang.org/docs/multiplatform-mobile-integrate-in-existing-app.html But try to substitute desktop for iOS?
k
I think you could add a new gradle module, where you apply multiplatform plugin and define targets / sourcesets. In here, you write common and platform specific code. With that, you have two options: use this module to write common Compose code (assuming you're using compose), so you use Multiplatform Compose to write the UI for Android and Desktop, or use this module as depenency in other gradle submodule, where you create JVM UI app (tornadofx?)
m
I've got it working, I had to define two new modules, one for desktop, and one shared for common code. I also had to update the existing Android module to use the multiplatform plugin.
c
for what it's worth, as someone who disdains copying and pasting massive amounts of boiler plate code I'll never understand, what I've been able to assert is: • You need to use the
multiplatform
plugin to get the ability to create a common target (which comes for free (?) i.e. you don't need to explicitly state you want common code) • You need to opt in to android, ios, js, wasm, native or desktop • You can create arbitrary other targets too, like a jvm target that could house jvm common code (imagine a jvm specific library dependency that doesn't have a common implementation, you would want to share that between desktop and android for example, but there wouldn't be a good place to store the shared code without another target) • Using the multiplatform approach seems to be the most straightforward way to achieve code sharing between desktop and android, even if you don't plan on targeting anything else. When you create a project using the wizard targeting desktop and android, you end up with 3 modules (desktop, android and common) and inside the common you have 3 targets (not sure that's the correct word). Each common target (common, desktop and android) contain target specific implementations of common defined symbols. At first I was confused why I had so many modules etc, until it clicked that the top level android and desktop modules that were created are consumers of the common module. It forces a separation of your "this is code that should work across all platforms" and "this is really specific to the app on this platform" kind of thing. Not sure if this helps at all, but it was a bit of an a-ha moment for me!
m
Yeah, it is a bit surprising at first why you need a module for every target, and then in a common module, again a source set for every target. But it actually makes sense.