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 引用

相关推荐
唐青枫12 小时前
Java JDBC 实战指南:从 Connection 到事务和连接池
java
一个做软件开发的牛马13 小时前
MyBatis-Plus 从零实战:完整搭建可运行 Demo,BaseMapper 零 SQL、Wrapper 条件构造、分页插件与代码生成器详解
java·后端
用户37215742613513 小时前
Java 处理 PDF 图片:提取 PDF 中的图片,并压缩 PDF 图片体积
java
逐光老顽童13 小时前
Java 与 Kotlin 混合开发避坑指南:30 个真实案例实录
android·kotlin
用户37215742613513 小时前
Java 打印 Word 文档:从基础打印到高级设置
java
爱勇宝1 天前
鸿蒙生态的下半场:开发者不只要能开发,还要能赚钱
android·前端·程序员
Yeyu1 天前
刷新一帧的艺术:invalidate / postInvalidate / postInvalidateOnAnimation全解析
android
用户3521802454751 天前
当 Prompt 学会"热更新":Spring Boot × Nacos3 AI 实战
java·spring boot·ai编程
潘潘潘1 天前
Android OTA 升级原理和流程介绍
android
东坡白菜1 天前
破局全栈:一个前端开发的Java入门实战记录(1)
java·全栈