报错:Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.8.0-.....

报错信息:

Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.IResultReceiver found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.ResultReceiver found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.ResultReceiver$1 found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules core-1.8.0-runtime (androidx.core:core:1.8.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)

Go to the documentation to learn how to Fix dependency resolution errors.

解决方法:

在项目的gradle.properties文件里面加上

# 启用AndroidX
android.useAndroidX=true
# 启用Jetifier,自动将旧的支持库依赖项转换为AndroidX依赖项‌
android.enableJetifier=true

通常项目自带第一句,只需要加第二句

<think>我们面对的问题是构建时出现重复类错误:Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.9.0.aar (androidx.core:core:1.9.0) and support-v4-22.1.1.aar (com.android.support:support-v4:22.1.1)。这是因为项目中同时使用了AndroidX和旧版支持库(Support Library)的相同类,导致冲突。 根据引用[2][4][5],解决此问题的关键是迁移到AndroidX并确保项目中没有混合使用支持库和AndroidX。以下是具体步骤: ### 解决步骤 1. **启用AndroidX和Jetifier** 在项目根目录的`gradle.properties`文件中添加以下两行: ```properties # 启用AndroidX android.useAndroidX=true # 启用Jetifier(自动转换第三方库中的支持库为AndroidX) android.enableJetifier=true ``` 这样设置后,Gradle在构建时会自动将依赖项中的支持库类转换为对应的AndroidX类[^4][^5]。 2. **迁移项目到AndroidX** 在Android Studio中,可以一键迁移: - 菜单栏选择 `Refactor` > `Migrate to AndroidX...` - 工具会自动检查并迁移代码和资源文件(备份项目以防万一)。 3. **检查并统一依赖项** 确保所有依赖库都使用AndroidX版本,替换掉所有`com.android.support`依赖: ```gradle dependencies { // 移除旧版支持库依赖 // implementation 'com.android.support:support-v4:22.1.1' // 删除此行 // 使用AndroidX替代库 implementation 'androidx.core:core:1.9.0' // 已存在则无需重复添加 } ``` 4. **排除冲突(可选)** 如果某个第三方库尚未支持AndroidX,可使用`exclude`排除其传递依赖中的支持库[^3]: ```gradle implementation ('some.library:1.0.0') { exclude group: 'com.android.support', module: 'support-compat' } ``` 5. **清理并重建项目** - 执行 `Build` > `Clean Project` - 再执行 `Build` > `Rebuild Project` ### 原理说明 - `android.useAndroidX=true`:强制项目使用AndroidX包名(`androidx.*`)而非支持库(`android.support.*`)。 - `android.enableJetifier=true`:自动重写二进制文件,将第三方库中的`android.support.*`引用转换为`androidx.*`。 ### 验证解决 构建成功后,检查`gradle`控制台输出,确保不再出现`Duplicate class`错误。 > **注意**:若迁移后出现其他兼容性问题,需检查第三方库是否已支持AndroidX。可通过[官方迁移文档](https://developer.android.com/jetpack/androidx/migrate)进一步排查。 §§相关问题§§ 1. 如何检测项目中哪些库尚未迁移到AndroidX? 2. 迁移到AndroidX后出现界面错位或功能异常怎么办? 3. Jetifier转换依赖的原理是什么? 4. 为什么AndroidX能解决兼容性问题? [^4]: 迁移安卓项目到AndroidX报错解决方案 [^5]: 报错Duplicate class ... 的解决属性设置 </think> ### 解决 Android 构建中的 "Duplicate class" 错误 该错误是由于项目中同时存在 AndroidX (`androidx.core:core`) 和旧版支持库 (`com.android.support:support-v4`) 导致类冲突。以下是完整解决方案: #### 🔧 解决方案步骤 1. **启用 AndroidX 和 Jetifier** 在项目根目录的 `gradle.properties` 文件中添加: ```properties # 强制使用 AndroidX android.useAndroidX=true # 自动转换旧版支持库为 AndroidX android.enableJetifier=true ``` 这是解决冲突的核心步骤,让 Gradle 自动处理库迁移[^4]。 2. **统一依赖库版本** 在 `app/build.gradle` 中: ```gradle dependencies { // 移除旧版支持库 // implementation 'com.android.support:support-v4:22.1.1' // 删除此行 // 使用 AndroidX 替代 implementation 'androidx.core:core:1.9.0' implementation 'androidx.appcompat:appcompat:1.6.1' } ``` 3. **检查传递依赖冲突** 使用命令检查依赖树: ```bash ./gradlew app:dependencies ``` 查找是否有模块引入了旧版支持库。 4. **排除冲突模块**(如果步骤3发现冲突源) ```gradle implementation ('com.example:some-library:1.0') { exclude group: 'com.android.support', module: 'support-v4' } ``` 5. **清理并重建项目** ```bash ./gradlew clean ./gradlew build ``` #### ⚠️ 重要注意事项 1. **迁移到 AndroidX** 如果项目尚未迁移: -Android Studio 中选择:`Refactor > Migrate to AndroidX` - 更新所有 `import` 语句(`android.support.v4` → `androidx.core`) 2. **版本兼容性** 确保所有库使用兼容版本: ```gradle // 正确示例 implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.recyclerview:recyclerview:1.3.0' ``` 3. **Jetifier 工作原理** 当 `android.enableJetifier=true` 时: ```mermaid graph LR A[旧版支持库] --> B(Jetifier转换器) B --> C[AndroidX等效库] ``` 自动重写字节码中的包名 (`android.support` → `androidx`)。 > **常见误区**:仅添加 `exclude` 不能根本解决冲突,必须启用 Jetifier 完成完整迁移[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值