'Exclude a class in gradle

I am trying to exclude a particular class from build but apparently it doesn`t work. This is how my gradle looks. Any solutions?

   android {
    sourceSets {    
            main {            
                java {
                    filter.excludes = [
                        'path/MyClass.java',
                      ]               
                }
    }
    }
}
    


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source