Android Studio Exclude Class from build?

2019-01-04 11:55发布

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

I am trying to migrate to Android-Studio and wonder how I can "exclude from build path" some of the classes I have yet to complete?

In Eclipse, it was straight forward right click. I can't find any reference to it in Studio.

4条回答
叼着烟拽天下
2楼-- · 2019-01-04 12:04

Move it to a new folder. Rightclick -> Show in explorer -> cut then paste to a new folder (outside of any project). I just create a new folder inside of AndroidStudioProjects folder and placed them there.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-04 12:15

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'
            }
        }
    }
}
查看更多
家丑人穷心不美
4楼-- · 2019-01-04 12:18

It can't be done.

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

Here is the issue:
https://code.google.com/p/android/issues/detail?id=64957

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

查看更多
Explosion°爆炸
5楼-- · 2019-01-04 12:19

AFAIK IntelliJ allows to exclude packages. Open Project Structure (Ctr+Alt+Shift+S in Linux) > Modules > Sources Tab.

However if you would like to exclude only one class use Gradle build file.

Android Studio uses Gradle so in build.gradle file add inside android configuration custom SourceSet 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'
        }
     }
  }
 }
查看更多
登录 后发表回答