android.support迁移到androidx

本文详细介绍从Android支持库迁移到AndroidX的过程,包括依赖库对比、类对比,以及迁移过程中可能遇到的问题与解决方法。

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

前言

Google 2018 IO 大会推出了 Android新的扩展库 AndroidX,用于替换原来的 Android扩展库,将原来的android.替换成androidx.;只有包名和Maven工件名受到影响,原来的类名,方法名和字段名不会更改。官方早就推荐将support库迁移到androidx,因为后续support库不会再做更新。
迁移时只需要3.2版本及以上的android studio,在菜单refactor中点击migrate to androidx即可,该向导会提示需要做的更新。其中包括gradle版本至少3.2以上,compileSdkVersion 版本28以上。
1、常用依赖库对比

Old build artifactAndroidX build artifact
com.android.support:appcompat-v7:28.0.2androidx.appcompat:appcompat:1.0.0
com.android.support:design:28.0.2com.google.android.material:material:1.0.0
com.android.support:support-v4:28.0.2androidx.legacy:legacy-support-v4:1.0.0
com.android.support:recyclerview-v7:28.0.2androidx.recyclerview:recyclerview:1.0.0
com.android.support.constraint:constraint-layout:1.1.2androidx.constraintlayout:constraintlayout:1.1.2

2、常用支持库类对比

Support Library classAndroidX class
android.support.v4.app.Fragmentandroidx.fragment.app.Fragment
android.support.v4.app.FragmentActivityandroidx.fragment.app.FragmentActivity
android.support.v7.app.AppCompatActivityandroidx.appcompat.app.AppCompatActivity
android.support.v7.app.ActionBarandroidx.appcompat.app.ActionBar
android.support.v7.widget.RecyclerViewandroidx.recyclerview.widget.RecyclerView

了解androidX可以看这一篇:androidX了解一下
更多androidX详细内容看:官方文档

关于gradle低版本升级可以看这篇文章:https://blog.youkuaiyun.com/u013183608/article/details/89428563

替换成androidx后遇到的问题

1、对findViewById的引用不明确

描述:Activity 中的方法 findViewById(int) 和 AppCompatActivity 中的方法 findViewById(int) 都匹配其中, T是类型变量:T扩展已在方法 findViewById(int)中声明的View

解决方法:compileSdkVersion不一致,应该进行统一

2、v4jar包keyeventcompat不存在

描述:错误: 找不到符号
符号: 类 KeyEventCompat
位置: 程序包 androidx.core.view

原因:KeyEventCompat类被取消了hasNoModifiers方法,而该方法已经被KeyEvent实现了
原代码:

		if (KeyEventCompat.hasNoModifiers(event)) {
		     handled = arrowScroll(FOCUS_FORWARD);
		} else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
		     handled = arrowScroll(FOCUS_BACKWARD);
		}

修改后代码

		if (event.hasNoModifiers()) {
		    handled = arrowScroll(FOCUS_FORWARD);
		} else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) {
		    handled = arrowScroll(FOCUS_BACKWARD);
		}

3、ERROR: [TAG] Failed to resolve variable ‘${junit.version}’

解决方法:file->Invalidate Caches / restart

4、Error:error: resource previously defined here.

原因:自定义View时自定义属性使用了系统的属性名称
解决方案:
1、重命名自已定义的属性,以避免与系统的属性名称重复

        <declare-styleable name="TestTextView">
                <attr name="text_color" format="color"/>
                <attr name="text_size" format="float" />
        </declare-styleable>

2、不对textColor进行重命名,直接引用系统的textColor,然后在xml里面使用 时,就不能使用自定义的命名空间了(例如:app:),得用使用原生的引用(android:)

	    <declare-styleable name="TestTextView">
	            <attr name="textColor"/>
	            <attr name="textSize"/>
	    </declare-styleable>
引入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`,这表明项目混合了AndroidXandroidx.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、付费专栏及课程。

余额充值