Per an Android Gradle build environment:
I currently read my Version Code and Version Name from a local version.properties file via a "readVersions" gradle task. I am in the process of adding flavors to my app. These flavors would need different versions. So I was thinking of putting two different version.properties files inside of flavor specific directories (e.g. one/version.properties, two/version.properties) next to the flavor specific "res" and "src" directories.
I have a readVersions task:
task readVersions() {
def Properties versionProps = new Properties()
def versionPropsFile = file('version.properties')
if (versionPropsFile.exists())
versionProps.load(new FileInputStream(versionPropsFile))
def v_code = (versionProps['VERSION_CODE'] ?: "0").toInteger()
def v_name = versionProps['VERSION_NAME']
// Set
versionCode v_code
versionName v_name
}
project.afterEvaluate {
preBuild.dependsOn readVersions
}
I would like to have a new readVersions task that incorporates the flavor so that I can use it in accessing the "version.properties" file within the "flavor" directory.
I have tried:
android.productFlavors.all{ flavor ->
task ("${flavor.name}_readVersions")<<{
def versionPropsFile = file(flavor.name+'/version.properties')
...
But then I don't know how to get only the active flavor's task to run during the "preBuild" step.
Conceptually I want this:
project.afterEvaluate {
preBuild.dependsOn ${active_flavor}_readVersions
}
To those who recommend reorganizing and finding simpler solutions. My build process currently has other dependencies on these version.properties files. I could just define the version code and version name in two places (e.g. inside the "flavor" as well as within a flavor's version.properties file, but I really want DRY configs)
build.gradlefile does not build your app. Yourbuild.gradlefile builds an object model of how to build your app. Builds work off of the object model. You need todependsOnfor both flavors, where appropriate flavor-specific tasks depend on your custom flavor-specific tasks. - CommonsWareproductFlavors.all, though I think that may need to bebuildVariants.all. But then you start talking about "active flavor's task" and go off the rails. In your loop, get the flavor-specific task that you want to say depends upon this newly-generated task. I do something along these lines in thisbuild.gradlefile. - CommonsWareJartask, not crafting something out of whole cloth. That's why I am posting comments, as I don't have a complete answer for you. Perhaps the solution is for you to be defining a "generic" task that handles an arbitrary flavor, then in the loop set up flavor-specific instances of that generic task and set up thedependsOn. - CommonsWare