title: How can I reduce the size of xml assets when building my apk
body: <p>In my app I use the assets directory to store some XML files (some are large).
  In my design time I want the files to use indentation and also put some comments in it.
  This is enlarging my xml files and can add up to a large size.</p>
  
  <p>Is it possible to add a task to the gradle build to remove all indentation and comments for the xml files before packaging it in the apk? If so how?</p>
  
  <p>This will not only shrink my apk, but will also assist at run time with the xml processing.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>The answer by <strong>fhomovc</strong> was correct, but was missing some part.
  I will mark it as correct but if anyone else will need it, here are the details:</p>
  
  <p>In general I need a task that will run the minify utility and it should look like:</p>
  
  <pre><code>task minifyAssets(type:Exec) {
      workingDir dirName // the directory of the merged assets under the build directory
      commandLine 'minify', '-r', '-o', '.', '.'
      doFirst {
          println 'minifyAssets...'
      }
  }
  </code></pre>
  
  <p>This task should only be executed after the merged assets task is executed and before the package task is executed.</p>
  
  <p>The main problem is that there should be a dedicated task for each variant, so I needed to do it dynamically:</p>
  
  <p>First create the exec task and make it dependent on the merge task</p>
  
  <pre><code>    applicationVariants.all { variant -&gt;
          // dynamically add minify task for specific variant
          def dirName = new File("app\\build\\intermediates\\merged_assets\\"  + variant.name + "\\out\\levels").getAbsolutePath()
          def minifyTaskName = "minifyAssets" + variant.name
          def mergeAssetsTaskName = "merge" + variant.name + "Assets"
          def myTask = tasks.register(minifyTaskName, Exec) {
              workingDir dirName
              // require that minify utility will be in the path. Download from https://github.com/tdewolff/minify/tree/master/cmd/minify
              commandLine 'minify', '-r', '-o', '.', '.'
              doFirst {
                  println 'minifyAssets...' + workingDir
              }
          }
          // set the minify task dependant on the merge assets task
          myTask.get().dependsOn mergeAssetsTaskName
      }
  </code></pre>
  
  <p>Now we need to make the specific package task depend on the minify task:</p>
  
  <pre><code>// when the package task is added make it dependant on the minify task
  tasks.whenTaskAdded { theTask -&gt;
      if (theTask.name.startsWith("package") &amp;&amp; (theTask.name.endsWith("Debug") || theTask.name.endsWith("Release"))) {
          def minifyTaskName = theTask.name.replace("package", "minifyAssets")
          theTask.dependsOn minifyTaskName
      }
  }
  </code></pre>