androidandroid-studioidebuildpath

Exclude a class from the build in Android Studio


I've been learning Android over the past few months and have been using Eclipse v4.2 (Juno) as my IDE.

I am trying to migrate to Android Studio. How can I exclude some of the classes from build path I have yet to complete?

In Eclipse, it was a straightforward right click. I can't find any reference to it in Android Studio.


Solution

  • 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'
                }
            }
        }
    }