当前位置:   article > 正文

安卓解决包冲突问题。_duplicate class android.support.design.widget.coor

duplicate class android.support.design.widget.coordinatorlayout$viewelevatio

在安卓开发中,当我们项目中依赖的远程包里面有一模一样的包存在时就会导致包冲突,我们常用的解决方式就是删除任意包里面的包的方式解决冲突。
android studio也告诉了我们如何添加编译依赖项,我们只要按照规范来操作就可以了。
排除不需要的依赖项的写法:

dependencies {
        implementation('some-library') {
            exclude group: 'com.example.imgtools', module: 'native'
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5

所以解决包依赖冲突的关键问题是需要找到两个相互冲突的包的绝对路径,也就是说它们各自依赖的远程仓库,然后通过exclude关键字来剔除掉那个我们可以修改的冲突包,保留我们无法剔除的那个项目包**(比如Sdk里面的依赖包我们无法修改)**。
下面我们就来举一个例子:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:checkDebugDuplicateClasses'.
> 1 exception was raised by workers:
  java.lang.RuntimeException: Duplicate class android.support.design.widget.CoordinatorLayout found in modules coordinatorlayout-28.0.0-runtime.jar (com.android.support:coordinatorlayout:28.0.0) and design-25.3.1-runtime.jar (com.android.support:design:25.3.1)
  Duplicate class android.support.design.widget.CoordinatorLayout$1 found in modules coordinatorlayout-28.0.0-runtime.jar (com.android.support:coordinatorlayout:28.0.0) and design-25.3.1-runtime.jar (com.android.support:design:25.3.1)
  Duplicate class android.support.design.widget.CoordinatorLayout$Behavior found in modules coordinatorlayout-28.0.0-runtime.jar (com.android.support:coordinatorlayout:28.0.0) and design-25.3.1-runtime.jar (com.android.support:design:25.3.1)
  Duplicate class android.support.design.widget.CoordinatorLayout$DefaultBehavior found in modules coordinatorlayout-28.0.0-runtime.jar (com.android.support:coordinatorlayout:28.0.0) and design-25.3.1-runtime.jar (com.android.support:design:25.3.1)
  Duplicate class android.support.design.widget.CoordinatorLayout$DispatchChangeEvent found in modules coordinatorlayout-28.0.0-runtime.jar (com.android.support:coordinatorlayout:28.0.0) and design-25.3.1-runtime.jar (com.android.support:design:25.3.1)
  Duplicate class android.support.design.widget.CoordinatorLayout$HierarchyChangeListener found in modules coordinatorlayout-28.0.0-runtime.jar (com.android.support:coordinatorlayout:28.0.0) and design-25.3.1-runtime.jar (com.android.support:design:25.3.1)
  Duplicate class android.support.design.widget.CoordinatorLayout$LayoutParams found in modules coordinatorlayout-28.0.0-runtime.jar (com.android.support:coordinatorlayout:28.0.0) and design-25.3.1-runtime.jar (com.android.support:design:25.3.1)
  Duplicate class android.support.design.widget.CoordinatorLayout$OnPreDrawListener found in modules coordinatorlayout-28.0.0-runtime.jar (com.android.support:coordinatorlayout:28.0.0) and design-25.3.1-runtime.jar (com.android.support:design:25.3.1)
  Duplicate class android.support.design.widget.CoordinatorLayout$SavedState found in modules coordinatorlayout-28.0.0-runtime.jar (com.android.support:coordinatorlayout:28.0.0) and design-25.3.1-runtime.jar (com.android.support:design:25.3.1)
  Duplicate class android.support.design.widget.CoordinatorLayout$SavedState$1 found in modules coordinatorlayout-28.0.0-runtime.jar (com.android.support:coordinatorlayout:28.0.0) and design-25.3.1-runtime.jar (com.android.support:design:25.3.1)
  Duplicate class android.support.design.widget.CoordinatorLayout$ViewElevationComparator found in modules coordinatorlayout-28.0.0-runtime.jar (com.android.support:coordinatorlayout:28.0.0) and design-25.3.1-runtime.jar (com.android.support:design:25.3.1)
  
  Go to the documentation to learn how to <a href="d.android.com/r/tools/classpath-sync-errors">Fix dependency resolution errors</a>.


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

这里项目其实已经是编译通过了,但是运行时报了包冲突的问题,As告诉我们在我们的项目中存在多个class android.support.design.widget.CoordinatorLayout ,我们需要做的就是找到这两个相互冲突的包,然后把剔除掉其中一个就可以解决问题了。
简单思路:我们先全局搜一下CoordinatorLayout,然后定位这个类的位置,然后再决定如何去处理这个冲突问题。
在这里插入图片描述
点击搜索,或者你可以按快捷键Ctrl+shift+F (windows),全局搜索CoordinatorLayout,然后我们找到了两个相同类在不同的远程依赖包里面。
在这里插入图片描述
在这里插入图片描述
现在问题终于找到了,就是这两个类,虽然不在同一个包下面,但是包名相同,导致他们其实是类冲突了。那么我们就需要找到这两个远程依赖对应的位置,然后剔除掉其中一个就可以了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/305799
推荐阅读
相关标签
  

闽ICP备14008679号