Exclude a class from the build in Android Studio

AFAIK, IntelliJ allows to exclude packages. Open Project Structure (Ctrl + Alt + Shift + S in Linux) → ModulesSources tab.

However, if you would like to exclude only one class, use the Gradle build file.

Android Studio uses Gradle, so in the build.gradle file, add a custom SourceSet inside the android configuration that excludes your class, e.g.:

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 19
        packageName "org.homelab.lab"
        testPackageName "org.homelab.lab.test"
    }

    sourceSets {
        main {
            java {
                exclude '**/SomeExcludedClass.java'
            }
        }
        androidTest {
            java {
                exclude '**/TestSomeExcludedClass.java'
            }
        }
    }
}

Leave a Comment