简体   繁体   English

在构建Android Studio中排除模块

[英]Exclude module on build Android Studio

I have an Android Studio project which has 3 modules in it. 我有一个包含3个模块的Android Studio项目。 A, B, C. A depends on C and B depends on C. I'm trying to speed up build times, and i realised that every time i execute assembleRelease/assembeDebug task it builds ALL modules. A,B,C。A依赖于C,B依赖于C。我试图加快构建速度,并且我意识到,每次执行assembleRelease / assembeDebug任务时,它都会构建所有模块。 Every time i build module A should only build A and C, because B has no dependency in that task, right? 每次构建模块A时,都只应构建A和C,因为B在该任务中没有依赖性,对吗? How can i avoid module B to build every time i build module A? 每次构建模块A时,如何避免构建模块B?

Module A dependencies: 模块A的依赖项:

dependencies {
    compile project(path: ':c', configuration: 'release')
    provided files('libs/some-lib.jar')
}

Module B dependencies: 模块B的依赖项:

dependencies {
    compile project(path: ':c', configuration: 'debug')
}

Module C dependencies: 模块C的依赖关系:

dependencies {
    compile files('libs/other-lib.jar')
}

I've made it clear in comments that I feel that hacking settings.gradle to remove modules is a Bad Idea. 我已在评论中明确指出,我认为hacking settings.gradle以删除模块是一个坏主意。

So, if you can't run Android Studio with two or more projects open, or you don't want to invoke Gradle on a command line, or you just want to disable some modules because they are not of interest to you, there actually is a supported way to do this. 因此,如果您无法在打开两个或多个项目的情况下运行Android Studio,或者不想在命令行上调用Gradle,或者只是想禁用某些模块,因为它们对您不感兴趣,那么实际上是支持此操作的方法。 (At least in recent releases of Android Studio like 3.2.x.) (至少在最新的Android Studio版本(如3.2.x)中。)

  1. Right-click on the top-level Project or any module in the Project view 右键单击顶级项目或“项目”视图中的任何模块
  2. Select Load/Unload Modules... 选择加载/卸载模块...
  3. Unload the modules you don't want to be active 卸载您不想激活的模块

Since this only ever changes your local project and can never be committed by accident, this is a safe way to reduce module overhead safely. 由于这只会更改您的本地项目,并且绝不会偶然提交,因此这是安全地减少模块开销的安全方法。 I think it even does a sync for you so you don't have to force a sync by hand. 我认为它甚至可以为您同步,因此您无需手动强制同步。

There is a bug in Android Studio where if you use the Gradle projects dialog to run tasks, those tasks will be run in the root context. Android Studio存在一个错误 ,如果您使用Gradle项目对话框运行任务,这些任务将在根上下文中运行。 That is, if you look carefully, even submodule tasks are run as if you ran the same task at the root level. 也就是说,如果仔细查看,即使子模块任务也像在根级别上运行同一任务一样运行。

Solutions: 解决方案:

  1. Use the command line Gradle or Gradle wrapper. 使用命令行Gradle或Gradle包装器。
  2. Always run two instances of Android Studio (ie, have multiple projects open) 始终运行两个Android Studio实例(即,打开多个项目)

Simply removing the include statement of the related module (B in this case) within the root settings.gradle file should stop Android Studio from building B as it will exclude it from the overall Gradle project. 只需在根settings.gradle文件中删除相关模块(在本例中为B)的include语句,即可阻止Android Studio构建B,因为它将从整个Gradle项目中将其排除。 The module will stay intact, however if you plan on modifying any code in it then re-adding the include statement is necessary. 该模块将保持不变,但是,如果您打算修改其中的任何代码,则必须重新添加include语句。 Also note that modifying the settings.gradle file will require a Gradle Sync for Android Studio to perform properly. 另请注意,修改settings.gradle文件将要求Gradle Sync for Android Studio正常运行。

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

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