Question

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.

Was it helpful?

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

OTHER TIPS

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

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.

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.

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).

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top