尝试使用 Paging Library 3 时在 Gradle 中发现重复 类

Duplicate classes found in Gradle when trying to use Paging Library 3

我正在尝试使用 Paging Library 3 实现分页。但是,在完成所有必要步骤(分页源、流程等)后,我无法 运行 我的项目。这是我的依赖项列表:

Duplicate class kotlinx.coroutines.AbstractCoroutine found in modules jetified-kotlinx-coroutines-core-jvm-1.4.1.jar (org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.4.1) and jetified-kotlinx-coroutines-core-jvm-1.4.1.jar (org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1)
Duplicate class kotlinx.coroutines.Active found in modules jetified-kotlinx-coroutines-core-jvm-1.4.1.jar (org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.4.1) and jetified-kotlinx-coroutines-core-jvm-1.4.1.jar (org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1)
Duplicate class kotlinx.coroutines.AwaitAll found in modules jetified-kotlinx-coroutines-core-jvm-1.4.1.jar (org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.4.1) and jetified-kotlinx-coroutines-core-jvm-1.4.1.jar (org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1)
Duplicate class kotlinx.coroutines.AwaitAll$AwaitAllNode found in modules jetified-kotlinx-coroutines-core-jvm-1.4.1.jar (org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.4.1) and jetified-kotlinx-coroutines-core-jvm-1.4.1.jar (org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1)
Duplicate class kotlinx.coroutines.AwaitAll$DisposeHandlersOnCancel found in modules jetified-kotlinx-coroutines-core-jvm-1.4.1.jar (org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.4.1) and jetified-kotlinx-coroutines-core-jvm-1.4.1.jar (org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1)

这不是完整的堆栈跟踪。我的一些依赖项:

// Coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1'
// Paging
    implementation 'androidx.paging:paging-runtime-ktx:3.0.0-alpha12'
 // Image Compressor
    implementation 'id.zelory:compressor:3.0.0' // this lib also uses coroutines

此外,我尝试通过这种方式排除一些依赖项:

implementation 'androidx.paging:paging-runtime-ktx:3.0.0-alpha12' {
        exclude group: 'org.jetbrains.kotlinx', module: 'kotlinx-coroutines-core-jvm'

但它会产生以下错误:

A problem occurred evaluating project ':app'.
> Could not find method androidx.paging:paging-runtime-ktx:3.0.0-alpha12() for arguments [build_cmaofa0fil3wjmmcunq4oc9m5$_run_closure2$_closure8@2e68c056] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

我尝试了 运行 从 raywenderlich 教程下载的示例项目,它 运行 具有相同的依赖关系。迁移到 Paging v2 可以解决问题,但我想使用更新版本中的新功能。

您似乎使用了错误的依赖项。更改为以下行:

implementation "androidx.paging:paging-runtime:3.0.0-alpha12"

你应该这样写依赖,以避免从下面的模块和组中使用。您还可以通过检查错误消息并将其粘贴到此处来检查包含重复 类 的模块 and/or 包。

implementation ('androidx.paging:paging-runtime:3.0.0-alpha12') {
    exclude group: 'org.jetbrains.kotlinx', module: 'kotlinx-coroutines-core-jvm'
}

顺便说一句,我必须使用稳定版的分页。 对我来说,以下解决方案有效。我希望这个解决方案在某些情况下也可能有所帮助。

implementation ('androidx.paging:paging-runtime-ktx:3.0.0') {
        exclude group: 'org.jetbrains.kotlinx'
 }