Beyond4D
Beyond4D

Reputation: 675

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.

Upvotes: 67

Views: 66964

Answers (7)

Gk Mohammad Emon
Gk Mohammad Emon

Reputation: 6986

For my case I need to prevent a whole folder then I did it by this -

    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
            java {
                exclude 'com/example/myfolder'
 /* The holder name I want to excludes its all classes */
            }
        }
    }

Upvotes: 0

Kirill Vashilo
Kirill Vashilo

Reputation: 1619

It works fine with Android Studio v3.0:

apply plugin: 'com.android.application'

android {
    defaultConfig {...}
    buildTypes {...}
    sourceSets {
        main {
            java {
                exclude 'com/example/full/package/path/MainActivity.java'
            }
        }
    }
}

Upvotes: 14

Lins Louis
Lins Louis

Reputation: 2643

The way I used to do the same was by,

For Windows: Right click on the Java file → Show in Explorer → change extension of the file from '.java' to '.c'.

For Mac: Right click on the Java file → Reveal in Finder → change the extension of the file from '.java' to '.c'

It is as simple as that.

Upvotes: 0

Jeffrey
Jeffrey

Reputation: 2106

Move it to a new folder.

Right-click → Show in explorer → cut and then paste to a new folder (outside of any project).

I just created a new folder inside of AndroidStudioProjects folder and placed them there.

Upvotes: -1

Tom
Tom

Reputation: 17892

It can't be done.

Maybe it could back in May 2013 when the accepted answer was provided, but not anymore (as of Android Studio 1.2).

Here is the issue: Sourceset component should be more like the Java one

According to the labels they are targetting Android Studio 1.5 for adding these feature.

Upvotes: 6

robotoaster
robotoaster

Reputation: 3132

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

Upvotes: 91

Ben H
Ben H

Reputation: 31

Cross posting from https://stackoverflow.com/a/69261642/3689782 but it seems useful to repeat here.

I came across a way to make this work specifically for Android unit tests (but I'm assuming it's adaptable) using a combination of other solutions from the link above:

def filesToExclude = [
    '**/*TestOne*.kt',
    '**/*TestTwo*.kt',
    ...
]
tasks.withType(org.gradle.api.tasks.SourceTask.class).configureEach {
  it.exclude(filesToExclude)
}
android.sourceSets.test.kotlin.exclude(filesToExclude)

In my particular case, the extra wildcards around the test name were needed due to other generation occurring (specifically, Dagger with kapt).

This seems to be a bit hacky way to approach it, but it works by ensuring the test target is excluded from all tasks that it could actually be excluded from (including both build & kapt tasks). The sourceSets exclusion is still necessary for the file not to be picked up for compilation (I think this is the Kotlin Gradle Plugin doing it, but it might also be Android Gradle Plugin--I'm not well versed enough in debugging Gradle builds to pin it down).

Upvotes: 0

Related Questions