问题背景
Android Studio 开发时使用到了 GSYVideoPlayer 开源的播放器框架,配置信息如下:
implementation ‘com.shuyu:GSYVideoPlayer:6.0.1‘
在这之前,已经配置了support库:
implementation ‘com.android.support:appcompat-v7:28.0.0‘
运行时报错如下:
Duplicate class android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.ActionBarDrawerToggle found in modules classes.jar (com.android.support:support-core-ui:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.ActionBarDrawerToggle$Delegate found in modules classes.jar (com.android.support:support-core-ui:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.ActionBarDrawerToggle$DelegateProvider found in modules classes.jar (com.android.support:support-core-ui:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.ActionBarDrawerToggle$SlideDrawable found in modules classes.jar (com.android.support:support-core-ui:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.ActivityCompat found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.ActivityCompat$SharedElementCallback21Impl found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.ActivityManagerCompat found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.ActivityOptionsCompat found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.BackStackRecord found in modules classes.jar (com.android.support:support-fragment:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.BackStackRecord$Op found in modules classes.jar (com.android.support:support-fragment:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.BackStackState found in modules classes.jar (com.android.support:support-fragment:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.BackStackState$1 found in modules classes.jar (com.android.support:support-fragment:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.DialogFragment found in modules classes.jar (com.android.support:support-fragment:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) ... Duplicate class android.support.v4.app.NotificationBuilderWithBuilderAccessor found in modules classes.jar (com.android.support:support-compat:28.0.0) and internal_impl-22.2.1.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.NotificationCompat found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.NotificationCompat$Action found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.NotificationCompat$Action$Builder found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.NotificationCompat$Action$Extender found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.NotificationCompat$Action$WearableExtender found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.NotificationCompat$BigPictureStyle found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.NotificationCompat$BigTextStyle found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.NotificationCompat$Builder found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.NotificationCompat$CarExtender found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.NotificationCompat$CarExtender$UnreadConversation found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.NotificationCompat$CarExtender$UnreadConversation$Builder found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.NotificationCompat$Extender found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.NotificationCompat$InboxStyle found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.NotificationCompat$Style found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.NotificationCompat$WearableExtender found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.NotificationCompatExtras found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.app.NotificationCompatJellybean found in modules classes.jar (com.android.support:support-compat:28.0.0) and internal_impl-22.2.1.jar (com.android.support:support-v4:22.2.1) ... Duplicate class android.support.v4.widget.ViewDragHelper$1 found in modules classes.jar (com.android.support:customview:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.widget.ViewDragHelper$2 found in modules classes.jar (com.android.support:customview:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Duplicate class android.support.v4.widget.ViewDragHelper$Callback found in modules classes.jar (com.android.support:customview:28.0.0) and classes.jar (com.android.support:support-v4:22.2.1) Go to the documentation to learn how to Fix dependency resolution errors.
通过报错的内容大概可以猜测出来是播放器使用的V4库和我们配置的V4库出现冲突了,而且是版本冲突。
解决方案
解决方案无非就是移除项目中的 support-v4 或者 GSYVideoPlayer 中的 support-v4 来保持唯一性。
方案1:移除项目中的support-v4
缺点:项目无法使用 support-v4 下的 API
compile(‘com.android.support:appcompat-v7:23.3.0‘) { exclude group: "com.android.support", module: "support-v4" }
方案2:移除 GSYVideoPlayer 中的 support-v4
优点:不影响项目使用 support-v4
缺点:如果项目涉及到多个开源框架出现此问题要在开源框架下个个移除
implementation (‘com.shuyu:GSYVideoPlayer:6.0.1‘) {
exclude module: ‘support-v4‘
exclude group: ‘com.android.support‘
}
一般情况下,我们选择第二种方式来解决冲突,这样更可控一些。