我想将一个大型应用程序从 Ant 迁移到 Gradle。 由于该应用程序已(错误地)创建为多个库,因此第一次迁移将保留相同的模块(它将在第二步中重构)并且它可能会以这种结构结束:
-- Project/ |-- App/ |-- libs/ |-- src/... |-- build.gradle |-- Module1/ |-- libs/ |-- lib1.jar |-- lib2.jar |-- src/... |-- build.gradle |-- Module2/ |-- libs/ |-- lib2.jar |-- lib3.jar |-- src/... |-- build.gradle build.gradle settings.gradle
App build.gradle 是这样的:
... apply plugin: 'com.android.application' dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:support-v4:20.0.0' compile 'com.android.support:appcompat-v7:20.0.0' compile project(':Module1') compile project(':Module2') } ...
然后每个模块 build.gradle 将是这样的:
... apply plugin: 'com.android.library' dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:support-v4:20.0.0' compile 'com.android.support:appcompat-v7:20.0.0' } ...
每个模块都可以正确编译,但是在构建 APK 时出现问题,因为 lib2.jar 被复制了 2 次,导致错误:
错误:Gradle:任务 ':app:packageDebug' 执行失败。
Duplicate files copied in APK META-INF/ASL2.0 and then the path to the 2 JARs.
在从每个模块构建 APK 时,如何告诉 Gradle 不要多次复制相同的 JAR? 目前,我无法将依赖项移动到 Maven 中央仓库,即使我将来会这样做。 也许在父应用程序中添加所有库?它对我来说看起来不太好......在这种情况下,我如何在 build.gradle 模块中指定依赖项?
最佳答案
在您的 gradle 项目文件中,您可以将其添加到“android” block 中吗:
packagingOptions {
exclude 'META-INF/ASL2.0'
}
META-INF/ASL2.0 文件似乎被复制了。
关于Android Gradle 应用程序和库 jar 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26590559/