title: "Duplicate resources" when adding resources with a res generating task
body: <p>I have my own Gradle plugin where I'd like to add a task which would take launcher icons from <code>main</code> folder and output icons for each build type/flavor folder tinted with different colors depending on user's extension configuration. </p>
  
  <p>I've added the task for application variant: </p>
  
  <pre><code>variant.registerResGeneratingTask(tintTask, new File("${project.buildDir}/generated/res/${variant.name}"))
  </code></pre>
  
  <p>Then in the task I perform the operation described above. Until here everything is fine - the sources are generated and the folder is marked as a resource folder. </p>
  
  <p>The problem is when I try to create a build and the flow hits the <code>mergeXXXResources</code> task (in this case xxx == debug). </p>
  
  <p>At this point I get an exception comparing the <code>mipmap-[dpi]-v4/ic_launcher</code> in the <code>main/res</code> with those in <code>generated/res/debug</code>. </p>
  
  <p>For example: </p>
  
  <pre><code>Execution failed for task ':app:mergeDebugResources'.
  [mipmap-hdpi-v4/ic_launcher] /{proj_location}/android_example/app/src/main/res/mipmap-hdpi/ic_launcher.png  
  [mipmap-hdpi-v4/ic_launcher] /{proj_location}/android_example/app/build/generated/res/debug/mipmap-hdpi/ic_launcher.png: 
  Error: Duplicate resources
  </code></pre>
  
  <p>I tried different locations for my output files, but I don't believe it makes any difference. I expected the resource merger to be able to identify generated resources and parse them in the final output, but obviously I'm doing something horribly wrong. </p>
  
  <p>I've tried using the Transform API instead, but perhaps because of scarce documentation and my lack of understanding my attempts weren't very successful (I couldn't locate resource files in a fashion similar to the way I've located java files during transform operation). </p>
  
  <p>I'm looking for either a piece of advice on how to resolve my current problem, or an alternative approach which would perform the task I've initially set out to achieve.</p>
  
  <p><strong>EDIT:</strong> on request, code for my task action:</p>
  
  <pre><code>@TaskAction
  def convertLauncherIcons() {
      def android = project.extensions.getByType(AppExtension)
      File outputDir = new File("${project.buildDir}/generated/tintLaunchIcons/res/${taskVariant.name}")
  
      android.sourceSets.each {
          if ("main".equals(it.name)) {
              it.res.srcDirs.each { dirIt -&gt;
                  dirIt.absoluteFile.list().each { resDir -&gt;
                      if (resDir.startsWith("mipmap-")) {
                          def relIconPath = "${resDir}/ic_launcher.png"
                          File launcherFile = new File(dirIt.absolutePath, relIconPath);
                          if (launcherFile.exists()) {
                              BufferedImage img = ImageIO.read(launcherFile);
                              /* IMAGE MANIPULATION HERE */
                              File outputFile = new File(outputDir, relIconPath);
                              if (!outputFile.exists()) {
                                  File parent = outputFile.getParentFile();
                                  if (!parent.exists() &amp;&amp; !parent.mkdirs()) {
                                      throw new IllegalStateException("Couldn't create dir: " + parent);
                                  }
                                  outputFile.createNewFile();
                              }
                              println "writing file ${outputFile.canonicalPath}"
                              ImageIO.write(img, "png", outputFile);
         } ... 
  </code></pre>