title: How do I stop gradle from deleting my intermediate files before later tasks can consume them?
body: <p>In my Android Studio/gradle build I'm trying to automatically generate my app icon. The task that generates the icon image (a PNG) is being run at the correct time, but the image is being deleted before the task that consumes it runs.</p>
  
  <p>This is a simplified version of what I've added to my <code>app/build.gradle</code> which illustrates the problem:</p>
  
  <pre class="lang-groovy prettyprint-override"><code>task createIcon(type:Exec) {
      def outdir =
          new File("$buildDir/intermediates/res/merged/debug/drawable-ldpi/")
      outputs.dir(outdir)
      if (!outdir.exists()) {
          outdir.mkdirs()
      }
      def svg = 'src/images/ic_launcher.svg'
      inputs.source(new File(svg))
      def png = new File(outdir, "ic_launcher.png")
      outputs.file(png)
      commandLine "$workingDir/svgToPng.sh", svg, png
  }
  
  tasks.whenTaskAdded{ t -&gt;
        if(t.name.equals("generateDebugAssets")){
            t.dependsOn createIcon
        }
  }
  </code></pre>
  
  <p>If I run <code>./gradlew clean</code> and then <code>./gradlew assembleDebug</code>, I can see that the output file of my <code>createIcon</code> task, "app/build/intermediates/res/merged/debug/drawable-ldpi/ic_launcher.png" is created and then very soon afterwards deleted. I get the following output from gradlew:</p>
  
  <pre class="lang-none prettyprint-override"><code>:app:preBuild UP-TO-DATE
  :app:preDebugBuild UP-TO-DATE
  ...
  Bitmap saved as: /home/laurence/src/foo/app/build/intermediates/res/merged/debug/drawable-ldpi/ic_launcher.png
  :app:generateDebugAssets
  :app:mergeDebugAssets
  :app:generateDebugResValues UP-TO-DATE
  :app:generateDebugResources
  :app:mergeDebugResources
  :app:processDebugManifest
  :app:processDebugResources
  /home/laurence/src/foo/app/build/intermediates/manifests/full/debug/AndroidManifest.xml:13:23-44 : AAPT: No resource found that matches the given name (at 'icon' with value '@drawable/ic_launcher').
  
  :app:processDebugResources FAILED
  
  FAILURE: Build failed with an exception.
  
  * What went wrong:
  Execution failed for task ':app:processDebugResources'.
  &gt; com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/home/laurence/.local/android-sdks/build-tools/23.0.2/aapt'' finished with non-zero exit value 1
  
  * Try:
  Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
  
  BUILD FAILED
  </code></pre>
  
  <p>You can see that the task <code>:app:processDebugResources</code> fails because <code>aapt</code> claims it can't find <code>@drawable/ic_launcher</code> (the value of <code>android:icon</code> in my <code>AndroidManifest.xml</code>) which should correspond to the the place where I'm generating the PNG.</p>
  
  <p>Note that I've confirmed that the PNG is being generated not just from the "Bitmap saved as" line in the console output, but also by watching the destination directory during the build. The file winks into existence and then vanishes a moment later. </p>
  
  <p>Even weirder than that, if I run <code>./gradlew assembleDebug</code> a second time the build succeeds!</p>
  
  <p>How do I prevent my generated PNG from being deleted and ensuring that <code>processDebugResources</code> can see it without having to run my build twice?</p>