Reputation: 2625
I have different flavors in a gradle build file:
I would like to put a .properties file in each app/src/flavor/ directory
app
src
flavor1/
java/
app.properties
flavor2/
java/
app.properties
and then use the references to that file in a task.
How do I reference the current flavor's directory in groovy/gradle?
Upvotes: 2
Views: 2692
Reputation: 1006674
If you really want per-flavor properties, here's an off-the-cuff approach.
Step #1: Come up with your list of flavors. For the purposes of this answer, I'll use vanilla
and chocolate
.
Step #2: Create, in your module root, a properties file per flavor (i.e., vanilla.properties
, chocolate.properties
).
Step #3: In your productFlavors
closure, you would have the code to load up that flavor's properties and use them, using something like this:
productFlavors {
vanilla {
applicationId "com.commonsware.android.gradle.hello.vanilla"
def propFile = rootProject.file('vanilla.properties')
if (propFile.canRead()) {
def Properties flavorProps = new Properties()
flavorProps.load(new FileInputStream(propFile))
// you can now access flavorProps[...] for various string keys
// identified here as ..., like flavorProps['foo']
}
}
// repeat for chocolate and other flavors
}
The only wrinkle in the above code that I can think of off the top of my head is my use of rootProject
. My sample code for all of these Gradle tricks are for a project without modules, but Android Studio projects often will be set up with your code in an app/
module (a.k.a., subproject). I have not experimented with this code yet in that scenario, and I do not know if rootProject
will refer to the module or the top-level project directory. There may be a different value to use, other than rootProject
, to get to the module.
Upvotes: 2