android.support

本文介绍了Android Support库v4、v7和v13的作用及区别。Support库旨在让开发者能在低版本Android上使用高版本的功能,如Fragment、ActionBar等。v4库功能最全,v7依赖于v4并引入了ActionBar,而v13则针对Android 3.2以上的版本。

Android Support V4, V7, V13是什么?

本质上就是三个java library。

为什么要有support库?

如果在低版本Android平台上开发一个应用程序,而应用程序又想使用高版本才拥有的功能,就需要使用Support库。

三个Support 库的区别和作用是什么?

  • Android Support v4 是最早(2011年4月份)实现的库。用在Android1.6 (API lever 4)或者更高版本之上。它包含了相对V7, V13大的多的功能。例如:Fragment,NotificationCompat,LoadBroadcastManager,ViewPager,PageTabAtrip,Loader,FileProvider 等。详细API 参考http://www.android-doc.com/reference/android/support/v4/app/package-summary.html

  • Android Support v7: 这个包是为了考虑Android2.1(API level 7) 及以上版本而设计的,但是v7是要依赖v4这个包的,也就是如果要使用,两个包得同时 被引用。v7支持了Action Bar。

  • Android Support v13:这个包的设计是为了android 3.2及更高版本的,一般我们都不常用,平板开发中能用到。


转载自:http://blog.youkuaiyun.com/hh2000/article/details/39718623

