添加JavaMail API后Gradle构建报错:存在重复META-INF/LICENSE文件
Hey there! I get it, dealing with Gradle errors when adding new libraries can be frustrating—especially when you're still getting the hang of how Gradle works. Let's break down this issue and fix it step by step.
What's causing this error?
When you added the JavaMail API, multiple dependencies in your project (likely JavaMail itself and other related libraries) are including identical META-INF/LICENSE files. Android's Gradle build system flags this as a conflict because it doesn't allow duplicate files in the final APK by default.
Solution 1: Exclude duplicate LICENSE files
The simplest fix is to tell Gradle to ignore all instances of the duplicate META-INF/LICENSE file. Here's how to do it:
- Open your app module's
build.gradlefile (not the root project's one—look for the file inside theapp/folder). - Add the
packagingOptionsblock inside theandroidsection:
android { // Keep your existing configuration (compileSdk, defaultConfig, etc.) here packagingOptions { exclude 'META-INF/LICENSE' // If you also see errors about LICENSE.txt, add this line too: // exclude 'META-INF/LICENSE.txt' } }
- Sync your Gradle project (click the "Sync Now" prompt that appears) and rebuild.
Solution 2: Pick the first occurrence instead of excluding
If you'd prefer to keep one copy of the LICENSE file instead of removing all, use pickFirst instead. This tells Gradle to use the first instance it finds and skip the rest:
android { // Existing configuration... packagingOptions { pickFirst 'META-INF/LICENSE' } }
Why the previous ASL2.0 solution didn't work
The solution you tried was for META-INF/ASL2.0, which is a different license file. Since your error is about META-INF/LICENSE, you need to target that specific file path instead.
Give either of these methods a shot—they should resolve the conflict caused by the JavaMail API dependencies.
内容的提问来源于stack exchange,提问作者Paras Gupta

