You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -17,28 +17,162 @@ In the following guide, we highlight changes related to this as well.
17
17
18
18
In this guide, we show how to restructure a combined multiplatform module into discrete modules clearly delineating
19
19
shared logic, shared UI, and individual entry points.
20
-
The example project is the default template project produced by a previous version of the KMP IDE plugin,
21
-
with the unified `composeApp` module that contains all of the shared logic and shared UI code.
22
20
23
-
Shared UI code and shared logic code are separated to demonstrate the most flexible and scalable project structure
24
-
that helps isolate UI implementation from the rest of shared code.
25
-
<!-- TODO I'm still not sure when this is beneficial, need details here. -->
26
-
If your project is simple enough, it might suffice to combine all shared code in a single module.
21
+
The example project is a Compose Multiplatform app that is the result of the [](compose-multiplatform-new-project.md)
22
+
tutorial.
23
+
You can check out the initial state of the project in the [update_october_2025 branch](https://github.com/kotlin-hands-on/get-started-with-cm/tree/update_october_2025)
24
+
of the sample project.
27
25
28
-
<!-- TODO: will people have access to the older template somewhere? Can we save it in a branch on GitHub or something so that
29
-
people could compare their own project and follow the instructions? -->
26
+
The example consists of a single Gradle module (`composeApp`) that contains all the shared code and all of the KMP entry points.
27
+
You will extract shared code and entry points into separate modules to reach two goals:
28
+
29
+
* Create a more flexible and scalable project structure that allows managing shared logic, shared UI, and different entry points
30
+
separately.
31
+
* Isolate the Android module (that uses the `androidApplication` Gradle plugin) from KMP modules (that use the `androidLibrary`
32
+
Gradle plugin).
33
+
34
+
For general modularization advice, see [Android modularization intro](https://developer.android.com/topic/modularization).
35
+
In these terms, you are going to create several **app modules**, for each platform, and shared **feature modules**, for UI and business logic.
36
+
37
+
> If your project is simple enough, it might suffice to combine all shared code (shared logic and UI) in a single module.
38
+
> We'll separate them to illustrate the modularisation pattern.
39
+
>
40
+
{style="note"}
30
41
31
42
### Create a shared logic module
32
43
33
-
You will isolate code implementing shared business logic in a `sharedLogic` module:
44
+
Before actually creating a module, you need to decide on what is business logic, which code is both UI- and platform-independent.
45
+
In this example, the only clear candidate is the `currentTimeAt()` function that returns exact time for a pair of location and time zone.
46
+
The `Country` data class, for example, relies on `DrawableResource` from Compose Multiplatform and can't be separated from UI code.
47
+
48
+
Isolate the corresponding code in a `sharedLogic` module:
34
49
35
50
1. Create the `sharedLogic` directory at the root of the project.
51
+
2. Inside that directory, create the `src` directory and an empty `build.gradle.kts` file.
52
+
3. Add the new module to `settings.gradle.kts` by adding this line at the end of the file:
53
+
54
+
```kotlin
55
+
include(":sharedLogic")
56
+
```
57
+
4. Configure the Gradle build script for the new module.
58
+
59
+
1. In `gradle/libs.versions.toml`, add the AndroidGradleLibrary plugin to your version catalog:
60
+
61
+
```text
62
+
[plugins]
63
+
androidMultiplatformLibrary = { id ="com.android.kotlin.multiplatform.library", version.ref ="agp" }
64
+
```
65
+
66
+
2. In `sharedLogic/build.gradle.kts`, specify the plugins necessary for the shared logic module:
67
+
68
+
```kotlin
69
+
plugins {
70
+
alias(libs.plugins.kotlinMultiplatform)
71
+
alias(libs.plugins.androidMultiplatformLibrary)
72
+
}
73
+
```
74
+
3. Make sure these plugins are mentioned in the **root** `build.gradle.kts` file:
0 commit comments