title: Override resources with gradle depending on buildType
body: <p>I want to override some strings in my res/strings.xml with gradle.</p>
  
  <p>I know that <a href="http://tools.android.com/tech-docs/new-build-system" rel="noreferrer">since Android Gradle Plugin 0.7.+</a> theres the possibilty to have a variant specific source folder. 
  But my app has a lot of flavors and I don't want to add additionally variant specific folders.</p>
  
  <p><strong>UPDATE 2014-01-17</strong></p>
  
  <p>What I want in detail:</p>
  
  <p>I have some variables in my Resources that are depending only by the buildType (e.g. "release"). 
  First I thought my <strong>SOLUTION_1</strong> (override data after resources were merged) is nice, because if I have to change these variables I just have to change them in the build.config (just one place). 
  But as Scott Barta wrote in the comment below there are some good reasons why this solution is NOT a good idea.</p>
  
  <p>So i tried another solution <strong>SOLUTION_2</strong> (just merge the right resources) based on <a href="https://github.com/shakalaca/learning_gradle_android/blob/082fa1575de1fa75ef09c90cb0cfdd60407e184d/07_tricks/app/build.gradle" rel="noreferrer">this GitHub project of shakalaca</a>. I think this way is more elegant and I still have the advantage just to change the variables in <strong>one</strong> place!</p>
  
  <p><strong>SOLUTION_1 (override data after resources were merged):</strong></p>
  
  <p>What I did in AS 0.4.2:</p>
  
  <ul>
  <li><p>in <code>build.gradle</code> I try to override the string "Hello World" to "OVERRIDE" (<a href="https://stackoverflow.com/questions/17465353/how-to-replace-a-string-for-a-buildvariant-with-gradle-in-android-studio">based on my answer at this post</a>):</p>
  
  <pre><code>android.applicationVariants.all{ variant -&gt;
      // override data in resource after merge task
      variant.processResources.doLast {
          overrideDataInResources(variant)
      }
  }
  
  def overrideDataInResources(buildVariant){
      copy {
          // *** SET COPY PATHS ***
          try {
              from("${buildDir}/res/all/${buildVariant.dirName}") {
                  // println "... FROM: ${buildDir}/res/all/${buildVariant.dirName}"
                  include "values/values.xml"
              }
          } catch (e) {
              println "... EXCEPTION: " + e
          }
  
          into("${buildDir}/res/all/${buildVariant.dirName}/values")
          // println "... INTO: ${buildDir}/res/all/${buildVariant.dirName}/values"
  
          // --- override string "hello_world"
          filter {
              String line -&gt;
                  line.replaceAll("&lt;string name=\"hello_world\"&gt;Hello world!&lt;/string&gt;",
                          "&lt;string name=\"hello_world\"&gt;OVERRIDE&lt;/string&gt;");
          }
  
      // *** SET PATH TO NEW RES *** 
      buildVariant.processResources.resDir = file("${buildDir}/res/all/${buildVariant.dirName}/values/values/values.xml")
      // println "... NEW RES PATH: " + "${buildDir}/res/all/${buildVariant.dirName}/values/values/values.xml"
      }
  }
  </code></pre></li>
  </ul>
  
  <p>The copy and filter task works fine, but I couldn't set the "new" values.xml as string resource.</p>
  
  <p><strong>SOLUTION_2 (just merge the right resources)</strong></p>
  
  <ul>
  <li>define a floavor for specific buildType (e.g. "releaseRes")</li>
  <li><p>merge this resourses with the flavor you want to build:</p>
  
  <pre><code>android.applicationVariants.all{ variant -&gt;
      variant.mergeResources.doFirst{
          checkResourceFolder(variant)
      }
  }
  
  def checkResourceFolder(variant){
      def name = variant.name;
      if(name.contains("Release")){
         android.sourceSets.release.res.srcDirs = ['src/releaseRes/res']
         android.sourceSets.flavor1.res.srcDirs = ['src/flavor1/res']
      }
  }
  </code></pre></li>
  </ul>