Duplicate files copied in APK META-INF/LICENSE异常冲突解决

来源:互联网 发布:photosynth替代软件 编辑:程序博客网 时间:2024/06/06 11:37

最近在引进一个开源项目,ColorfulNews遇到这样一个问题:

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE
File1: C:\Users\admin\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-databind\2.7.2\84ffa765dd258dbab8695963c41308b054f3a1cb\jackson-databind-2.7.2.jar
File2: C:\Users\admin\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-annotations\2.7.0\19f42c154ffc689f40a77613bc32caeb17d744e3\jackson-annotations-2.7.0.jar
File3: C:\Users\admin\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-core\2.7.2\8b8310381b690e317f5f0574e9b2dd7034778b4c\jackson-core-2.7.2.jar

把它理解成证书冲突吧,就像我们有时候引用jar包一样,里面有重复的class,需要在gradle文件中加如下引用,解决这个问题

packagingOptions {    exclude 'META-INF/LICENSE.txt'    exclude 'META-INF/LICENSE'    exclude 'META-INF/license.txt'}
很多人不明白为什么,证书冲突也不可以,stackflow上的解释:

In Android Gradle builds, you're not permitted to include the same file with the same path more than once in the output. In your build, there were two META-INF/DEPENDENCIES files coming from different places. Since you don't need this file at all in your application, the simplest thing to do is to tell the build system to ignore it altogether, which is what this exclude directive does.

在gradle构建中,不允许引入一个相同的文件(拥有相同的路径)2次。在构建中,如果有2个META-INF/DEPENDENCIES 文件来自不同的路径。如果在应用中不需要这个文件了,最简单的方法是告诉构建系统忽略全部这个,通过用 exclude指令来做这件事

There's also a pickFirst directive to tell the build system to keep one of the copies; there's a tiny amount of detail on that in Android Gradle plugin 0.7.0: "duplicate files during packaging of APK".

还有一个指令pickFirst指令告诉构建系统保持一份拷贝,这里有一个细节文章详见 Android Gradle plugin 0.7.0: "duplicate files during packaging of APK".

Android builds in Gradle are rather strict about duplicate files, which can make life difficult. There's a similar problem if you include the same Java class more than once, where you get the "Multiple dex files define" error (see Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat) for a typical example).

Other build systems are more lenient. It's typical in Java that if you include the same class more than once in a classpath, for example, the first copy it sees is the one that's used; duplicates after that are ignored. This is in most cases easier to deal with, but it has a couple problems. The biggest one is that there can be subtle errors if multiple different versions of a file creep into the build without you knowing -- it can be difficult to figure out what's going on. When you do figure it out, you can usually solve it by juggling the order in which things are included to make sure the one you want makes it to the final output, but in very complex builds, this can be difficult to achieve, and it can happen that doing seemingly unrelated things like including new libraries in your project can upset the ordering and lead to a lot of woe.

For that reason, Gradle has the philosophy of not relying on ordering of things to determine "winners" in the game of resolving duplicates, and it forces the developer to make all dependencies explicit. Android's implementation of its build system on top of Gradle follows that philosophy.


0 0
原创粉丝点击