> Task :app:checkDebugDuplicateClasses FAILED AGPBI: {"kind":"warning","text":"Your project has set `android.useAndroidX=true`, but configuration `debugRuntimeClasspath` still contains legacy support libraries, which may cause runtime issues.\nThis behavior will not be allowed in Android Gradle plugin 8.0.\nPlease use only AndroidX dependencies or set `android.enableJetifier=true` in the `gradle.properties` file to migrate your project to AndroidX (see https://developer.android.com/jetpack/androidx/migrate for more info).\nThe following legacy support libraries are detected:\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-compat:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-media-compat:28.0.0-rc02 -> com.android.support:support-annotations:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-compat:28.0.0-rc02 -> com.android.support:collections:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-compat:28.0.0-rc02 -> android.arch.lifecycle:runtime:1.1.1\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-compat:28.0.0-rc02 -> android.arch.lifecycle:runtime:1.1.1 -> android.arch.lifecycle:common:1.1.1\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-compat:28.0.0-rc02 -> android.arch.lifecycle:runtime:1.1.1 -> android.arch.core:common:1.1.1\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-media-compat:28.0.0-rc02 -> com.android.support:versionedparcelable:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-media-compat:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-utils:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-utils:28.0.0-rc02 -> com.android.support:documentfile:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-fragment:28.0.0-rc02 -> com.android.support:loader:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-fragment:28.0.0-rc02 -> com.android.support:loader:28.0.0-rc02 -> android.arch.lifecycle:livedata-core:1.1.1\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-fragment:28.0.0-rc02 -> com.android.support:loader:28.0.0-rc02 -> android.arch.lifecycle:livedata-core:1.1.1 -> android.arch.core:runtime:1.1.1\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-fragment:28.0.0-rc02 -> android.arch.lifecycle:viewmodel:1.1.1\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-utils:28.0.0-rc02 -> com.android.support:localbroadcastmanager:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-utils:28.0.0-rc02 -> com.android.support:print:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02 -> com.android.support:customview:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02 -> com.android.support:viewpager:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02 -> com.android.support:coordinatorlayout:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02 -> com.android.support:drawerlayout:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02 -> com.android.support:slidingpanelayout:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02 -> com.android.support:interpolator:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02 -> com.android.support:swiperefreshlayout:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02 -> com.android.support:asynclayoutinflater:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02 -> com.android.support:cursoradapter:28.0.0-rc02\ndebugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-fragment:28.0.0-rc02","sources":[{}]} Your project has set `android.useAndroidX=true`, but configuration `debugRuntimeClasspath` still contains legacy support libraries, which may cause runtime issues. This behavior will not be allowed in Android Gradle plugin 8.0. Please use only AndroidX dependencies or set `android.enableJetifier=true` in the `gradle.properties` file to migrate your project to AndroidX (see https://developer.android.com/jetpack/androidx/migrate for more info). The following legacy support libraries are detected: debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-compat:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-media-compat:28.0.0-rc02 -> com.android.support:support-annotations:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-compat:28.0.0-rc02 -> com.android.support:collections:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-compat:28.0.0-rc02 -> android.arch.lifecycle:runtime:1.1.1 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-compat:28.0.0-rc02 -> android.arch.lifecycle:runtime:1.1.1 -> android.arch.lifecycle:common:1.1.1 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-compat:28.0.0-rc02 -> android.arch.lifecycle:runtime:1.1.1 -> android.arch.core:common:1.1.1 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-media-compat:28.0.0-rc02 -> com.android.support:versionedparcelable:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-media-compat:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-utils:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-utils:28.0.0-rc02 -> com.android.support:documentfile:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-fragment:28.0.0-rc02 -> com.android.support:loader:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-fragment:28.0.0-rc02 -> com.android.support:loader:28.0.0-rc02 -> android.arch.lifecycle:livedata-core:1.1.1 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-fragment:28.0.0-rc02 -> com.android.support:loader:28.0.0-rc02 -> android.arch.lifecycle:livedata-core:1.1.1 -> android.arch.core:runtime:1.1.1 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-fragment:28.0.0-rc02 -> android.arch.lifecycle:viewmodel:1.1.1 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-utils:28.0.0-rc02 -> com.android.support:localbroadcastmanager:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-utils:28.0.0-rc02 -> com.android.support:print:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02 -> com.android.support:customview:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02 -> com.android.support:viewpager:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02 -> com.android.support:coordinatorlayout:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02 -> com.android.support:drawerlayout:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02 -> com.android.support:slidingpanelayout:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02 -> com.android.support:interpolator:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02 -> com.android.support:swiperefreshlayout:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02 -> com.android.support:asynclayoutinflater:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-core-ui:28.0.0-rc02 -> com.android.support:cursoradapter:28.0.0-rc02 debugRuntimeClasspath -> com.android.support:support-v4:28.0.0-rc02 -> com.android.support:support-fragment:28.0.0-rc02 Execution failed for task ':app:checkDebugDuplicateClasses'. > A failure occurred while executing com.android.build.gradle.internal.tasks.CheckDuplicatesRunnable > Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.3.2-runtime (androidx.core:core:1.3.2) and support-compat-28.0.0-rc02-runtime (com.android.support:support-compat:28.0.0-rc02) Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules core-1.3.2-runtime (androidx.core:core:1.3.2) and support-compat-28.0.0-rc02-runtime (com.android.support:support-compat:28.0.0-rc02) Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules core-1.3.2-runtime (androidx.core:core:1.3.2) and support-compat-28.0.0-rc02-runtime (com.android.support:support-compat:28.0.0-rc02) Duplicate class android.support.v4.graphics.drawable.IconCompatParcelizer found in modules core-1.3.2-runtime (androidx.core:core:1.3.2) and support-compat-28.0.0-rc02-runtime (com.android.support:support-compat:28.0.0-rc02) Duplicate class android.support.v4.os.IResultReceiver found in modules core-1.3.2-runtime (androidx.core:core:1.3.2) and support-compat-28.0.0-rc02-runtime (com.android.support:support-compat:28.0.0-rc02) Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules core-1.3.2-runtime (androidx.core:core:1.3.2) and support-compat-28.0.0-rc02-runtime (com.android.support:support-compat:28.0.0-rc02) Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules core-1.3.2-runtime (androidx.core:core:1.3.2) and support-compat-28.0.0-rc02-runtime (com.android.support:support-compat:28.0.0-rc02) Duplicate class android.support.v4.os.ResultReceiver found in modules core-1.3.2-runtime (androidx.core:core:1.3.2) and support-compat-28.0.0-rc02-runtime (com.android.support:support-compat:28.0.0-rc02) Duplicate class android.support.v4.os.ResultReceiver$1 found in modules core-1.3.2-runtime (androidx.core:core:1.3.2) and support-compat-28.0.0-rc02-runtime (com.android.support:support-compat:28.0.0-rc02) Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules core-1.3.2-runtime (androidx.core:core:1.3.2) and support-compat-28.0.0-rc02-runtime (com.android.support:support-compat:28.0.0-rc02) Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules core-1.3.2-runtime (androidx.core:core:1.3.2) and support-compat-28.0.0-rc02-runtime (com.android.support:support-compat:28.0.0-rc02) Duplicate class androidx.core.graphics.drawable.IconCompatParcelizer found in modules core-1.3.2-runtime (androidx.core:core:1.3.2) and support-compat-28.0.0-rc02-runtime (com.android.support:support-compat:28.0.0-rc02) Duplicate class androidx.core.internal.package-info found in modules core-1.3.2-runtime (androidx.core:core:1.3.2) and support-compat-28.0.0-rc02-runtime (com.android.support:support-compat:28.0.0-rc02) Duplicate class androidx.versionedparcelable.CustomVersionedParcelable found in modules versionedparcelable-1.1.0-runtime (androidx.versionedparcelable:versionedparcelable:1.1.0) and versionedparcelable-28.0.0-rc02-runtime (com.android.support:versionedparcelable:28.0.0-rc02) Duplicate class androidx.versionedparcelable.NonParcelField found in modules versionedparcelable-1.1.0-runtime (androidx.versionedparcelable:versionedparcelable:1.1.0) and versionedparcelable-28.0.0-rc02-runtime (com.android.support:versionedparcelable:28.0.0-rc02) Duplicate class androidx.versionedparcelable.ParcelField found in modules versionedparcelable-1.1.0-runtime (androidx.versionedparcelable:versionedparcelable:1.1.0) and versionedparcelable-28.0.0-rc02-runtime (com.android.support:versionedparcelable:28.0.0-rc02) Duplicate class androidx.versionedparcelable.ParcelImpl found in modules versionedparcelable-1.1.0-runtime (androidx.versionedparcelable:versionedparcelable:1.1.0) and versionedparcelable-28.0.0-rc02-runtime (com.android.support:versionedparcelable:28.0.0-rc02) Duplicate class androidx.versionedparcelable.ParcelImpl$1 found in modules versionedparcelable-1.1.0-runtime (androidx.versionedparcelable:versionedparcelable:1.1.0) and versionedparcelable-28.0.0-rc02-runtime (com.android.support:versionedparcelable:28.0.0-rc02) Duplicate class androidx.versionedparcelable.ParcelUtils found in modules versionedparcelable-1.1.0-runtime (androidx.versionedparcelable:versionedparcelable:1.1.0) and versionedparcelable-28.0.0-rc02-runtime (com.android.support:versionedparcelable:28.0.0-rc02) Duplicate class androidx.versionedparcelable.VersionedParcel found in modules versionedparcelable-1.1.0-runtime (androidx.versionedparcelable:versionedparcelable:1.1.0) and versionedparcelable-28.0.0-rc02-runtime (com.android.support:versionedparcelable:28.0.0-rc02) Duplicate class androidx.versionedparcelable.VersionedParcel$1 found in modules versionedparcelable-1.1.0-runtime (androidx.versionedparcelable:versionedparcelable:1.1.0) and versionedparcelable-28.0.0-rc02-runtime (com.android.support:versionedparcelable:28.0.0-rc02) Duplicate class androidx.versionedparcelable.VersionedParcel$ParcelException found in modules versionedparcelable-1.1.0-runtime (androidx.versionedparcelable:versionedparcelable:1.1.0) and versionedparcelable-28.0.0-rc02-runtime (com.android.support:versionedparcelable:28.0.0-rc02) Duplicate class androidx.versionedparcelable.VersionedParcelParcel found in modules versionedparcelable-1.1.0-runtime (androidx.versionedparcelable:versionedparcelable:1.1.0) and versionedparcelable-28.0.0-rc02-runtime (com.android.support:versionedparcelable:28.0.0-rc02) Duplicate class androidx.versionedparcelable.VersionedParcelStream found in modules versionedparcelable-1.1.0-runtime (androidx.versionedparcelable:versionedparcelable:1.1.0) and versionedparcelable-28.0.0-rc02-runtime (com.android.support:versionedparcelable:28.0.0-rc02) Duplicate class androidx.versionedparcelable.VersionedParcelStream$FieldBuffer found in modules versionedparcelable-1.1.0-runtime (androidx.versionedparcelable:versionedparcelable:1.1.0) and versionedparcelable-28.0.0-rc02-runtime (com.android.support:versionedparcelable:28.0.0-rc02) Duplicate class androidx.versionedparcelable.VersionedParcelable found in modules versionedparcelable-1.1.0-runtime (androidx.versionedparcelable:versionedparcelable:1.1.0) and versionedparcelable-28.0.0-rc02-runtime (com.android.support:versionedparcelable:28.0.0-rc02) Duplicate class androidx.versionedparcelable.VersionedParcelize found in modules versionedparcelable-1.1.0-runtime (androidx.versionedparcelable:versionedparcelable:1.1.0) and versionedparcelable-28.0.0-rc02-runtime (com.android.support:versionedparcelable:28.0.0-rc02) Go to the documentation to learn how to Fix dependency resolution errors. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. Explain with AI 怎么解决上面的问题
12-24
<think>我们遇到的问题是在Android项目构建时出现了重复类(Duplicate classes)的错误,同时项目配置了使用AndroidX,但是仍然检测到了遗留的support库。 错误信息建议我们使用纯AndroidX依赖,或者在gradle.properties中设置`android.enableJetifier=true`来迁移项目到AndroidX。 错误原因分析: 项目中的某些依赖可能同时引入了AndroidX和旧版support库,导致相同的类被重复定义。特别是,我们看到错误中列出了很多重复类,例如: - android.support.v4.app.INotificationSideChannel 和 androidx.core:core中的相同类 - 以及其他重复的类 解决方案: 根据错误提示,有两种方法: 1. 使用纯AndroidX依赖:确保所有依赖都使用AndroidX版本,移除所有旧版support库依赖。 2. 启用Jetifier:在gradle.properties文件中设置`android.enableJetifier=true`,这样Gradle插件会自动将旧版support库转换为使用对应的AndroidX库。 但是,我们注意到错误信息中已经设置了`android.useAndroidX=true`,说明项目已经启用了AndroidX,但是还是混用了support库。因此,我们首先尝试启用Jetifier。 另外,错误信息中还提到了具体的重复类冲突,这些冲突主要是由于`com.android.support:support-v4:28.0.0-rc02`引起的。我们需要检查项目中的依赖,看是否有直接或间接依赖了旧版的support库。 步骤: 1. 在项目的gradle.properties文件中,确保有以下两行: android.useAndroidX=true android.enableJetifier=true # 这一行可能缺失,加上它 2. 检查项目的build.gradle(Module: app)文件,查看dependencies部分,将任何明确使用旧版support库的依赖替换为对应的AndroidX库。 例如,将类似 `implementation 'com.android.support:支持库名称:版本'` 替换为对应的AndroidX依赖。 3. 如果使用了第三方库,而这些库依赖了旧版support库,那么启用Jetifier后,构建时会自动将这些库中的依赖转换为AndroidX。因此,设置`android.enableJetifier=true`通常可以解决这类问题。 4. 如果添加了`android.enableJetifier=true`后仍然出现重复类错误,那么可能是由于多个依赖同时引入了AndroidX和support库,我们需要排除一些传递依赖。 例如,我们可以这样排除传递依赖: implementation('some-library') { exclude group: 'com.android.support', module: 'support-v4' } 5. 执行一次clean和rebuild。 但是,在错误信息中,我们看到重复类出现在`debugRuntimeClasspath`中,并且指向了`com.android.support:support-v4:28.0.0-rc02`和`androidx.core:core:1.3.2`等。因此,我们可能需要排除旧版support库的传递依赖。 具体到我们的错误,我们看到重复类主要来自`support-compat`和`versionedparcelable`等。我们可以尝试在依赖中排除这些。 但是,由于错误信息中提到了很多重复类,而且涉及多个模块,更简单的方法是启用Jetifier,并确保没有直接依赖旧版support库。 让我们按照以下步骤操作: 步骤1:在gradle.properties中添加或修改 android.enableJetifier=true 步骤2:检查并修改依赖 如果问题仍然存在,我们可以通过查看依赖树来找出引入旧版support库的依赖,然后排除它。 在终端运行: ./gradlew app:dependencies --configuration debugRuntimeClasspath 查看哪些依赖引入了旧版support库,然后排除它们。 但是,错误信息已经列出了是`com.android.support:support-v4:28.0.0-rc02`,所以我们可以尝试在所有的依赖中排除这个模块。 例如,如果我们的某个依赖引入了它,我们可以: implementation('com.example:some-library:1.0') { exclude group: 'com.android.support', module: 'support-v4' } 但是,由于我们的项目可能有很多依赖,我们也可以全局排除: configurations { all { exclude group: 'com.android.support', module: 'support-v4' } } 但是注意,全局排除可能会影响其他依赖,所以最好先知道是哪个依赖引入了它,然后单独排除。 然而,根据错误信息,我们还可以看到重复类也出现在`versionedparcelable`中,有androidx版本和support版本。因此,除了排除`support-v4`,可能还需要排除`com.android.support:versionedparcelable`。 但是,启用Jetifier后,理论上这些旧版库会被自动转换成AndroidX,为什么还有重复类呢?可能是因为同时存在转换后的类和原始类?所以,启用Jetifier的同时,我们还需要确保没有同时引入新旧两种依赖。 因此,我们还需要检查build.gradle文件,确保没有任何地方直接引入旧版support库。 例如,我们应该将: implementation 'com.android.support:appcompat-v7:28.0.0' 替换为: implementation 'androidx.appcompat:appcompat:1.0.0' 总结解决方案: 1. 在gradle.properties中设置android.enableJetifier=true 2. 将项目中的旧版support依赖全部替换为AndroidX依赖(如果有的话)。 3. 如果还有问题,通过依赖树检查并排除特定的旧版support库。 如果上述步骤后仍然出现重复类,那么我们可以尝试在构建文件中排除重复的类(不推荐,但作为临时解决方案): packagingOptions { exclude 'META-INF/*' // 或者具体排除重复的类,但这样可能会引发运行时错误,因为类缺失 // 所以最好还是解决依赖冲突 } 但是,根据错误信息,我们更倾向于使用前三个步骤。 代码示例: 修改gradle.properties(项目根目录下的gradle.properties): android.useAndroidX=true android.enableJetifier=true 然后,修改app模块的build.gradle文件,确保依赖都是AndroidX的,例如: 将 implementation 'com.android.support:appcompat-v7:28.0.0' 改为 implementation 'androidx.appcompat:appcompat:1.2.0' 最后,如果还有冲突,排除特定的传递依赖。 由于错误信息中提到了android.arch.lifecycle等,我们可能还需要将Architecture Components的依赖也升级到AndroidX版本。 例如,将: implementation "android.arch.lifecycle:extensions:1.1.1" 改为: implementation "androidx.lifecycle:lifecycle-extensions:2.2.0" 注意:在修改依赖时,需要查看官方文档以确保使用正确的AndroidX包名和版本。 如果问题仍然存在,我们可能需要使用`./gradlew app:dependencies`命令来查看依赖树,然后找出引入冲突库的依赖,并进行排除。 下面是一个排除传递依赖的例子(在app的build.gradle中): implementation ('com.some.library:library-name:1.0.0') { exclude group: 'com.android.support', module: 'support-v4' exclude group: 'com.android.support', module: 'support-compat' // 根据错误信息排除其他模块 } 或者,全局排除(在app的build.gradle中): configurations.all { exclude group: 'com.android.support', module: 'support-v4' exclude group: 'com.android.support', module: 'support-compat' // 注意:全局排除可能会影响其他依赖,请谨慎使用 } 但是,由于我们启用了Jetifier,理论上这些旧库会被转换,所以出现重复类可能是因为在转换过程中,旧库和新库(AndroidX)同时存在。因此,排除旧库应该可以解决问题。 最后,执行Gradle同步,然后Clean项目并重新构建。 如果以上步骤都不能解决问题,可能需要检查项目中的其他模块(如果有多模块)是否也正确配置了AndroidX和Jetifier。 由于错误信息很长,我们可能还需要注意其他重复类,比如versionedparcelable,因此排除的时候也要注意这些模块。 另外,错误信息中提到了`Duplicate class androidx.core.internal.package-info`,这可能是由于core库的重复,所以确保我们只使用一个版本的androidx.core。 我们可以强制统一androidx.core的版本: implementation('androidx.core:core:1.3.2') { force = true } 但是,如果其他库依赖了不同版本,可能会引发其他问题,所以谨慎使用。 综上所述,我们优先尝试以下步骤: 1. 在gradle.properties中设置: android.useAndroidX=true android.enableJetifier=true 2. 将app的build.gradle中的旧版support依赖替换为AndroidX。 3. 排除传递依赖(如果知道是哪个库引入的,就单独排除;否则,可以尝试全局排除com.android.support的所有库): configurations.all { exclude group: 'com.android.support' } 注意:全局排除所有com.android.support可能会有些激进,但如果我们确信所有依赖都有AndroidX替代,那么可以这样做。 4. 同步并清理构建。 5. 如果还有问题,查看依赖树,进行更精准的排除。 由于问题复杂,可能需要多次尝试。 下面给出一个修改gradle.properties和build.gradle的示例: 在gradle.properties中添加(如果不存在): android.useAndroidX=true android.enableJetifier=true 在app的build.gradle中,替换依赖,例如: dependencies { // 替换旧版support库 implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' // ... 其他AndroidX依赖 // 如果有明确的旧版support库依赖,删除或替换 // 如果有第三方库引入了旧版support,排除 implementation ('com.example:example-library:1.0') { exclude group: 'com.android.support' } // 或者全局排除(在dependencies块外面) configurations.all { exclude group: 'com.android.support' } } 注意:全局排除的配置写在build.gradle的最外层(与android和dependencies平级)。 例如,在app的build.gradle中: android { ... } configurations.all { exclude group: 'com.android.support' } dependencies { ... } 但是,请注意,全局排除可能会排除掉所有com.android.support的库,包括那些我们可能还没有完全替换的模块。因此,如果我们已经将所有支持库替换为AndroidX,那么可以这样做。 最后,重新同步并构建。 如果问题解决,那么构建成功;如果还有其他问题,再根据错误信息调整。 </think> 要解决Android构建中的"Duplicate classes"错误和遗留支持库问题,请按照以下步骤操作: ### 解决方案步骤: 1. **启用Jetifier迁移工具** 在项目根目录的 `gradle.properties` 文件中添加以下配置: ```properties android.useAndroidX=true android.enableJetifier=true ``` 2. **替换遗留依赖为AndroidX** 在app模块的 `build.gradle` 中替换所有遗留支持库依赖为对应的AndroidX库: ```gradle dependencies { // 替换前(遗留支持库): // implementation 'com.android.support:appcompat-v7:28.0.0' // implementation 'com.android.support:support-v4:28.0.0' // 替换后(AndroidX): implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.core:core-ktx:1.12.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0' } ``` 3. **排除冲突的传递依赖** 在 `build.gradle` 中添加全局排除规则(放在 `dependencies` 块外): ```gradle configurations.all { exclude group: 'com.android.support', module: 'support-v4' exclude group: 'com.android.support', module: 'support-compat' exclude group: 'com.android.support', module: 'support-media-compat' exclude group: 'com.android.support', module: 'support-fragment' } ``` 4. **检查第三方库依赖** 对于引入冲突的第三方库,添加排除规则: ```gradle implementation('com.example:some-library') { exclude group: 'com.android.support' exclude group: 'android.arch.lifecycle' } ``` 5. **清理并重建项目** 在终端执行: ```bash ./gradlew clean ./gradlew :app:assembleDebug ``` ### 关键问题解释: 1. **Duplicate classes错误**:项目同时引入了AndroidX和旧版支持库(如`androidx.core`和`com.android.support`包含相同类) 2. **Jetifier作用**:自动将旧版支持库的二进制文件转换为AndroidX等效项,解决兼容性问题 3. **依赖排除原理**:`exclude`指令阻止Gradle引入指定的冲突依赖模块 4. **支持库映射**: | 旧支持库 | AndroidX等价库 | |----------|---------------| | support-v4 | androidx.legacy:legacy-support-v4 | | appcompat-v7 | androidx.appcompat:appcompat | | design | com.google.android.material:material | ### 完整配置示例: ```gradle // app/build.gradle android { // ... 其他配置保持不变 } configurations.all { exclude group: 'com.android.support', module: 'support-v4' exclude group: 'com.android.support', module: 'support-compat' } dependencies { implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.core:core-ktx:1.12.0' implementation 'com.google.android.material:material:1.11.0' // 示例:处理有冲突的第三方库 implementation('com.some.library:1.0') { exclude group: 'com.android.support' } } ``` ### 验证修复: 1. 删除项目所有 `build` 文件夹 2.Android Studio中选择 **File > Invalidate Caches / Restart** 3. 重新构建项目,错误应消失
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值