3

I want to exclude Kotlin files within a folder from the gradle build in Android Studio. This works fine as long as the files are .java files but when converting them to Kotlin, the exclude command ignores the Kotlin files in the folder.

The following is used within my app modules build.gradle file to exclude the folder:

 sourceSets {
    main {
        java {
            exclude '**/folderToExclude'
        }
    }
}

I already checked the following link, but the suggested solution didn't work:

How to exclude kotlin files from compiling with gradle

Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51
user993441
  • 121
  • 1
  • 4
  • 15
  • The 'correct' way is to file a feature request for AGP and/or Kotlin compiler. – Alex Cohn Sep 24 '20 at 07:52
  • I don't see how this question is different from *[How to exclude kotlin files from compiling with gradle](https://stackoverflow.com/q/64016332/192373)*. *`The suggested solution didn't work`* is not a reason in itself to open a separate question. If you are not satisfied with an answer, you can vote for the question, add comments, propose a bounty or spread the word via other channels. – Alex Cohn Sep 24 '20 at 07:55
  • 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:51

3 Answers3

0

try below :

exclude '**/*.kt'

It will exclude all file with extention .kt

Samir Bhatt
  • 3,041
  • 2
  • 25
  • 39
0

Try below, it works on my machine.

android {
    ...

    sourceSets {
        main {
            java {
                exclude '**/your_file_1.kt',
                        '**/your_file_2.kt',
                        '**/your_file_3.kt',
                        '**/your_file_4.kt',
                        '**/your_file_5.kt'
            }
        }
    }

    ...
}
shizhen
  • 12,251
  • 9
  • 52
  • 88
  • 2
    Thanks for your reply. This also does not work for me. The java files I excluded throw a ClassNotFoundException when accessing them in code but this is not true for the Kotlin files – user993441 Apr 02 '19 at 09:37
0

You can exclude paths matching pattern like:

android {
    sourceSets {
        main {
            java {
                exclude '**/TestExcludeClass.java'
            }

            kotlin {
                exclude '**/TestExcludeKotlinClass.kt'
            }
        }
    }
}
Top-Master
  • 7,611
  • 5
  • 39
  • 71
Richard
  • 111
  • 1
  • 5