title: How to have two build flavors inherit from a root flavor in Android Studio?
body: <p>I used to have the following project flavors:</p>
  
  <ol>
  <li>Apple</li>
  <li>Orange</li>
  </ol>
  
  <p>Originally the only difference was the <code>applicationId/packageName</code>. Now there is a single java file that is different. A custom ArrayAdapter to be exact. The solution was to create <code>src/Apple</code> and <code>src/Orange</code> and both inherit from <code>src/main</code>. I removed the java file from <code>src/main</code> and put a copy into <code>src/Apple</code> and <code>src/Orange</code> and modified it appropriately. All was good in the world. </p>
  
  <p>Fast forward a few weeks, now there are about 10 java files that differ between Apple and Orange. Again... no big deal. Easy to handle. Separate java files in <code>src/Apple</code> and <code>src/Orange</code>.</p>
  
  <p>Fast forward to today. I need to modify things up a bit, because I want to have a free and premium version of each. The free and premium versions only differ by a URL. I was going to simply create the new types called:</p>
  
  <ol>
  <li>AppleFree</li>
  <li>ApplePremium</li>
  <li>OrangeFree</li>
  <li>OrangePremium</li>
  </ol>
  
  <p>I have a dilema though. Since now <code>src/Apple</code> and <code>src/Orange</code> have 10 different files that have been changed... if I change any java file in <code>AppleFree</code> I have to make sure I do the same in <code>ApplePremium</code>. I'm kind of at a crossroads and hope my question makes sense at this point. I have come up with three possible solutions, but I'm not sure how I would implement them/what would be the correct approach/the solution is not what I want.</p>
  
  <h2>Solution 1:</h2>
  
  <p>Use an if statement
  <code>if (BuildConfig.FLAVOR==appleFree) {//use free Url} else {// use premium url}</code></p>
  
  <p><strong>Issue:</strong> Both Urls are technically compiled into the apk. I do not want this.</p>
  
  <h2>Solution 2:</h2>
  
  <p>Have src/AppleFree and src/ApplePremium inherit from an src/Apple parent directory somehow.</p>
  
  <p><strong>Issue:</strong> Not sure how I would do this.</p>
  
  <h2>Solution 3:</h2>
  
  <p>Add the free and premium url right in build.gradle like so?</p>
  
  <pre><code>    productFlavors {
              appleFree {
                  applicationId "com.example.apple.free"
                  versionName "1.0"
                  url "http://freeurl.com"
                  versionCode 1
              }
              applePremium {
                  applicationId "com.example.apple.premium"
                  versionName "1.0"
                  url "http://premiumurl.com"
                  versionCode 1
              }
              orangeFree {
                  applicationId "com.example.orange.free"
                  versionName "1.0"
                  versionCode 1
                  url "http://freeurl.com"
  
  
              }
              orangePremium {
                  applicationId "com.example.orange.premium"
                  url "http://premiumurl.com"
                  versionName "1.0"
                  versionCode 1
  
              }
          } 
  </code></pre>
  
  <p><strong>Issue:</strong> Not sure how to make that work.</p>
  
  <p>Any tips are helpful.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <h2>Final Solution?</h2>
  
  <pre><code>flavorGroups 'fruit', 'paid'
  
  productFlavors {
      apple {
          flavorGroup 'fruit'
      }
      orange {
          flavorGroup 'fruit'
      }
      free {
          flavorGroup 'paid'
      }
      premium {
          flavorGroup 'paid'
      }
      appleFree {
          applicationId "com.example.apple.free"
          versionName "1.0"
          buildConfigField 'String', 'BASE_URL', 'http://freeurl.com'
          versionCode 1
      }
      applePremium {
          applicationId "com.example.apple.premium"
          versionName "1.0"
          buildConfigField 'String', 'BASE_URL', 'http://premiumurl.com'
          versionCode 1
      }
      orangeFree {
          applicationId "com.example.orange.free"
          versionName "1.0"
          versionCode 1
          buildConfigField 'String', 'BASE_URL', 'http://freeurl.com'
      }
      orangePremium {
          applicationId "com.example.orange.premium"
          buildConfigField 'String', 'BASE_URL', 'http://premiumurl.com' 
          versionName "1.0"
          versionCode 1
      }
      }
  </code></pre>