title: Android Studio: product flavor combination with more than two flavor dimensions (flavor groups)
body: <p>I am developing an Android application using Android Studio (v 2.1, gradle plugin v 2.1.0). My application has various versions which share a lot of common code so I decided to use flavor dimensions and product flavors to customize code and resources when and where it is requested. This worked fined as long as I only had two flavor dimensions. As an example, my <code>app.gradle</code> was</p>
  
  <pre><code>…
  flavorDimensions "fruit", "color"
  
  productFlavors {
  
      apple {
          dimension "fruit"
      }
      pear {
          dimension "fruit"
      }
  
      red {
          dimension "color"
      }
      yellow {
          dimension "color"
      }
  }
  …
  </code></pre>
  
  <p>and my <code>src</code> folder was</p>
  
  <pre><code>src/
      appleRed/
      appleYellow/
      pearRed/
      pearYellow/
  </code></pre>
  
  <p>each one with a custom version of my code. Again, as an example</p>
  
  <pre><code>src/
      appleRed/java/com/example/ExampleFragment.java
      appleYellow/java/com/example/ExampleFragment.java
      pearRed/java/com/example/ExampleFragment.java
      pearYellow/java/com/example/ExampleFragment.java
  </code></pre>
  
  <p>of course, there is no instance of <code>ExampleFragment</code> in <code>src/main</code>.</p>
  
  <p>At some point during development, I had to include a <em>free</em> and a <em>paid</em> version of the app. I thought that it could be easily achieved by adding a new flavor dimension named <code>version</code> and two product flavors named <code>free</code> and <code>paid</code>:</p>
  
  <pre><code> …
  flavorDimensions "fruit", "color”, “version”
  
  productFlavors {
  
      apple {
          dimension "fruit"
      }
      pear {
          dimension "fruit"
      }
  
      red {
          dimension "color"
      }
      yellow {
          dimension "color"
      }
  
      free {
          dimension "version"
      }
      paid {
          dimension “version”
      }
  }
  …
  </code></pre>
  
  <p>but all of a sudden the custom code generated by the combination of <code>fruit</code> and <code>color</code> was not detected by Android Studio anymore. So no <code>appleRed</code>, <code>appleYellow</code>, <code>pearRed</code> nor <code>pearYellow</code> can be used to have custom code and the only way I was able to regain my configuration was to use all the combinations of all the three flavour dimensions:</p>
  
  <pre><code>  src/
        appleRedFree/java/com/example/ExampleFragment.java
        appleRedPaid/java/com/example/ExampleFragment.java
        appleYellowFree/java/com/example/ExampleFragment.java
        appleYellowPaid/java/com/example/ExampleFragment.java
        pearRedFree/java/com/example/ExampleFragment.java
        pearRedPaid/java/com/example/ExampleFragment.java
        pearYellowFree/java/com/example/ExampleFragment.java
        pearYellowPaid/java/com/example/ExampleFragment.java
  </code></pre>
  
  <p>This is not good because <code>ExampleFragment</code> is duplicated across the same <code>fruitColor*</code> combination (<code>appleRedFree</code>, <code>appleRedPaid</code> have the same <code>ExampleFragment</code>). Same problem happens for resources (the ones in <code>res</code> folder).</p>
  
  <p>My questions are: </p>
  
  <p>1) Is this the expected behaviour from gradle in Android Studio (<em>i.e.</em>, not being able to combine a subset of product flavors, following their priority based on their dimension, when having more than two flavour dimensions)?</p>
  
  <p>2) Given the fact that this is the expected behaviour, is there another way I can achieve my customisation without duplicated code or without having a single file with an <em>if-statement</em> inside (<em>e.g.</em>, <code>if (BuildConfig.FLAVOR_version == "free") ...</code>) ? </p>
  
  <p>Please note that I’m talking about having custom code which could be complex, so I’m not asking for basic customisation like a build config variable, variant filtering, or something like that.</p>