Gradle-Plugin: Get SourceSets of Koltin Multiplatform project

Hi,

I want to write a Gradle plugin that need to access the source sets.

In a normal Kotlin-JVM project, I can get the sourceSets during task execution in the following way:

val sourceSets = project.properties["sourceSets"] as SourceSetContainer

This gives me “main” and “test” source sets.

But in a Koltin Multiplatform project, the sourceSets field is empty during task execution.

Is there another way to get the source sets of the Koltin Multiplatform plugin? Or do I need to respect some special contains?

Hi!

In Kotlin Multiplatform plugin, the sourceSets container is a part of the kotlin project extension. So you need to get the extension first:

val kotlinExtension = project.extensions.getByName("kotlin") as KotlinMultiplatformExtension
val kotlinSourceSets = kotlinExtension.sourceSets

Hi,

thanks for the hint.

But I have another problem: KotlinMultiplatformExtension cannot be resolved.

I see it here: https://github.com/JetBrains/kotlin/blob/master/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinMultiplatformExtension.kt

But I cannot find which artifact I have to add to the dependencies of my plugin project.

OK, I have found it:

compile("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61")

Now it works. Thank you!