android-How to solve 'More than one file was found with OS independent path META-INF/DEPENDENCIES'

1. The purpose of this post

I would demo how to solve this error when using android studio

More than one file was found with OS independent path 'META-INF/DEPENDENCIES'

2. Environments

  • Android Studio 3.x
  • gradle wrapper version:
    • distributionUrl=https://services.gradle.org/distributions/gradle-5.4.1-all.zip
  • project build.gradle
    • classpath ‘com.android.tools.build:gradle:3.5.0’

3. Solution and Code

Add this package options in the app’s build.gradle

android {
	...
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
    }
    ...
}

Clean,rebuild and run the app, everything works.