Conditionally enable a project in gradle build

Scenario: we have an Android application with several different additional components that we would like to include / exclude depending on customer needs and licensing. Is it possible to include specific projects based on the assembly parameter and without creating all permutations as assembly attributes ?

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

I thought I could do something like this in the dependencies:

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

But that does not work.

Update. Given the number of switchable functions, using explicit build options for each permutation is cumbersome.

For example, given 3 functions with the ability to switch, I donโ€™t want to create options like this:

 Feature1 Feature1-Feature2 Feature1-Feature3 Feature1-Feature2-Feature3 Feature2 Feature2-Feature3 ... 
+5
source share
3 answers

The solution for my scenario was to move the if from the dependencies:

Assume on the command line:

 gradlew assembleRelease -PincludeFeatureA 

At the start of the build.gradle project, I include the following:

 def featureA_Proj=':featureA_NotIncluded' 

Then I have this task:

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

Finally, in the dependencies, I just include:

 dependencies{ include(featureA_Proj) } 
+5
source

Use Build Options . You can enable or disable dependencies on projects based on them. You can use individual resources or source code with them.

+2
source

Check the settings.gradle file, it can be used to indicate what all projects need to be built, here you can read the settings and use them.

Cm
https://docs.gradle.org/current/userguide/build_lifecycle.html https://docs.gradle.org/current/userguide/multi_project_builds.html

This can help.

+1
source

All Articles