Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: how to keep kotlin and java in the same source folder?

I would like to have kotlin and java files in the same folder like:

src/main/xxx/JavaClass.java
src/main/xxx/KotlinClass.kt

src/test/xxx/JavaTestClass.java
src/test/xxx/KotlinTestClass.kt

i don't care if xxx is kotlin, java, whatever. i just want to have all the files providing a single functionality in one place with working cross-references / cross-compilation.

how can i configure it in gradle?

like image 977
piotrek Avatar asked Oct 27 '22 22:10

piotrek


2 Answers

this should work

sourceSets {
    main.java.srcDirs = ['src/main/xxx']
    main.kotlin.srcDirs = ['src/main/xxx']
    test.java.srcDirs = ['src/test/xxx']
    test.kotlin.srcDirs = ['src/test/xxx']
}
like image 75
birneee Avatar answered Oct 31 '22 09:10

birneee


= is the wrong operator; += can be used to extend the class-path.

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

referencing a separate module is still more solid than within the same module:

a) because some Gradle DSL is Java or Kotlin specific.

b) the test-runners do not care about other technologies.

adding more complexity (a library module) barely solves a problem; but in this case, it circumvents it - because it permits another one build.gradle & test-runner. which otherwise would not be possible.

like image 34
Martin Zeitler Avatar answered Oct 31 '22 10:10

Martin Zeitler