Hi Folks, I am new to Kotlin Multiplatform and had...
# multiplatform
n
Hi Folks, I am new to Kotlin Multiplatform and had some queries. Answers would be helpful 🙌 1. When creating KMM project via Android Studio wizard, if I select "Share UI", then I am getting project structure which has
commonMain
,
androidMain
,
iosMain
folders in both
:shared
and
:composeApp
modules. Shouldn't these be only in
:shared
module? This does not happen when I am not sharing UI. 2. When generating project without sharing UI, the
org.jetbrains.kotlin.multiplatform
plugin is applied in the
build.gradle.kts
of
:composeApp
as well. Is it required? I can see only
androidMain
folder in the
:composeApp
module. Why do we need KMP plugin for android module when not sharing UI?
z
As far as I know, the first thing only happens if you choose a server application (if it's just Android + iOS and it's with shared UI, there would be no separate
shared
module created). The readme describes the purpose of
composeApp
and `shared`:
```*
/composeApp
is for code that will be shared across your Compose Multiplatform applications.
It contains several subfolders:
-
commonMain
is for code that's common for all targets.
- Other folders are for Kotlin code that will be compiled for only the platform indicated in the folder name.
For example, if you want to use Apple's CoreCrypto for the iOS part of your Kotlin app,
iosMain
would be the right folder for such calls.
*
/shared
is for the code that will be shared between all targets in the project.
The most important subfolder is
commonMain
. If preferred, you can add code to the platform-specific folders here too.```
Whatever you put in
shared
will be shared by all platforms, which includes server. There is logic or shared implementation you might want to put there so it can run on both clients and server.
composeApp
on the other hand will be used by whatever targets of your project use Compose Multiplatform (could be Android, iOS, Desktop, Web). You don't want to write this code in the
shared
module because it makes no sense for the server project to depend on it.
For the second question, yes, you could set up your Android Compose app in that scenario as a regular Android project with just Jetpack Compose, if you wanted to. The reason we don't generate that is that having the multiplatform setup in place allows you to more easily add platforms later, if you want to for example try Compose Hot Reload with desktop, or experiment with the web target.
n
Makes sense! For the 1st one, yes I have created project with server application as well. Curious, since when adding server we are getting
:shared
module as well, how is the
.framework
file created with commonMain in both
:composeApp
and
:shared
? Is it a single
.framework
file with code from both or separate ones? For 2nd one, got it. That helps.
z
Yes, for iOS, these two modules will be built into a single framework. You can actually do this with any number of modules if you have your code organized by features for example. Only the "top" one that depends on the rest outputs a framework. Covered as part of this page, here: https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-project-configuration.html#several-shared-modules
n
Got it, this helps. Thanks @zsmb!
👍 1
o
I will really suggest people use this instead. I makes it easier to creating CMP project. https://terrakok.github.io/Compose-Multiplatform-Wizard
z
Easier as in you can add extra libraries, or is it something else about it?
o
You can add extra libraries and it gives other good recommendations for other libraries.
👍 1