Android 开发问题:项目同时引入了两个包含相同类文件的库(AndroidX 库、旧版本支持库),导致了重复类错误

groovy 复制代码
implementation 'com.pes.materialcolorpicker:library:1.2.5'
java 复制代码
ColorPicker colorPicker = new ColorPicker(this);

colorPicker.setColor(Color.RED);

colorPicker.setCallback(color -> {
    colorPicker.dismiss();
    Log.i(TAG, "color = " + color);
});

colorPicker.show();
  • 在 Android 开发中,引入 ColorPicker 后,构建项目时,出现如下错误信息

    Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.1-runtime (com.android.support:support-compat:25.3.1)
    Duplicate class android.support.v4.app.INotificationSideChannelStub found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.1-runtime (com.android.support:support-compat:25.3.1) Duplicate class android.support.v4.app.INotificationSideChannelStubProxy found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.1-runtime (com.android.support:support-compat:25.3.1) Duplicate class android.support.v4.os.IResultReceiver found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.1-runtime (com.android.support:support-compat:25.3.1) Duplicate class android.support.v4.os.IResultReceiverStub found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.1-runtime (com.android.support:support-compat:25.3.1)
    Duplicate class android.support.v4.os.IResultReceiverStubProxy found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.1-runtime (com.android.support:support-compat:25.3.1)
    Duplicate class android.support.v4.os.ResultReceiver found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.1-runtime (com.android.support:support-compat:25.3.1)
    Duplicate class android.support.v4.os.ResultReceiver1 found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.1-runtime (com.android.support:support-compat:25.3.1) Duplicate class android.support.v4.os.ResultReceiverMyResultReceiver found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.1-runtime (com.android.support:support-compat:25.3.1)
    Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules core-1.9.0-runtime (androidx.core:core:1.9.0) and support-compat-25.3.1-runtime (com.android.support:support-compat:25.3.1)

    解读

    在模块 core-1.9.0-runtime(androidx.core:core:1.9.0)
    和 support-compat-25.3.1-runtime(com.android.support:support-compat:25.3.1)
    中发现重复的类 android.support.v4.app.INotificationSideChannel

    ...

问题原因
  • 项目同时引入了两个包含相同类文件的库,导致了重复类错误,冲突双方如下
  1. AndroidX 库(新版本支持库):androidx.core:core:1.9.0

  2. 旧版本支持库:com.android.support:support-compat:25.3.1

处理策略
  • gradle.properties 文件中添加如下内容
properties 复制代码
android.useAndroidX=true
android.enableJetifier=true
  1. android.useAndroidX=true:表示项目使用 AndroidX 库

  2. android.enableJetifier=true:表示允许 Gradle 自动重写旧的第三方库的二进制文件,将其中的旧支持库引用转换为 AndroidX 引用

相关推荐
万法若空1 小时前
【算法-查找】查找算法
java·数据结构·算法
HZZD_HZZD3 小时前
DL/T 645-2026新国标深度解读与智能电表协议适配实战:从帧结构变化到Java采集器升级的全链路改造方案
java·开发语言
山登绝顶我为峰 3(^v^)36 小时前
C/C++ 交叉编译方法
java·c语言·c++
一叶飘零_sweeeet6 小时前
Codex 与 Claude Code 深度拆解:两代 AI 编程智能体的技术本质与 Java 实战指南
java·ai·codex·claude code
Listen·Rain6 小时前
提示词和提示词模板
java·人工智能·spring boot
wuqingshun3141597 小时前
重写equals而不重写hashCode,会出什么问题?
java·开发语言
我今晚不熬夜7 小时前
Java语言实现Modbus协议通过用串口读取数据(以RTU为例)
java·网络协议
小锋java12348 小时前
Maven 4 要来了:15 年后,Java 构建工具迎来"彻底重构"
java·maven
卓怡学长8 小时前
w269基于spring boot + vue 候鸟监测数据管理系统
java·数据库·spring boot·spring·intellij-idea