I'm building an app wit different build variant flavors. The flavors are "Free" and "Paid". I want to create some logic on my java classes which should only get triggered if the app is "Paid". Therefore I need a way to get the "applicationId" set during the gradle build process as shown below:
gradle.build
productFlavors {
free {
applicationId "com.example.free"
resValue "string", "app_name", "Free App"
versionName "1.0-free"
}
paid {
applicationId "com.example.paid"
resValue "string", "app_name", "Paid App"
versionName "1.0-paid"
}
Once I have the application ID I could do something like this:
if(whateverpackageid.equals("paid")) {
// Do something or trigger some premium functionality.
}
Am I correct to say that during the gradle build process the "applicationId" eventually becomes the "package name" once the app has been compiled? If so what is the best way to get either the "application ID" or "package name" so that I can implement some flavor dependent logic in my java files?
I found best solution to get all values like APPLICATION_ID, BUILD_TYPE, FLAVOR, VERSION_CODE and VERSION_NAME.
Just write : Log.d("Application Id : ",BuildConfig.APPLICATION_ID); in your code. It will provide APPLICATION_ID of your flavor.
BuildConfig.java
For more details you can refer this link : http://blog.brainattica.com/how-to-work-with-flavours-on-android/
I would use build configuration variables in your product flavors. Something along the lines of:
Then after a build you can use:
You may have to do a sync/build on gradle after you add the attribute before you can compile and import the BuildConfig class that Gradle generates on your behalf.