I'm building my Android app with the Gradle plugin. I'm using the flavors feature to create four different variants. At the moment, it's a single dimension with four values, but it would be more logical to set it up as two dimensions with two values each. I've got that building, but this is where I run into trouble.
I need each of the four variants to have a different package name. The first three are easy since I have a default and each dimension can override the package, but I need to set a package name for when both non-default dimensions are in play.
flavorDimensions "foo", "bar"
productFlavors {
boring.flavorDimension "foo"
charm {
flavorDimension "foo"
packageName "com.example.charm"
}
strange {
flavorDimension "bar"
packageName "com.example.strange"
}
// This is the idea of what I want, but it doesn't work because there
// must be only a single dimension specified here.
charmStrange {
flavorDimension "foo", "bar"
packageName "com.example.charminglystrange"
}
}
I tried setting it after declaration by looking up the composed flavor variant, but I didn't have much luck. I'm not very familiar with Gradle, so I'm sure there's trickery I haven't employed. Alternately, maybe I could specify the package name in src/charmStrange/AndroidManifest.xml
and let the merge sort it out? That seems like it could cause problems in the future.
It shouldn't matter. In case you still wish to change, you have to unpublish your app and publish it as a new app. This is because Google identifies your app through you bundle ID, or package name as it is called. So you cannot repeat 2 package names.
Creating product flavors is similar to creating build types: add them to the productFlavors block in your build configuration and include the settings you want. The product flavors support the same properties as defaultConfig —this is because defaultConfig actually belongs to the ProductFlavor class.
Flavor Dimensions is a way to group flavors by a name. For now, we're using just a single group. Add the following line in your defaultConfig block: flavorDimensions "default" Now syncing the gradle would give you the following product flavors: Android Build Variants combine build types and product flavors.
BuildConfig.FLAVOR gives you combined product flavor. So if you have only one flavor dimension: productFlavors { normal { } admin { } } Then you can just check it: if (BuildConfig. FLAVOR.
I had a similar question this answer isn't tested in my case I was appending the product flavor to the app id set by the other dimension, using your example it would append .strange to package com.example.charm
android.applicationVariants.all { variant ->
def flavorString = variant.getVariantData().getVariantConfiguration().getFlavorName()
def mergedFlavour = variant.getVariantData().getVariantConfiguration().getMergedFlavor();
def appId = variant.getVariantData().getVariantConfiguration().getApplicationId();
if(flavorString.contains("Strange")) {
mergedFlavour.setApplicationId(appId + ".strange")
}
}
but in your case you want a full package name so something like
android.applicationVariants.all { variant ->
def flavorString = variant.getVariantData().getVariantConfiguration().getFlavorName()
def mergedFlavour = variant.getVariantData().getVariantConfiguration().getMergedFlavor();
def appId = variant.getVariantData().getVariantConfiguration().getApplicationId();
if(flavorString.contains("charmStrange")) {
mergedFlavour.setApplicationId("com.example.charminglystrange)
}
}
charmStrange might need to be CharmStrange, try playing with it
You can now set applicationIdSuffix for productFlavors:
android {
productFlavors {
free {
applicationIdSuffix = ".free"
}
prod {
}
}
}
Source: http://android-developers.blogspot.ie/2015/12/leveraging-product-flavors-in-android.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With