Android Studio:在编译时,复制在META-INF / DEPENDENCIES中的复制文件 [英] Android Studio: Duplicate files copied in APK META-INF/DEPENDENCIES when compile

查看:188
本文介绍了Android Studio:在编译时,复制在META-INF / DEPENDENCIES中的复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Eclipse导出了我的项目,并使用此链接中的说明导入到Android Studio: http://开发人员.android.com / sdk / installation / migrate.html



当我构建时,我有一个错误:

 复制在APK中的重复文件META-INF / DEPENDENCIES 

搜索后,我找到一个解决方案:添加

  packagingOptions {
exclude'META-INF / DEPENDENCIES'
}

into build.gradle 。而且它的工作原理!



但我不明白为什么我有这个错误,为什么我不得不应用该修复。有人可以解释吗?

解决方案

在Android Gradle构建中,不允许包含相同路径的相同文件一次输出。在您的构建中,有两个来自不同地方的 META-INF / DEPENDENCIES 文件。由于您的应用程序中根本不需要这个文件,所以最简单的就是告诉构建系统完全忽略它,这就是这个排除指令



还有一个 pickFirst 指令,告诉构建系统保持一个副本;在 Android Gradle插件0.7.0:APK包装过程中的重复文件



在Gradle中构建的重复文件相当严格,可以使生活困难重重。如果您多次包含相同的Java类,那么您会得到多个dex文件定义错误(参见多个dex文件定义了Landroid / support / v4 / accessibilityservice / AccessibilityServiceInfoCompat ))



其他构建系统更宽松。在Java中是典型的,如果在classpath中多次包含相同的类,例如,它看到的第一个副本是使用的;之后的重复项被忽略。这在大多数情况下更容易处理,但它有几个问题。最大的一个是,如果文件的多个不同版本在没有你知道的情况下爬行到构建中,那么可能会有微妙的错误 - 很难弄清楚发生了什么。当你弄清楚,你通常可以通过玩弄包含事物的顺序来解决这个问题,以确保你想要的那个顺序达到最终输出,但是在非常复杂的构建中,这可能很难实现,而且可能会发生这样,看似无关紧要的事情,例如在你的项目中包括新的图书馆可能会扰乱排序,并导致很多的困扰。



因此,Gradle的理念是不依赖于事物的排序来确定解决重复的游戏中的获胜者,并且强制开发人员使所有依赖关系明确。 Android的构建系统在Gradle之上的实现遵循了这一哲学。


I exported my project from Eclipse and imported to Android Studio using the instructions in this link: http://developer.android.com/sdk/installing/migrate.html

When I build, I have an error:

Duplicate files copied in APK META-INF/DEPENDENCIES

After searching, I found a solution: add

packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
}

into build.gradle. And it works!

But I don't understand why I had this error and why I've had to apply that fix. Can anybody explain?

解决方案

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.

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".

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.

这篇关于Android Studio:在编译时,复制在META-INF / DEPENDENCIES中的复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