Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally include project in gradle build

Scenario: We have an Android app with a few different optional components that we would like to be able to include/exclude depending on customer needs and licensing. Is it possible to include specific projects based on a build parameter and without creating all permutations as build flavors?

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

I thought I could do something like this in dependencies:

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

But that doesn't seem to work.

Update: Considering the number of toggle-able features, using explicit build variants for every permutation is cumbersome.

For example, given 3 toggle-able features, I do not want to have to build flavors like this:

Feature1
Feature1-Feature2
Feature1-Feature3
Feature1-Feature2-Feature3
Feature2
Feature2-Feature3
...
like image 846
Ken Avatar asked May 21 '15 21:05

Ken


Video Answer


1 Answers

The solution for my scenario was to move the if statement out of the dependencies:

Assuming the command line:

gradlew assembleRelease -PincludeFeatureA

At the beginning of the project build.gradle, I include this:

def featureA_Proj=':featureA_NotIncluded'

Then I have a task like this:

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

Finally, under dependencies, I just include:

dependencies{
  include(featureA_Proj)
}
like image 198
Ken Avatar answered Sep 30 '22 09:09

Ken