title: Android gradle: Exclude file from external dependencies
body: <p>I added <code>com.eftimoff:android-pathview:1.0.8@aar</code> to my project but when i run app i got this error:</p>
  
  <blockquote>
    <p>Cause: duplicate entry: META-INF/MANIFEST.MF</p>
  </blockquote>
  
  <p>This is <code>pathview</code> file in external libraries :</p>
  
  <p><a href="https://i.stack.imgur.com/il5er.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/il5er.png" alt="enter image description here"></a></p>
  
  <p>Now i want to exclude <code>META-INF</code> folder so I added this in to module gradle :</p>
  
  <pre><code>android {
  ...
      packagingOptions {
          exclude 'META-INF/*'
      }
  }
  </code></pre>
  
  <p>But i still got above error.</p>
  
  <p>I wrote gradle script like <a href="https://stackoverflow.com/questions/34397796/how-to-exclude-specific-resources-from-an-aar-depedency/34525498">this suggestion</a> </p>
  
  <p>When i added library like this way :
  <code>implementation ('com.eftimoff:android-pathview:1.0.8@aar').exclude("META-INF/MANIFEST.MF")</code></p>
  
  <p>I got this error :</p>
  
  <pre><code>FAILURE: Build failed with an exception.
  
  * Where:
  Build file 'D:\Projects\Android\Mvvm\Kotlin\MovieDb\app\build.gradle' line: 80
  
  * What went wrong:
  A problem occurred evaluating project ':app'.
  &gt; Could not find method leftShift() for arguments [build_37vlhf9la1wtl8koroxp1kll7$_run_closure4@b32cc6a] on task ':app:excludeTask' of type org.gradle.api.DefaultTask.
  
  * Try:
  Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
  
  * Get more help at https://help.gradle.org
  
  CONFIGURE FAILED in 0s
  Could not find method leftShift() for arguments [build_37vlhf9la1wtl8koroxp1kll7$_run_closure4@b32cc6a] on task ':app:excludeTask' of type org.gradle.api.DefaultTask.
  Open File
  </code></pre>
  
  <p>This is where <code>tasks.create("excludeTask") &lt;&lt; {</code> i got error.
  So i changed <code>task.create()</code> to this:</p>
  
  <pre><code>tasks.create("excludeTask")  {
      doLast{
          exclusions.each {
              File file = file("${buildDir}/intermediates/exploded-aar/${it}")
              println("Excluding file " + file)
              if (file.exists()) {
                  file.delete()
              }
          }
      }
  }
  </code></pre>
  
  <p>And those errors gone but still i got this error again:</p>
  
  <blockquote>
    <p>Cause: duplicate entry: META-INF/MANIFEST.MF</p>
  </blockquote>
  
  <p>This script seems work on <code>resources</code> and now i want to exclude a file inside a folder.</p>
  
  <p><strong>This is complete gradle:</strong></p>
  
  <pre><code>apply plugin: 'com.android.application'
  
  apply plugin: 'kotlin-android'
  
  apply plugin: 'kotlin-android-extensions'
  
  android {
  
      // Enables data binding.
      dataBinding {
          enabled = true
      }
  
      compileSdkVersion 29
      buildToolsVersion "29.0.2"
      defaultConfig {
          applicationId "com.t.moviedb"
          minSdkVersion 19
          targetSdkVersion 29
          versionCode 1
          versionName "1.0"
          testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
          }
      }
  
  //    packagingOptions {
  //        exclude '/META-INF/*'
  //    }
  //    aaptOptions {
  //        ignoreAssetsPattern "!META-INF/MANIFEST.MF"
  //        ignoreAssetsPattern "META-INF/MANIFEST.MF"
  //    }
  }
  
  
  final List&lt;String&gt; exclusions = [];
  
  Dependency.metaClass.exclude = { String[] currentExclusions -&gt;
      currentExclusions.each {
          exclusions.add("${getGroup()}/${getName()}/${getVersion()}/${it}")
      }
      return thisObject
  }
  
  
  dependencies {
      implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
      implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
  
      // Support libraries
      implementation 'androidx.appcompat:appcompat:1.1.0'
      implementation 'androidx.fragment:fragment:1.1.0'
      implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  
      // Android KTX
      implementation 'androidx.core:core-ktx:1.1.0'
  
      // Testing
      implementation 'androidx.legacy:legacy-support-v4:1.0.0'
      testImplementation 'junit:junit:4.12'
      androidTestImplementation 'androidx.test.ext:junit:1.1.1'
      androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
  
      //other
      implementation ('com.eftimoff:android-pathview:1.0.8@aar').exclude("META-INF/*")
  }
  
  tasks.create("excludeTask")  {
      doLast{
          exclusions.each {
              File file = file("${buildDir}/intermediates/exploded-aar/${it}")
              println("Excluding file " + file)
              if (file.exists()) {
                  file.delete()
              }
          }
      }
  }
  
  tasks.whenTaskAdded({
      if (it.name.matches(/^process.*Resources$/)) {
          it.dependsOn excludeTask
      }
  }) 
  </code></pre>
  
  <p>This is my gradle version: </p>
  
  <pre><code>\MovieDb&gt;gradlew --version
  
  ------------------------------------------------------------
  Gradle 5.4.1
  ------------------------------------------------------------
  
  Build time:   2019-04-26 08:14:42 UTC
  Revision:     261d171646b36a6a28d5a19a69676cd098a4c19d
  
  Kotlin:       1.3.21
  Groovy:       2.5.4
  Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
  JVM:          1.8.0_231 (Oracle Corporation 25.231-b11)
  OS:           Windows 10 10.0 amd64
  </code></pre>
  
  <p>and :</p>
  
  <p><a href="https://i.stack.imgur.com/Vyqq5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Vyqq5.png" alt="enter image description here"></a></p>