赞
踩
在安卓开发中,当我们项目中依赖的远程包里面有一模一样的包存在时就会导致包冲突,我们常用的解决方式就是删除任意包里面的包的方式解决冲突。
android studio也告诉了我们如何添加编译依赖项,我们只要按照规范来操作就可以了。
排除不需要的依赖项的写法:
dependencies {
implementation('some-library') {
exclude group: 'com.example.imgtools', module: 'native'
}
}
所以解决包依赖冲突的关键问题是需要找到两个相互冲突的包的绝对路径,也就是说它们各自依赖的远程仓库,然后通过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
这里项目其实已经是编译通过了,但是运行时报了包冲突的问题,As告诉我们在我们的项目中存在多个class android.support.design.widget.CoordinatorLayout ,我们需要做的就是找到这两个相互冲突的包,然后把剔除掉其中一个就可以解决问题了。
简单思路:我们先全局搜一下CoordinatorLayout,然后定位这个类的位置,然后再决定如何去处理这个冲突问题。
点击搜索,或者你可以按快捷键Ctrl+shift+F (windows),全局搜索CoordinatorLayout,然后我们找到了两个相同类在不同的远程依赖包里面。
现在问题终于找到了,就是这两个类,虽然不在同一个包下面,但是包名相同,导致他们其实是类冲突了。那么我们就需要找到这两个远程依赖对应的位置,然后剔除掉其中一个就可以了。
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。