首页 文章

Android Gradle应用程序和库jar冲突

提问于
浏览
1

我想将一个大型应用程序从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')
    }

    ...

然后每个Module 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'的执行失败 .

在APK META-INF / ASL2.0中复制的重复文件,然后是2个JAR的路径 .

在从每个模块构建APK时,如何告诉Gradle不要多次复制相同的JAR?此时,我无法将依赖关系移动到Maven中央回购,即使我将来会这样做 . 也许在父App中添加所有的lib?它对我来说看起来不太好......在这种情况下,我如何在build.gradle模块中指定依赖项?

1 回答

  • 1

    在您的gradle项目文件中,您可以将其添加到“android”块:

    packagingOptions {
        exclude 'META-INF/ASL2.0'
    }
    

    似乎META-INF / ASL2.0文件正在被复制 .

相关问题