android - Set a value based on current build/flavor settings -


i need set value in build script based on flavor building. here relevant parts of current build.gradle:

android {     // ...     productflavors {         lite {             applicationid "bbct.android"             versioncode 15             versionname "0.6.2"         }         premium {             applicationid "bbct.android.premium"             versioncode 14             versionname "0.6.2"         }     } }  testdroid {     mode 'full_run'     projectname 'bbct lite'      fullrunconfig {         instrumentationrunner = 'android.support.test.runner.androidjunitrunner'     } } 

now want set testdroid.projectname different values depending on whether build lite or premium flavor. tried following:

android {     // ...     productflavors {         lite {             applicationid "bbct.android"             versioncode 15             versionname "0.6.2"             testdroid.projectname = "bbct lite" // here         }         premium {             applicationid "bbct.android.premium"             versioncode 14             versionname "0.6.2"             testdroid.projectname = "bbct premium" // , here         }     } } 

however, unconditionally assigns testdroid.projectname "bbct premium". (i assume because last line executed sets value.) how set testdroid.projectname correct value depending on build flavor?

as explained in comment, when building buildscript both terms evaluated , latter 1 sets value.

to this, can make use of ext object every task / item has. if assign value it, can later execute right step in actual build.

the following 1 way it, , there sure better way using afterevaluate or something, works me in similar usecase.

android {     productflavors {         lite {             applicationid "bbct.android"             versioncode 15             versionname "0.6.2"             ext.projectname = "bbct lite" // here         }         premium {             applicationid "bbct.android.premium"             versioncode 14             versionname "0.6.2"             ext.projectname = "bbct premium" // , here         }     } } prebuild << {     android.applicationvariants.all { variant ->         variant.productflavors.each { flavor ->             resvalue "string", "projectname", flavor.ext.projectname             testdroid.projectname = flavor.ext.projectname         }     } }  

Comments