title: Editing AndroidManifest.xml in Gradle task processManifest.doLast has no effect when running app from Android Studio
body: <p>I use the following Gradle script to make some modifications to the AndroidManifest.xml at compile time. In this example I want to inject a <code>&lt;meta-data&gt;</code> element. The code is based on <a href="https://stackoverflow.com/a/27372806/559745">this answer</a>.</p>
  
  <pre class="lang-groovy prettyprint-override"><code>android {
      // ...
      applicationVariants.all { variant -&gt;
          variant.outputs.each { output -&gt;
              output.processManifest.doLast {
                  def manifestOutFile = output.processManifest.manifestOutputFile
                  def newFileContents = manifestOutFile.getText('UTF-8').replace("&lt;/application&gt;", "&lt;meta-data ... /&gt;&lt;/application&gt;")
                  manifestOutFile.write(newFileContents, 'UTF-8')
              }
          }
      }
  }
  </code></pre>
  
  <p>This works as expected when I do a Gradle sync in Android Studio or make a clean build from command line: The meta-data is accessible from within the app.</p>
  
  <p>But when I run ▶ the application from Android Studio the modified manifest seems to be ignored, since the inserted meta-data is not part of the compiled manifest in the APK, and the app itself cannot find it at runtime either, the meta-data is simply not there.</p>
  
  <p>In all cases the merged intermediate AndroidManifest.xml (in <em>/build/intermediates/manifests/</em>) does contain the changes, but for some reason it looks like it gets ignored if I run the app.</p>
  
  <p>To make it even more obvious, I tried to insert some invalid XML: In this case, the Gradle sync and the clean build failed as expected because of a syntax error in the manifest. But I was still able to run the app from Android Studio, thus the modification effectively gets ignored..</p>
  
  <p>The easiest way to reproduce this is to clean the project first (in Android Studio), which causes the manifest to be reprocessed (in case of the syntax error I get a failure as expected), and then run the app, which works even with an invalid manifest.</p>
  
  <p>Note that the task in <code>doLast</code> gets executed everytime: A <code>println()</code> in the task is printed and the intermediate manifest contains the changes.</p>
  
  <p>It's as if the manifest gets compiled into the APK before my task is executed.</p>
  
  <p>Where is the issue here?</p>
  
  <p>I'm using Android Studio 2.0 with the Android Gradle Plugin 2.0.0.</p>