有条件地包括gradle构建项目

时间:2015-05-21 21:04:27

标签: android build gradle

场景:我们有一个Android应用程序,其中包含一些不同的可选组件,我们希望能够根据客户需求和许可来包含/排除这些组件。是否可以根据构建参数包含特定项目,而不创建所有排列作为构建风格

./gradlew assembleRelease -PincludeFeatureA=true -PincludeFeatureB=false

我以为我可以在依赖项中做这样的事情:

dependencies {
  if(includeFeatureA){
    compile project(':featureAEnabled')    
  } else {
    compile project(':featureADisabled')
  }
}

但这似乎不起作用。

更新:考虑到可切换功能的数量,对每个排列使用显式构建变体是很麻烦的。

例如,给定3个可切换功能,我不想构建这样的风格:

Feature1
Feature1-Feature2
Feature1-Feature3
Feature1-Feature2-Feature3
Feature2
Feature2-Feature3
...

3 个答案:

答案 0 :(得分:6)

我的方案的解决方案是将if语句移出依赖项:

假设命令行:

gradlew assembleRelease -PincludeFeatureA

在项目build.gradle的开头,我包含了这个:

def featureA_Proj=':featureA_NotIncluded'

然后我有这样的任务:

task customizeFeatureA(){
    if(project.hasProperty('includeFeatureA')){
        println 'Including Feature A'
        featureA_Proj=':featureA'
    }
}

最后,在依赖项下,我只包括:

dependencies{
  include(featureA_Proj)
}

答案 1 :(得分:2)

使用Build Variants。您可以根据它们启用或禁用项目的依赖项您甚至可以使用单独的资产或源代码。

答案 2 :(得分:1)

查看settings.gradle文件,它可用于指示要构建的所有项目,在这里您可以阅读设置并使用它们。

请参阅
https://docs.gradle.org/current/userguide/build_lifecycle.html https://docs.gradle.org/current/userguide/multi_project_builds.html

这可能会有所帮助。

相关问题