How to exclude a module not to build in android studio

I want to exclude a module/ Java class. so, It cannot exist in apk file. I tried the following. in app.gradle.
sourceSets {
main {
jniLibs.srcDirs = [‘libs’]
jni.srcDirs =

        java{
            exclude '**/SampleActivity.java'
        }
    }
}

Excluding is working fine, but facing import errors on the remaining classes. Please provide me a solution , what is the correct way to exclude a class or module. Thanks in Advance.

What kind of errors?
If it’s about missing classes, which were excluded, then I’m not sure I understand what you are trying to do. If you exclude classes which are required for running the app, how can the app work? So, why don’t you wasn’t some classes in your application?