简体   繁体   English

从 Android Studio 的构建中排除 class

[英]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.在过去的几个月里,我一直在学习 Android,并且一直使用Eclipse v4.2 (Juno) 作为我的 IDE。

I am trying to migrate to Android Studio.我正在尝试迁移到 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.在 Eclipse 中,这是一个简单的右键单击。 I can't find any reference to it in Android Studio.我在 Android Studio 中找不到任何对它的引用。

AFAIK IntelliJ allows to exclude packages. AFAIK IntelliJ允许排除包。 Open Project Structure (Ctr+Alt+Shift+S in Linux) > Modules > Sources Tab. 打开项目结构(Linux中的Ctr + Alt + Shift + S)>模块>源选项卡。

However if you would like to exclude only one class use Gradle build file. 但是,如果您只想排除一个类,请使用Gradle构建文件。

Android Studio uses Gradle so in build.gradle file add inside android configuration custom SourceSet that excludes your class eg: Android Studio使用Gradle,所以在build.gradle文件中添加android配置自定义SourceSet,排除你的类,例如:

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

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). 也许它可以在13年5月提供接受的答案,但不再是(从1.2开始)。

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

According to the labels they are targetting AS 1.5 for adding these feature. 根据标签,他们针对AS 1.5来添加这些功能。

Works fine with Android Studio v3.0 适用于Android Studio v3.0

apply plugin: 'com.android.application'

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

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. 我只是在AndroidStudioProjects文件夹中创建一个新文件夹并将它们放在那里。

The way I used to do the same is by, 我曾经这样做的方式是,

For Windows :Right click on the java file -> Show in Explorer -> change extension of file from '.java' to '.c' 对于Windows:右键单击java文件 - >在资源管理器中显示 - >将文件扩展名从“.java”更改为“.c”

For Mac :Right click on the java file -> Reveal in Finder -> change extension of file from '.java' to '.c' 对于Mac:右键单击java文件 - >在Finder中显示 - >将文件扩展名从'.java'更改为'.c'

As simple as that. 就如此容易。

Cross posting from https://stackoverflow.com/a/69261642/3689782 but it seems useful to repeat here.来自https://stackoverflow.com/a/69261642/3689782的交叉发布,但在这里重复似乎很有用。

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:我遇到了一种方法来使这项工作专门用于 Android 单元测试(但我假设它是适应性强的),使用上面链接中的其他解决方案的组合:

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).在我的特定情况下,由于发生了其他生成(特别是带有 kapt 的 Dagger),因此需要在测试名称周围使用额外的通配符。

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).这似乎是一种有点笨拙的方法,但它的工作原理是确保将测试目标排除在它实际上可以排除的所有任务(包括构建和 kapt 任务)之外。 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). 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把它固定下来)。

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 */
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM