com.android.support冲突的解决办法

本文介绍了当在Android项目中遇到com.android.support库版本冲突时的两种解决方案:一是调整项目中支持库版本与依赖库一致;二是排除第三方库中特定的支持库依赖,以确保项目的顺利构建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自: https://blog.csdn.net/yuzhiqiang_1993/article/details/78214812

错误:All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes

当我们使用android studio添加一些第三方的依赖库时,很可能会提示上面这个错误。

大致意思就是com.android.support的包版本号要保持一致,但是可能我们自己新建的项目的com.android.support包版本号要高一些,一些第三方的库的com.android.support可能没有及时更新support库,就会出现这个错误。

解决方法(同样的适用于其他的依赖冲突。) 
1)修改自己项目中的com.android.support的版本号,与所依赖的库版本号一致,但是当我们依赖的库中的com.android.support版本号有好几个版本就不行了。(不推荐)

2)推荐这种方法,如果发生冲突了,依赖第三方库时候排除掉对com.android.support包的依赖,这样自己的项目随便依赖什么版本都可以。

exclude group:表示只要包含com.android.support的都排除 
api是android studio3.0中新的依赖方式,对依赖方式还不熟悉的话可以看这篇文章:Android Studio3.0新的依赖方式

例如:

    api("com.afollestad.material-dialogs:core:0.9.5.0") {
        exclude group: 'com.android.support'
    }
  •  

module:删排除group中的指定module 
例如:

    api("com.afollestad.material-dialogs:core:0.9.5.0") {
        exclude group: 'com.android.support', module: 'support-v13'
        exclude group: 'com.android.support', module: 'support-vector-drawable'
    }
  •  

另外还有一个建议,在我们自己创建library给别人使用时,如果需要依赖com.android.support的话,建议用provided的方式依赖(android studio3.0中更改为compileOnly),这样只会在编译时有效,不会参与打包。以免给使用者带来不便。

例:

    provided 'com.android.support:appcompat-v7:26.1.0'
    provided 'com.android.support:design:26.1.0'
    provided 'com.android.support:support-vector-drawable:26.1.0'
