【问题标题】:Handling common dependencies between interdependent modules in Android Studio在 Android Studio 中处理相互依赖的模块之间的常见依赖关系
【发布时间】:2015-05-07 23:57:05
【问题描述】:

假设我的 Android Studio 项目中有 2 个模块:

---- :A
---- :B

其中 :A 依赖于 B:

---- :A ---> + :B
---- :B

让事情变得复杂的是 A 和 B 都需要一个库 L:

---- :A ---> + :B
             + :L

---- :B ---> + :L

最初包含的模块的 gradle 文件:

模块 A:

dependencies {
    compile project(':B')
    compile 'com.L:library:1.0.0'
}

模块 B:

dependencies {
    compile 'com.L:library:1.0.0'
}

但这给出了一个错误,表明在 dex 文件的 inputList.txt 中提供了多个库:

Execution failed for task ':funtainment:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
C:\Development\Android\AndroidStudio\Sdk\build-tools\21.1.2\dx.bat --dex --no-optimize --output C:\Development\Workspace\Android\AndroidStudio\Funtainment\funtainment\build\intermediates\dex\debug --input-list=C:\Development\Workspace\Android\AndroidStudio\Funtainment\funtainment\build\intermediates\tmp\dex\debug\inputList.txt
Error Code:
2
Output:

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lcom/nineoldandroids/animation/Animator$AnimatorListener;
    at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
    at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)
    at com.android.dx.command.dexer.Main.run(Main.java:246)
    at com.android.dx.command.dexer.Main.main(Main.java:215)
    at com.android.dx.command.Main.main(Main.java:106)

所以我尝试了一种在 Eclipse 上对我有用的不同结构,以应对类似情况:只需从模块 A 中删除库依赖项:

---- :A ---> + :B

---- :B ---> + :L

模块的 gradle 文件包含:

模块 A:

dependencies {
    compile project(':B')
}

模块 B:

dependencies {
    compile 'com.L:library:1.0.0'
}

但是我得到了和以前一样的错误!

其他相关信息:

  • 在整个过程中没有出现任何 java 编译错误(如预期)
  • 该错误仅在尝试部署时出现(特别是在:A:dexDebug
  • 上面的库“L”代表“com.nineoldandroids:library:2.4.0”

这是和 Android Studio 的错误还是这是预期的行为? 如何让这两个模块都引用这个库,而不会导致多个库被添加到 dex 中并导致错误?

【问题讨论】:

  • 我昨天遇到了同样的错误,我在我的项目中包含了一个 api。您必须检查您的yourproject/apps/libs 文件夹,可能有多个同名文件。假设您在 libs 文件夹中有 sourcebinary 文件,然后从中删除源文件并尝试再次同步您的项目
  • 我已确保我的项目中没有重复的 lib 文件。搜索任何此类 lib 文件仅在 intermediates 文件夹中显示重复项(由 Android Studio 在编译/部署操作期间生成,我认为这是预期的行为。因此该解决方案似乎不适用于我

标签: android module android-studio dependencies


【解决方案1】:

您可以使用exclude

compile ('great:library:1.0') {
    exclude module: 'lib'
}

compile (project(':mymodule')) {
    exclude module: 'lib'
}

【讨论】:

  • 第一种方法不适用于我,因为模块 A 需要模块 B。您的第二种方法似乎是合乎逻辑的解决方案,但我在尝试第二种方法时遇到与以前相同的错误:'(
  • 成功了!在尝试了您的解决方案后,我从所有 intermediates 文件夹中手动删除了 L lib 的所有副本(通过搜索并删除结果),然后尝试再次部署。我花了 5 分钟来部署,但它成功了!
猜你喜欢
  • 1970-01-01
  • 2021-02-21
  • 2015-12-28
  • 2021-10-17
  • 1970-01-01
  • 1970-01-01
  • 2011-06-26
  • 2013-04-06
  • 1970-01-01
相关资源
最近更新 更多