65

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Beyond4D
  • 655
  • 1
  • 5
  • 7
  • 1
    If you're learning, why are you trying to migrate to an unreleased, preview product which needs experience to use successfully and to work around the various bugs in it? – Simon May 22 '13 at 21:51
  • 1
    Good question Simon. I see the writing on the wall. Besides, between the documentation and StackOverflow, I've been able to build an app applying best practices. – Beyond4D May 24 '13 at 02:10

7 Answers7

89

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'
            }
        }
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
robotoaster
  • 3,082
  • 1
  • 24
  • 23
  • Excellent. Thank you. I'm sure future releases will have this available from the right-click menu? – Beyond4D May 24 '13 at 02:11
  • 1
    Can we exclude files in build types? If its possible, can we do it the same way as its done in sourceSets? – Ahmed Sep 29 '14 at 20:13
  • 4
    Answer is outdated. first part didn't work for me. second part probably wont – Vedant Agarwala Dec 20 '14 at 07:52
  • This doesn't work because android plugin source sets are not behaving the same as JavaSourceSets – Igor Čordaš Apr 24 '15 at 15:19
  • If you want to remove .class files then follow my answer - https://stackoverflow.com/questions/32008492/android-studio-exclude-class-or-file-from-jar/44716007#44716007 – Ajit Jun 23 '17 at 07:56
  • 2
    This DOES work currently, using AndroidStudio 3.1. The nice feature is that the java classes are ignored by the build, but you can still see errors listed by the inspections, etc. Very useful when refactoring code and you want to keep the old code around for reference even after it becomes incompatible with dependencies as you change code. – Brendon Whateley Nov 29 '17 at 20:11
  • Does this work specific to build types (release only and not debug)? – Arka Prava Basu May 15 '19 at 21:17
  • 1
    It doesn't work. Any updates? What changed since this answer was posted? – SirKnigget Sep 22 '20 at 19:32
  • 1
    Any updates? Running into this issue as well! – lawonga Apr 11 '21 at 19:32
  • The gradle option still works fine for me. – Mick O'Hea Mar 02 '23 at 14:51
13

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'
            }
        }
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kirill Vashilo
  • 1,559
  • 1
  • 18
  • 27
6

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tom
  • 17,103
  • 8
  • 67
  • 75
  • That link says, "The java/resources components of the sourcesets allow for include/exclude patterns. We should do this for aidl/rs/jni/assets." It sounds like they expect "java" excludes are currently working. – Brian White May 26 '15 at 00:32
  • @BrianWhite Thanks for correction. Glad to hear that this is supported. – Tom May 26 '15 at 23:57
  • No, it's not supported. At least, it's not working for me. It's just that the issue explicitly mentions aidl/rs/jni/assets but not java. – Brian White May 27 '15 at 01:21
  • 3
    I was all excited when I saw https://www.jetbrains.com/idea/help/excluding-files-from-project.html and then I became aware of a tiny little NOTE `This action is not applicable to Java files and binaries.` bah ! – Someone Somewhere Nov 18 '15 at 13:44
  • The gradle option in the accepted answer still works. – Mick O'Hea Mar 02 '23 at 14:51
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lins Louis
  • 2,393
  • 2
  • 25
  • 30
0

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

Ben H
  • 31
  • 5
0

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 */
            }
        }
    }
Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42
-1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jeffrey
  • 1,998
  • 1
  • 25
  • 22
  • 7
    This is the ugliest solution I can imagine for this problem and yet exactly what I finally did. :-( – Bowi Jul 30 '19 at 08:15
  • If you move the file then all connected classes and resources will be disconnected and that'll create another problem. Not good. – Tuhin Subhra Jul 09 '20 at 12:44