引入design support库报错:Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.9.0.aar -> core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-24.2.1.aar -> support-compat-24.2.1-runtime (com.android.support:support-compat:24.2.1) Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules core-1.9.0.aar -> core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-24.2.1.aar -> support-compat-24.2.1-runtime (com.android.support:support-compat:24.2.1) Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules core-1.9.0.aar -> core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-24.2.1.aar -> support-compat-24.2.1-runtime (com.android.support:support-compat:24.2.1) Duplicate class android.support.v4.os.IResultReceiver found in modules core-1.9.0.aar -> core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-24.2.1.aar -> support-compat-24.2.1-runtime (com.android.support:support-compat:24.2.1) Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules core-1.9.0.aar -> core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-24.2.1.aar -> support-compat-24.2.1-runtime (com.android.support:support-compat:24.2.1) Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules core-1.9.0.aar -> core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-24.2.1.aar -> support-compat-24.2.1-runtime (com.android.support:support-compat:24.2.1) Duplicate class android.support.v4.os.ResultReceiver found in modules core-1.9.0.aar -> core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-24.2.1.aar -> support-compat-24.2.1-runtime (com.android.support:support-compat:24.2.1) Duplicate class android.support.v4.os.ResultReceiver$1 found in modules core-1.9.0.aar -> core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-24.2.1.aar -> support-compat-24.2.1-runtime (com.android.support:support-compat:24.2.1) Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules core-1.9.0.aar -> core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-24.2.1.aar -> support-compat-24.2.1-runtime (com.android.support:support-compat:24.2.1) Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules core-1.9.0.aar -> core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-24.2.1.aar -> support-compat-24.2.1-runtime (com.android.support:support-compat:24.2.1)
最新发布
07-12
<think>我们正在处理一个Android开发中的依赖冲突问题。用户引入了design support库,但遇到了Duplicate class错误,涉及androidx.core和com.android.support冲突。这通常是因为项目同时使用了AndroidX和旧版Support库,导致类重复。 根据引用[3]和引用[4],我们可以通过排除冲突的模块或统一使用AndroidX来解决。由于用户提到的是design support库,我们需要注意design库的版本可能依赖于旧版support库,而项目中可能已经部分迁移到了AndroidX。 解决方案步骤: 1. 尝试迁移到AndroidX(如果项目允许)。Android Studio提供了迁移选项:Refactor > Migrate to AndroidX。 2. 如果暂时不能迁移,则需确保所有support库版本一致,并排除重复的类。 但是,用户错误信息中提到了`Duplicate class androidx.core`和`com.android.support`,这表明项目混合了AndroidX(androidx.core)和Support库(com.android.support)。因此,我们需要将整个项目统一到AndroidX,或者将design库也降级到与support库一致的版本(但design库的新版本可能已经依赖AndroidX)。 由于design库在28.0.0版本之后就不再更新(后续被Material Design组件库替代),而AndroidX是未来的方向,建议迁移到AndroidX并使用新的Material Design库。 具体操作: 方案一:迁移到AndroidX 1. 在gradle.properties文件中添加: ``` android.useAndroidX=true android.enableJetifier=true ``` 2. 使用Android Studio的迁移工具:Refactor > Migrate to AndroidX。 3. 将design库替换为Material Design库(com.google.android.material:material),因为新的Material组件库是AndroidX的一部分。 方案二:排除冲突(临时解决,不推荐长期使用) 如果无法立即迁移,可以尝试排除design库中引入的support-v4模块,但注意这可能导致功能缺失。 在build.gradle中: ```groovy implementation ('com.android.support:design:28.0.0') { exclude group: 'com.android.support', module: 'support-v4' // 根据错误信息排除其他重复的模块 } ``` 但是,由于错误信息中出现了androidx.core,说明项目中已经有AndroidX的库,所以排除可能无法完全解决问题。因此,强烈建议迁移到AndroidX。 根据引用[2],也可以参考官方文档解决重复类的问题:https://developer.android.com/studio/build/dependencies#duplicate_classes 综上所述,最佳方案是迁移到AndroidX并使用新的Material Design组件库。 相关问题: 1. 如何将Android项目迁移到AndroidX? 2. Material Design组件库如何引入和使用? 3. 如何解决Android依赖冲突的其他常见方法?</think>### Android 引入 Design Support 库时报重复类冲突的解决方案 您遇到的 `Duplicate class androidx.core` 和 `com.android.support` 冲突是由于项目同时使用了 AndroidX 和旧版 Support 库造成的混合依赖问题。以下是分步解决方案: #### ✅ 最佳解决方案:迁移到 AndroidX 1. **在 `gradle.properties` 添加配置**: ```properties android.useAndroidX=true android.enableJetifier=true ``` 2. **修改 `build.gradle` 依赖**: ```groovy dependencies { // 替换旧版 Design Support 库为 Material Components implementation 'com.google.android.material:material:1.6.0' // 确保其他库也使用 AndroidX implementation 'androidx.appcompat:appcompat:1.4.2' } ``` 3. **执行清理操作**: -Android Studio 中选择 **Refactor > Migrate to AndroidX** - 清理项目:`Build > Clean Project` + `Build > Rebuild Project` #### ⚠️ 临时解决方案(不推荐长期使用) 如果暂时无法迁移,可尝试排除冲突模块: ```groovy implementation ('com.android.support:design:28.0.0') { exclude group: 'com.android.support', module: 'support-v4' exclude group: 'com.android.support', module: 'support-compat' } ``` #### 🔍 验证是否解决 检查构建输出中是否还有以下错误: ``` Duplicate class androidx.core... Duplicate class com.android.support... ``` 若错误消失,说明冲突已解决[^2][^3]。 #### ⚙️ 预防措施 1. 统一所有依赖库的版本号 2. 定期运行 `./gradlew app:dependencies` 检查依赖树 3. 避免同时使用 Support 库和 AndroidX 库 > 建议优先采用 AndroidX 迁移方案,这是 Google 官方推荐的解决路径,详见 [Android 官方依赖管理指南](https://developer.android.com/studio/build/dependencies)[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值