Kotlin-dsl-samples: SourceSets - Groovy -> Kotlin DSL

Created on 26 Jul 2017  路  19Comments  路  Source: gradle/kotlin-dsl-samples

Groovy DSL:

  sourceSets {
    androidTest.java.srcDirs += "src/androidTest/kotlin"
    debug.java.srcDirs += "src/debug/kotlin"
    main.java.srcDirs += "src/main/kotlin"
    test.java.srcDirs += "src/test/kotlin"
  }

How can I convert this to convert this to Kotlin? I have not see any example how to append to existing source sets.

Most helpful comment

@JLLeitschuh Thanks.

For Java, you can use:

java.sourceSets {
    getByName("main").java.srcDirs("src/main/kotlin")
    getByName("test").java.srcDirs("src/test/kotlin")
}

The solution I posted seems to work with the Android Gradle Plugin: https://github.com/jaredsburrows/android-gif-example/blob/master/build.gradle.kts#L87.

All 19 comments

@bamboo @eskatos Do you all know how to convert this? For Android, the IDE does not recognize Kotlin source sets.

So far I have this:

  sourceSets {
    getByName("androidTest").java.srcDirs("src/androidTest/kotlin")
    getByName("debug").java.srcDirs("src/debug/kotlin")
    getByName("main").java.srcDirs("src/main/kotlin")
    getByName("test").java.srcDirs("src/test/kotlin")
  }

  println(sourceSets["androidTest"].java)
  println(sourceSets["androidTest"].java.srcDirs)
  println(sourceSets["debug"].java)
  println(sourceSets["debug"].java.srcDirs)
  println(sourceSets["main"].java)
  println(sourceSets["main"].java.srcDirs)
  println(sourceSets["test"].java)
  println(sourceSets["test"].java.srcDirs)

Which prints out:

[src/androidTest/java, src/androidTest/kotlin]
[/Users/<>/repo/android-gif-example/src/androidTest/java, /Users/<>/repo/android-gif-example/src/androidTest/kotlin]
[src/debug/java, src/debug/kotlin]
[/Users/<>/repo/android-gif-example/src/debug/java, /Users/<>/repo/android-gif-example/src/debug/kotlin]
[src/main/java, src/main/kotlin]
[/Users/<>/repo/android-gif-example/src/main/java, /Users/<>/repo/android-gif-example/src/main/kotlin]
[src/test/java, src/test/kotlin]
[/Users/<>/repo/android-gif-example/src/test/java, /Users/<>/repo/android-gif-example/src/test/kotlin]

I haven't actually run this but this code seems to look valid in IntelliJ IDEA with gradle 4.0.2:

the<JavaPluginConvention>().sourceSets {
    "main" {
        java {
            srcDirs("src/androidTest/kotlin")
        }
    }
}

@JLLeitschuh Thanks.

For Java, you can use:

java.sourceSets {
    getByName("main").java.srcDirs("src/main/kotlin")
    getByName("test").java.srcDirs("src/test/kotlin")
}

The solution I posted seems to work with the Android Gradle Plugin: https://github.com/jaredsburrows/android-gif-example/blob/master/build.gradle.kts#L87.

java.sourceSets {

should apparently just be:

sourceSets {
    getByName("main").java.srcDirs("YOUR-SRC")

at least in an example Java app project created by `gradle init'.

sourceSets {
        main {
            kotlin.srcDirs = ['src/main/kotlin', 'src-gen/main/kotlin']
        }

        test {
            kotlin.srcDirs = ['src/test/kotlin', 'src-gen/test/kotlin']
        }
    }

hello,
here i try to convert this to kotlin.

sourceSets {

        main {
            kotlin.srcDir = files("src/main/kotlin", "src-gen/main/kotlin")  

        }

**srcDirs not attach**

And i used this way.. but i also have same error on scrDirs

 sourceSets {
        getByName("main").apply {
            kotlin.srcDirs("src/main/kotlin", "src-gen/main/kotlin")
        }

Unresolved reference: srcDirs

Hi @karippery,

Please see https://docs.gradle.org/current/userguide/groovy_plugin.html#sec:changing_groovy_project_layout
It works the same way with Kotlin source sets.

e.g.:

plugins {
    kotlin("jvm") version "1.3.21"
}

sourceSets.main {
    withConvention(KotlinSourceSet::class) {
        kotlin.srcDirs("src/main/kotlin", "src-gen/main/kotlin")
    }
}

Note that this issue tracker isn't suited for support questions, please use the Gradle forums in the future https://discuss.gradle.org/

@eskatos
thank you... it's work fine now :)

 sourceSets.main {
        withConvention(KotlinSourceSet::class) {
            kotlin.srcDirs("src/main/kotlin", "src-gen/main/kotlin")
            //kotlin.srcDir = files("src/main/kotlin", "src-gen/main/kotlin")  //*********srcDirs not attach***
        }
    }
        sourceSets.test {
            withConvention(KotlinSourceSet::class)  {
            kotlin.srcDirs("src/main/kotlin", "src-gen/main/kotlin")
        }
    }

Tip: Don't forget to switch your build variant in case your don't see the source directory highlighted. And you can always run ./gradlew sourcesets to make sure the sourceset was added appropriately

@JLLeitschuh Thanks.

For Java, you can use:

java.sourceSets {
    getByName("main").java.srcDirs("src/main/kotlin")
    getByName("test").java.srcDirs("src/test/kotlin")
}

The solution I posted seems to work with the Android Gradle Plugin: https://github.com/jaredsburrows/android-gif-example/blob/master/build.gradle.kts#L87.

This method doesn't resolve on gradle wrapper 5.0 @jaredsburrows

@mochadwi , with gradlew 5.4.1, in my multiproject build I had success with:

subprojects.forEach {
    val projectSources = it.property("sourceSets") as SourceSetContainer
    sourceSets(projectSources.getByName("main"))
}

The Project docs (property, sourceSets) helped me.

Hope it helps!

Cheers

Try this if you like kotlin-idiomatic way, which utilizes delegated properties.
These functions are located in NamedDomainObjectCollectionExtensions.kt.

sourceSets {
    val main by getting
    main.java.srcDirs("src/main/kotlin")
}

For other source sets:

sourceSets {
        val main by getting
        main.java.srcDirs("src/androidMain/kotlin")
        main.manifest.srcFile("src/androidMain/AndroidManifest.xml")
        main.res.srcDirs("src/androidMain/res")
    }

I do the same but cannot see kotlin instead of java, what I am doing wrong?

I do the same but cannot see kotlin instead of java, what I am doing wrong?

@nuhkoca Rename the folder manually on Finder/Windows Explorer/... .

I do the same but cannot see kotlin instead of java, what I am doing wrong?

@nuhkoca Rename the folder manually on Finder/Windows Explorer/... .

Vaow, that's great! thanks a lot, it is working :)

If you want to ensure all of your sourceSets support a kotlin directory, you can do something like this

sourceSets {
    map { it.java.srcDir("src/${it.name}/kotlin") }
}

By using map, you ensure that if you ever add another buildType or flavor, it will still support the usage of a kotlin directory.

If you want to ensure all of your sourceSets support a kotlin directory, you can do something like this

sourceSets {
    map { it.java.srcDir("src/${it.name}/kotlin") }
}

By using map, you ensure that if you ever add another buildType or flavor, it will still support the usage of a kotlin directory.

A good approach!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xmlking picture xmlking  路  3Comments

jmfayard picture jmfayard  路  3Comments

cbeams picture cbeams  路  3Comments

jaredsburrows picture jaredsburrows  路  3Comments

iNikem picture iNikem  路  3Comments