5

With Java, we are excluding like this:

java {
    srcDir 'src'
    exclude '**/myTests/**'
}

I want to make the same thing with Kotlin. I am trying to find some documentation on this in official documentation configuring Kotlin, but without any success.

What I've expected and already tried (and of course without any success):

kotlin {
    srcDir 'src'
    exclude '**/myTests/*.kt'
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HeyAlex
  • 1,666
  • 1
  • 13
  • 31
  • Do you have tests and productive code in the same directory? In that case you should better split your source directory following the default project layout to separate productive from test code. https://docs.gradle.org/current/userguide/organizing_gradle_projects.html – Simulant Jan 26 '19 at 16:59
  • 1
    @SimulantI have /src folder, where i keep/main(productive code) and /tests. So it seems like in this guide i have a similar structure – HeyAlex Jan 26 '19 at 17:51
  • What's the actual path for `**/myTests/**`? As @Simulant suggests, your source sets could be optimised or it might be possible, that you don't need this configuration in the end. – tynn Jan 28 '19 at 14:41
  • 1
    I just firgured out, that it's possible to optimised my configuration with moving /myTests folder out of the /src. So it would be a solution and seems like i will do the same. But i think that's a little bit strange that android-kotlin plugin doesn't contain api for excluding. Also it's seems like possible to do with Kotlin DSL: kotlin.sourceSets { main {kotlin.exclude('...')} } – HeyAlex Jan 28 '19 at 14:52
  • What file are you all talking about? File *build.gradle*? Some other file or place? Where is it located? – Peter Mortensen May 07 '22 at 22:22
  • I also met the issue. when apply plugin: 'org.jetbrains.kotlin.android', the exclude does not work. Very sad. – Victor Choy May 16 '23 at 15:52

4 Answers4

5
java {
    srcDir 'src'
    exclude '**/myTests/*.kt'
}

There isn't any Kotlin related configuration.

Why I am saying this: I have all the Kotlin files into the kotlin directory and Java files into java directory. But while configuring, I have added:

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

This means that with src/main/java, add source files from src/main/kotlin also while compiling.

This should solve your issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rahul
  • 4,699
  • 5
  • 26
  • 38
  • 1
    exclude '/myTests/*.kt' will not exclude kt files from compile[flavourName]Kotlin gradle task. Also i have a mixed folder with kotlin and java, so i don't need to define dir for kotlin package – HeyAlex Jan 28 '19 at 11:39
  • See https://issuetracker.google.com/issues/155215177. There seems to be still no way to exclude files or folders, even in AS 4.2. – Alex Cohn Oct 06 '20 at 06:33
  • Thanks this was very helpful. Can we tell how to show (kotlin) beside this package., like we have (test), (androidTest) – Arnab Kundu Mar 19 '21 at 04:56
2

Android (likely need improvement, seems flaky):

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile.class).configureEach {
    it.exclude('**/TestExcludeKotlinClass.kt')
}
DA_123
  • 337
  • 3
  • 10
  • 1
    This really help me. I also met the issue. when apply plugin: 'org.jetbrains.kotlin.android', the sourceset exclude does not work. Very sad. – Victor Choy May 16 '23 at 15:55
1

If you use a Kotlin Gradle project, try this:

tasks.withType<KotlinCompile> {
exclude("**/packageToExlude/**")}

In my case, a non-Android project.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rsobies
  • 293
  • 4
  • 16
0

I came across a way to make this work specifically for Android unit tests (but I'm assuming it's adaptable) using a combination of other solutions here:

def filesToExclude = [
    '**/*TestOne*.kt',
    '**/*TestTwo*.kt',
    ...
]
tasks.withType(org.gradle.api.tasks.SourceTask.class).configureEach {
  it.exclude(filesToExclude)
}
android.sourceSets.test.kotlin.exclude(filesToExclude)

In my particular case, the extra wildcards around the test name were needed due to other generation occurring (specifically, Dagger with kapt).

This seems to be a bit hacky way to approach it, but it works by ensuring the test target is excluded from all tasks that it could actually be excluded from (including both build & kapt tasks). The sourceSets exclusion is still necessary for the file not to be picked up for compilation (I think this is the Kotlin Gradle Plugin doing it, but it might also be the Android Gradle Plugin).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ben H
  • 31
  • 5