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)