Interface KotlinTargetHierarchyDsl

    • Constructor Detail

    • Method Detail

      • default

         abstract Unit default(Function1<KotlinTargetHierarchyBuilder.Root, Unit> describeExtension)

        Set's up a 'natural'/'default' hierarchy withing KotlinTarget's in the project.

        kotlin {
            targetHierarchy.default() // <- position of this call is not relevant!
        
            iosX64()
            iosArm64()
            linuxX64()
            linuxArm64()
        }

        Will create the following SourceSets: `iosMain, iosTest, appleMain, appleTest, linuxMain, linuxTest, nativeMain, nativeTest

        Hierarchy:

                                                                                common
                                                                                   |
                                                                 +-----------------+-------------------+
                                                                 |                                     |
        
                                                               native                                 ...
        
                                                                |
                                                                |
                                                                |
                    +----------------------+--------------------+-----------------------+
                    |                      |                    |                       |
        
                  apple                  linux                mingw              androidNative
        
                    |
             +-----------+------------+------------+
             |           |            |            |
        
            macos       ios         tvos        watchos

        Let's imagine we would additionally like to share code between linux and apple (unixLike)

        kotlin {
            targetHierarchy.default { target ->
                group("native") { // <- we can re-declare already existing groups and connect children to it!
                    group("unixLike") {
                        withLinux()
                        withApple()
                    }
                }
            }
        }
        Parameters:
        describeExtension - : Additional groups can be described to extend the 'default'/'natural' hierarchy:
      • custom

         abstract Unit custom(Function1<KotlinTargetHierarchyBuilder.Root, Unit> describe)

        Allows to create a fully custom hierarchy (no defaults applied) Note: Using the custom hierarchy will also require to set the edges to 'commonMain' and 'commonTest' SourceSets by using the common group.

        Sharing code between iOS and a jvmTarget:

        targetHierarchy.custom {
            common {
                withJvm()
                group("ios") {
                    withIos()
                }
            }
        }

        Will create two SourceSetTree using the 'common' and 'ios' groups, applied on the "test" and "main" compilations: When the following targets are specified:

        • jvm()

        • iosX64()

        • iosArm64()

                               "main"                               "test"
                             commonMain                           commonTest
                                 |                                    |
                                 |                                    |
                      +----------+----------+              +----------+----------+
                      |                     |              |                     |
                    iosMain               jvmMain        iosTest               jvmTest
                      |                                    |
                 +----+-----+                         +----+-----+
                 |          |                         |          |
            iosX64Main   iosArm64Main            iosX64Test   iosArm64Test
        targetHierarchy.custom {
            common {
                group("ios") {
                    withIos()
                }
        
                group("frontend") {
                    withJvm()
                    group("ios") // <- ! We can again reference the 'ios' group
                }
        
                group("apple") {
                    withMacos()
                    group("ios") // <- ! We can again reference the 'ios' group
                }
            }
        }

        In this case, the group "ios" can be created with 'group("ios")' and later referenced with the same construction to build the tree. Applying the descriptor from the example to the following targets:

        • iosX64()

        • iosArm64()

        • macosX64()

        • jvm()

        will create the following 'main' SourceSetTree:

                                 commonMain
                                      |
                         +------------+----------+
                         |                       |
                     frontendMain            appleMain
                         |                        |
               +---------+------------+-----------+----------+
               |                      |                      |
            jvmMain                iosMain               macosX64Main
                                      |
                                      |
                                 +----+----+
                                 |         |
                           iosX64Main   iosArm64Main
      • android

         abstract Unit android(Function1<KotlinAndroidTargetHierarchyDsl, Unit> configure)

        Configure Android specific settings within the context of KotlinTargetHierarchy. The difference between Android and other targets is that the build author is free to choose the names of compilations, whereas Android is using predefined SourceSet names.

        By default, Kotlin Multiplatform will set the following default dependsOn edges:

        • androidMain ->commonMain

        • androidUnitTest ->commonTest

        In this default setup, SourceSets like androidInstrumentedTest will not dependOn commonTest. This API can be used to change the default behavior

        This can be done by putting the 'androidInstrumentedTest' variants into the 'test' SourceSetTree:

        targetHierarchy.android {
            instrumentedTest.sourceSetTree.set(SourceSetTree.test)
        }
        targetHierarchy.android {
            instrumentedTest.sourceSetTree.set(SourceSetTree.test)
            unitTest.sourceSetTree.set(SourceSetTree.unitTest) // ! <- Anything *other* than 'test'
        }