'Build gradle : Could not find method packagingOptions() for arguments root Project "fasterDev"

I am using single build.gradle file in application. Regarding version 1.6 it's working properly. but I want to use this for location update with module.

apply plugin: 'com.android.application'
...

dependencies {
    compile 'com.google.android.gms:play-services:fp9.0.0'
}

Here is my build.gradle.

build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
    }
}
apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

android {
    compileSdkVersion 15
    buildToolsVersion "21.1.2"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }

}

packagingOptions {
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/dependencies.txt'
    exclude 'META-INF/LGPL2.1'
}  

Any tutorial or suggestion for this?

Logs:

org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:48)
Caused by: org.gradle.api.internal.MissingMethodException: Could not find method packagingOptions() for arguments [build_b54s9k8r9uto6awxmkxnu2ipo$_run_closure3@3a2d3196] on root project 'fasterDev'.
    at org.gradle.api.internal.AbstractDynamicObject.methodMissingException(AbstractDynamicObject.java:68)


Solution 1:[1]

You've provided the packagingOptions outside of the android closure, that is the reason Gradle supposes, that it's some root project's method.

Just move it into the android closure, as:

android {
    ...

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }      
}

Solution 2:[2]

With newer versions of AGP (Android Gradle Plugin), the exclude function is deprecated and replaced with resources.excludes property.

Kotlin DSL (build.gradle.kts):

android {
  // ...
  packagingOptions {
    resources.excludes += "META-INF/LICENSE*"
    resources.excludes += "META-INF/NOTICE.txt"
    // OR
    // resources.excludes += setOf(
    //   "META-INF/LICENSE*",
    //   "META-INF/NOTICE.txt"
    // )
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stanislav
Solution 2