Reputation: 33521
According to this I can create several "flavors" in my Android Studio project. But, it looks like I can only create two trees that then will be built.
Is it possible to add variables in my build.gradle
file, that I can then access from code, so that I can have a more fine-grained control on how my app will be built?
E.g. in my build.gradle
:
productFlavors {
free {
applicationId "com.example.app.free"
showAds "yes"
}
full {
applicationId "com.example.app.full"
showAds "no"
}
}
and then in my code
String showAds = Gradle.getString("showAds");
if ("yes".equals(showAds)) {
// show ads
}
Upvotes: 0
Views: 520
Reputation: 1006799
Is it possible to add variables in my gradle.build file, that I can then access from code, so that I can have a more fine-grained control on how my app will be built?
I presume you really mean your build.gradle
file.
For your specific case, you could just use the already-existing BuildConfig.FLAVOR
, which will be free
or paid
.
Or, you can use buildConfigField
to add your own fields to BuildConfig
:
productFlavors {
free {
applicationId "com.example.app.free"
buildConfigField "boolean", "I_CAN_HAZ_ADS", 'true'
}
full {
applicationId "com.example.app.full"
buildConfigField "boolean", "I_CAN_HAZ_ADS", 'false'
}
}
Then, you would refer to BuildConfig.I_CAN_HAZ_ADS
in your Java code.
Note that I have only used BuildConfig
field for String
types, not boolean
. AFAIK, any simple type like this should work.
Upvotes: 1