Change dependency using task in gradle

I created lib so that to compile the application you need to set a special style and type of debugging / debugging. However, I am trying to do this with systemProperty when building the gradle command through.

For this, I am doing something like this:

gradlew init assembleRelease -DP="flavor1" 

In the build.gradle application, I created a task to use this system property ā€œPā€, for example:

 task init(type: JavaExec){ systemProperty "Production", System.getProperty("P") //this variable comes from command variable rootProject.ext.set("Production", systemProperties["Production"]); } 

Despite this, the following code is always executed before the init task:

 dependencies{ if(rootProject.ext.Production == "flavor1"){ releaseCompile "compile with flavor1" }else{ releaseCompile "compile with flavor2" } } 

Is there a way to change the dependencies on the init task to create an apk based on the attribute set by the system property on the command line?

Note I have an application that adds a lib dependency that has many options, and what I want to change on the fly through the command line is the dependency that needs to be added to use this library.

+2
source share
1 answer

Well, what you do is just gambling. You perform both settings of rootProject.ext.Production and consumption of rootProject.ext.Production in the configuration phase. But I do not think that there is any guarantee that is fulfilled first as you announced it. In addition, using the JavaExec task phase configuration code to set some ext properties in a project is completely meaningless anyway.

Instead

 task init(type: JavaExec){ systemProperty "Production", System.getProperty("P") //this variable comes from command variable rootProject.ext.set("Production", systemProperties["Production"]); } dependencies{ if(rootProject.ext.Production == "flavor1"){ releaseCompile "compile with flavor1" }else{ releaseCompile "compile with flavor2" } } 

just write

 dependencies{ if(System.properties.P == 'flavor1'){ releaseCompile 'compile with flavor1' }else{ releaseCompile 'compile with flavor2' } } 

or if you need this variable in the root project due to subprojects requiring a value in their assembly file

 rootProject.ext.Production = System.properties.P; dependencies{ if(rootProject.Production == 'flavor1'){ releaseCompile 'compile with flavor1' }else{ releaseCompile 'compile with flavor2' } } 

If you just need the same assembly file several times, you will also need a local variable, there is no need for an ext-property project

 def production = System.properties.P; dependencies{ if(production == 'flavor1'){ releaseCompile 'compile with flavor1' }else{ releaseCompile 'compile with flavor2' } } 

In addition, I would not use a system property, but a project property.
Just use

 dependencies{ if(production == 'flavor1'){ releaseCompile 'compile with flavor1' }else{ releaseCompile 'compile with flavor2' } } 

and name it gradlew assembleRelease -P production=flavor1

+1
source

Source: https://habr.com/ru/post/1266841/


All Articles