Exclude a class from the build in Android Studio
Asked Answered
S

7

66

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.

Solomonsolon answered 22/5, 2013 at 20:43 Comment(2)
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?Kilderkin
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.Solomonsolon
B
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'
            }
        }
    }
}
Ben answered 23/5, 2013 at 8:58 Comment(10)
Excellent. Thank you. I'm sure future releases will have this available from the right-click menu?Solomonsolon
Can we exclude files in build types? If its possible, can we do it the same way as its done in sourceSets?Caesar
Answer is outdated. first part didn't work for me. second part probably wontMelanie
This doesn't work because android plugin source sets are not behaving the same as JavaSourceSetsGossett
If you want to remove .class files then follow my answer - #32008992Quisling
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.Bite
Does this work specific to build types (release only and not debug)?Flutist
It doesn't work. Any updates? What changed since this answer was posted?Sanmiguel
Any updates? Running into this issue as well!Marnie
The gradle option still works fine for me.Anonym
Z
14

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'
            }
        }
    }
}
Zenobiazeolite answered 3/3, 2018 at 11:28 Comment(0)
D
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.

Divalent answered 25/4, 2015 at 22:5 Comment(5)
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.Pagano
@BrianWhite Thanks for correction. Glad to hear that this is supported.Divalent
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.Pagano
I was all excited when I saw 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 !Virgy
The gradle option in the accepted answer still works.Anonym
M
0

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.

Mou answered 15/7, 2018 at 3:18 Comment(2)
This is the ugliest solution I can imagine for this problem and yet exactly what I finally did. :-(Skulduggery
If you move the file then all connected classes and resources will be disconnected and that'll create another problem. Not good.Loehr
F
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.

Frizz answered 15/1, 2019 at 8:59 Comment(0)
K
0

Cross posting from https://mcmap.net/q/76346/-how-to-exclude-kotlin-files-from-compiling-with-gradle 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).

Kanazawa answered 20/9, 2021 at 22:59 Comment(0)
M
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 */
            }
        }
    }
Morra answered 3/8, 2022 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.