Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

752
Views
How to exclude Kotlin files from compiling with Gradle

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'
}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

Android (likely need improvement, seems flaky):

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile.class).configureEach {
    it.exclude('**/TestExcludeKotlinClass.kt')
}
over 4 years ago · Santiago Trujillo Report

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).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!