title: Conditional dependencies with Gradle, is it possible?
body: <p>I have an Android project (already ported to Android Studio and using Gradle) that is made up of different modules.</p>
  
  <p>The project is actually used to create two different apps, where the code is pretty much the same, except for some resources.</p>
  
  <p>Thus the resources have been split into two different modules.</p>
  
  <p>The original author of this project used to work in Eclipse and switch the resource modules included in the dependencies based on which app he wanted to build. And he also used to change by hand the package name in AndroidManifest.xml</p>
  
  <p>I would like to automate all of this and still have a single code base, but have two build targets with specific modules for each target. Is that doable with Gradle?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>To make things even harder, my project has a hierarchy that is pretty much the following:</p>
  
  <pre><code>--+--MainProject
    +--LibData
    +--LibBase
    +--LibResA
    +--LibResB
  </code></pre>
  
  <p>Where:</p>
  
  <ul>
  <li>MainProject depends on LibBase and LibData.</li>
  <li>LibData depends on LibBase</li>
  <li>LibBase either depends on LibResA or LibResB based on the final APK that I need to build.</li>
  </ul>
  
  <p>As suggested, I've tried implementing this with flavors by adding in the MainProject build.gradle the following:</p>
  
  <pre><code>productFlavors {
      producta {
      }
      productb {
      }
  }
  </code></pre>
  
  <p>And then in LibBase I've added the following to its build.gradle:</p>
  
  <pre><code>dependencies {
      productaCompile project(':LibResA')
      productbCompile project(':LibResB')
  }
  </code></pre>
  
  <p>But then, when I build the project, LibData can't find the classes and resources inherited from LibBase. So now I'm stuck with this error. To me it looks like LibBase isn't being copied to the intermediates of LibData. That way LibData can't resolve the classes in LibBase, but it's just my assumption.</p>
  
  <p><strong>Update 2:</strong></p>
  
  <p>I kept investigating this issue and now I've changed my build.gradle files to look like this:</p>
  
  <p><em>Main Project build.gradle:</em></p>
  
  <pre><code>defaultPublishConfig "productaRelease"
  publishNonDefault true
  
  productFlavors {
      producta {
          applicationId "com.producta"
      }
  
      productb {
          applicationId "com.productb"
      }
  }
  
  dependencies {
      compile project(':LibData')
  }
  </code></pre>
  
  <p><em>LibData build.gradle (has no product flavors, just the dependencies):</em></p>
  
  <pre><code>dependencies {
      compile project(':LibBase')
  }
  </code></pre>
  
  <p><em>LibBase build.gradle:</em></p>
  
  <pre><code>defaultPublishConfig "productaRelease"
  publishNonDefault true
  
  productFlavors {
      producta {
      }
  
      productb {
      }
  }
  
  dependencies {
      productaCompile project(path: ':LibResA')
      productbCompile project(path: ':LibResB')
  }
  </code></pre>
  
  <p>This way I get no errors when doing the usual <code>gradle clean build</code> but I can see that the resources included are always those of LibResA just like the defaultPublishConfig is the only one used at all times. </p>
  
  <p>If I open this project in Android Studio (0.8.1 atm) the result is that if I try to switch the build variant of the LibBase module and set it to productbRelease, the following error is being shown: Error:Module 'LibBase' has variant 'productbRelease' selected, but the module ''LibData'' depends on variant 'productaRelease'.</p>
  
  <p>I'm running out of ideas.</p>