Skip to content

Commit ab47c93

Browse files
authored
update: build file instructions (#4765)
1 parent 0003f72 commit ab47c93

File tree

1 file changed

+56
-53
lines changed

1 file changed

+56
-53
lines changed

docs/topics/multiplatform/multiplatform-hierarchy.md

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The Kotlin Gradle plugin has a built-in default [hierarchy template](#see-the-fu
2525
It contains predefined intermediate source sets for some popular use cases.
2626
The plugin sets up those source sets automatically based on the targets specified in your project.
2727

28-
Consider the following example:
28+
Consider the following `build.gradle(.kts)` file in the project's module that contains shared code:
2929

3030
<tabs group="build-script">
3131
<tab title="Kotlin" group-key="kotlin">
@@ -67,7 +67,8 @@ from the `apple`, `native`, and `common` source sets is compiled to `watchosArm6
6767
The Kotlin Gradle plugin provides both type-safe and static accessors for all of the source sets from the default hierarchy
6868
template, so you can reference them without `by getting` or `by creating` constructs compared to the [manual configuration](#manual-configuration).
6969

70-
If you try to access the source set without declaring the corresponding target first, you'll see a warning:
70+
If you try to access the source set in the shared module's `build.gradle(.kts)` file without declaring the corresponding
71+
target first, you'll see a warning:
7172

7273
<tabs group="build-script">
7374
<tab title="Kotlin" group-key="kotlin">
@@ -147,8 +148,8 @@ To solve this issue, configure your project by doing one of the following:
147148

148149
**Case**. All of your intermediate source sets are currently covered by the default hierarchy template.
149150

150-
**Solution**. Remove all manual `dependsOn()` calls and source sets with `by creating` constructions.
151-
To check the list of all default source sets, see the [full hierarchy template](#see-the-full-hierarchy-template).
151+
**Solution**. In the shared module's `build.gradle(.kts)` file, remove all manual `dependsOn()` calls and source sets
152+
with `by creating` constructions. To check the list of all default source sets, see the [full hierarchy template](#see-the-full-hierarchy-template).
152153

153154
#### Creating additional source sets
154155

@@ -157,7 +158,7 @@ for example, one between a macOS and a JVM target.
157158

158159
**Solution**:
159160

160-
1. Reapply the template by explicitly calling `applyDefaultHierarchyTemplate()`.
161+
1. In the shared module's `build.gradle(.kts)` file, reapply the template by explicitly calling `applyDefaultHierarchyTemplate()`.
161162
2. Configure additional source sets [manually](#manual-configuration) using `dependsOn()`:
162163

163164
<tabs group="build-script">
@@ -258,61 +259,63 @@ the plugin picks the shared source sets based on the specified targets from the
258259
You can manually introduce an intermediate source in the source set structure.
259260
It will hold the shared code for several targets.
260261
261-
For example, heres what to do if you want to share code among native Linux,
262+
For example, here's what to do if you want to share code among native Linux,
262263
Windows, and macOS targets (`linuxX64`, `mingwX64`, and `macosX64`):
263264

264-
1. Add the intermediate source set `desktopMain`, which holds the shared logic for these targets.
265-
2. Specify the source set hierarchy using the `dependsOn` relation.
265+
1. In the shared module's `build.gradle(.kts)` file, add the intermediate source set `desktopMain`, which holds the shared
266+
logic for these targets.
267+
2. Using the `dependsOn` relation, set up the source set hierarchy. Connect `commonMain` with `desktopMain` and then
268+
`desktopMain` with each of the target source sets:
266269
267-
<tabs group="build-script">
268-
<tab title="Kotlin" group-key="kotlin">
269-
270-
```kotlin
271-
kotlin {
272-
linuxX64()
273-
mingwX64()
274-
macosX64()
275-
276-
sourceSets {
277-
val desktopMain by creating {
278-
dependsOn(commonMain.get())
270+
<tabs group="build-script">
271+
<tab title="Kotlin" group-key="kotlin">
272+
273+
```kotlin
274+
kotlin {
275+
linuxX64()
276+
mingwX64()
277+
macosX64()
278+
279+
sourceSets {
280+
val desktopMain by creating {
281+
dependsOn(commonMain.get())
282+
}
283+
284+
linuxX64Main.get().dependsOn(desktopMain)
285+
mingwX64Main.get().dependsOn(desktopMain)
286+
macosX64Main.get().dependsOn(desktopMain)
279287
}
280-
281-
linuxX64Main.get().dependsOn(desktopMain)
282-
mingwX64Main.get().dependsOn(desktopMain)
283-
macosX64Main.get().dependsOn(desktopMain)
284288
}
285-
}
286-
```
287-
288-
</tab>
289-
<tab title="Groovy" group-key="groovy">
290-
291-
```groovy
292-
kotlin {
293-
linuxX64()
294-
mingwX64()
295-
macosX64()
296-
297-
sourceSets {
298-
desktopMain {
299-
dependsOn(commonMain.get())
300-
}
301-
linuxX64Main {
302-
dependsOn(desktopMain)
303-
}
304-
mingwX64Main {
305-
dependsOn(desktopMain)
306-
}
307-
macosX64Main {
308-
dependsOn(desktopMain)
289+
```
290+
291+
</tab>
292+
<tab title="Groovy" group-key="groovy">
293+
294+
```groovy
295+
kotlin {
296+
linuxX64()
297+
mingwX64()
298+
macosX64()
299+
300+
sourceSets {
301+
desktopMain {
302+
dependsOn(commonMain.get())
303+
}
304+
linuxX64Main {
305+
dependsOn(desktopMain)
306+
}
307+
mingwX64Main {
308+
dependsOn(desktopMain)
309+
}
310+
macosX64Main {
311+
dependsOn(desktopMain)
312+
}
309313
}
310314
}
311-
}
312-
```
313-
314-
</tab>
315-
</tabs>
315+
```
316+
317+
</tab>
318+
</tabs>
316319
317320
The resulting hierarchical structure will look like this:
318321

0 commit comments

Comments
 (0)