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
source share