Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set my Android app package name based on two different flavor dimensions?

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.

like image 616
Argyle Avatar asked May 15 '14 19:05

Argyle


People also ask

Does package name matter Android Studio?

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.

How do I use Android product flavors?

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.

What is Flavour dimension android?

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.

What is BuildConfig flavor?

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.


2 Answers

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

like image 125
Brian Avatar answered Sep 18 '22 17:09

Brian


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

like image 31
Gustavo Avatar answered Sep 20 '22 17:09

Gustavo