9

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.

Argyle
  • 3,324
  • 25
  • 44
  • 2
    Don't set the name in the manifest, it will get overwritten during the Gradle build process – Dre May 19 '14 at 19:52
  • How are the dimensions different? Why can't you use a single package name? How much space will you really be saving using that strategy? – Stephan Branczyk May 07 '15 at 23:11
  • @StephanBranczyk They're different in terms of having slightly different business rules, so a single APK is out of the question. Fortunately for me, one of those axes has since disappeared so the question is moot. Although I'd still be curious about an answer. – Argyle May 07 '15 at 23:43

3 Answers3

3

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

Brian
  • 4,328
  • 13
  • 58
  • 103
3

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

Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71
  • If I weren't stuck with some legacy names that make this insufficient for my needs, I'd be all aboard. – Argyle Dec 15 '15 at 23:50
1

You will want to set the applicationId, not the packageName:

    productFlavors {
        boring.flavorDimension "foo"
        charm {
            flavorDimension "foo"
            applicationId "com.example.charm"
        }
        strange {
            flavorDimension "bar"
            applicationId "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"
        }
    }
Kio Krofovitch
  • 2,954
  • 1
  • 21
  • 30
  • 1
    Since I wrote the question, the name of that variable has changed. The fundamental nature of the question, however, has not. – Argyle May 07 '15 at 23:45