在模块类中发现重复的类android.support.v4.app.INotificationSideChannel?

202

我正在开发一款新闻应用程序,但从Gradle控制台中获得以下错误:

(androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.graphics.drawable.IconCompatParcelizer found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.IResultReceiver found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.ResultReceiver found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.ResultReceiver$1 found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class androidx.core.graphics.drawable.IconCompatParcelizer found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class androidx.core.internal.package-info found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:28.0.0)
Duplicate class androidx.versionedparcelable.CustomVersionedParcelable found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.NonParcelField found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.ParcelField found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.ParcelImpl found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.ParcelImpl$1 found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.ParcelUtils found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcel found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcel$1 found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcel$ParcelException found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcelParcel found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcelStream found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcelStream$FieldBuffer found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcelStream$InputBuffer found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcelable found in modules classes.jar (androidx.versionedparcelable:versionedparcelable:1.0.0) and classes.jar (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable.VersionedParcelize found in modules class

以下是我的app.gradle文件

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    implementation 'com.github.bassaer:chatmessageview:2.0.1'
    implementation 'com.google.code.gson:gson:2.8.5'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

可能是[Execution failed for task ':app:checkDebugDuplicateClasses'. Ionic4 Android]的重复问题,请参考此链接:(https://dev59.com/rek5XIcBkEYKwwoY49sn)。 - Top-Master
你的项目(或其子项目之一)正在使用加号 + 作为依赖项的结尾;请查看 Execution failed for task ':app:checkDebugDuplicateClasses' 的答案(虽然该问题是关于Ionic的,但答案应该适用)。 - Top-Master
14个回答

507

您可以将以下2行代码添加到您的gradle.properties文件中:

android.useAndroidX=true
android.enableJetifier=true

请注意检查,不要重复任何已经存在的行(并确保现有行为“true”)。

详情:

您的项目(或其子项目或依赖项之一)不再使用android.support库,而是使用androidx库,如果与android.support库混合使用会导致冲突。

如果您想在旧项目中使用androidx命名空间库,则需要将编译SDK设置为Android 9.0(API级别28)或更高版本但低于“API级别31”,并将上述两个Android Gradle插件标志都设置为true

android.useAndroidX:当将此标志设置为true时,Android插件将使用适当的AndroidX库而不是Support库。如果未指定,则该标志默认为false

android.enableJetifier:当将此标志设置为true时,Android插件会通过重写其二进制文件自动迁移现有的第三方库以使用AndroidX依赖项。如果未指定,则该标志默认为false


3
你能否增加一些关于这个东西究竟在做什么的信息? - Nathan F.
3
这是因为混合使用了支持库。通过添加这些代码行,AndroidX 被选作你的支持库。 - Anice Jahanjoo
3
这将强制替换所有对android.support.*的依赖关系为androidx.*的依赖关系(但对我来说,这导致编译错误,因为代码中使用了android.support)。如果有人正在寻找解决方法而非解决办法,请查看Execution failed for task ':app:checkDebugDuplicateClasses'的答案(尽管该问题是关于Ionic的,但这个答案应该适用)。 - Top-Master
2
@nicej,我通过选择菜单栏中的“重构”>“迁移到AndroidX”来解决了这个问题。谢谢。 - Pablo Alfonso
2
也许这个解决方案不应该是你们的首选,因为它会减慢项目构建速度,正如文档中所述:“注意:截至2021年底,大多数库生态系统已经原生支持AndroidX。这意味着你的项目很可能已经直接使用AndroidX库,没有必要按照此迁移指南中的步骤进行操作。此外,在本指南中提到的enableJetifier标志可能会导致较慢的构建时间,除非必要,否则不应使用。” - Filipe Bezerra de Sousa
显示剩余14条评论

36
在gradle.properties文件中添加以下这些行可以解决“重复类”错误:
android.useAndroidX=true

android.enableJetifier=true

然而,这给我的项目带来了新的错误:

"package android.support.annotation does not exist"

"cannot find symbol class Fragment"

"package android.support.v4.content does not exist"

"Program type already present"

等等。

然而,重构对我有用:

Migrate your project to AndroidX by selecting Refactor > 
"Migrate to AndroidX" from the menu bar.

Select Build > Clean project

Restore Android Studio

现在,我的项目(Match4app)已经与AndroidX兼容,并且我能够在PlayStore上发布它而没有任何问题。

注:此任务还允许我升级所有依赖于AndroidX的其他库(例如com.firebaseui:firebase-ui-auth:6.0.2,com.google.android.gms:play-services-games:18.0.1,com.google.android.gms:play-services-auth:17.0.0,com.google.android.gms:play-services-ads:18.2.0等)。


15
我遇到了类似的问题,基于我的其他答案

你的项目(或其子项目)正在使用以+(加号)结尾的依赖项,例如com.google.firebase:firebase-auth:+,这意味着尽可能使用更高版本,并且新版本不再使用android.support库,而是使用androidx。为了解决此问题,请按照以下步骤操作,或者按照其他答案更新到androidx

步骤:

  1. 确保已设置 ANDROID_HOME 环境变量,并打开控制台应用程序(但对于 Windows,请使用 git-bash),并 cd 到您的 android 目录(对于 Ionic 项目,它应该是 platforms/android)。

  2. 首先,在控制台中运行以下命令,将所有依赖项列入文件:

    ./gradlew :app:dependencies > my-list.txt
    

    注意:如果上述命令无法正常工作,请将 :app 部分更改为另一个 Gradle 模块的名称。

    可以查看模块名称列表,例如:

    ./gradlew -q projects
    
  3. 在首选文本编辑器中打开生成的 my-list.txt 文件,并搜索 androidx

  4. 如果找到了相关内容,请按照以下步骤操作。否则,您已经完成了!(无需重复这些步骤。)

  5. 向上滚动,直到看到任何一行末尾出现->,例如16.0.8 -> 19.0.0+ -> 19.0.0,这两种情况都意味着该版本已自动解析(由于加了+而被解析为高于您指定的版本)。

  6. 因此,请手动降低版本:

    • 如果可能,请在项目中查找并替换+符号为具体版本。
    • 或者,像以下所述强制特定版本的依赖项。
  7. 最后,重复以上步骤(但只跳过第一步,除非您关闭该控制台)。


要强制指定依赖项的特定版本,请将类似以下内容的内容添加到根build.gradle文件中(这是我的实验),但是当然请编辑以下内容并添加自己的规则(因为这些可能不适用于您的情况):
allprojects {
  // ...
  configurations.all {
    resolutionStrategy {
      force 'com.google.firebase:firebase-common:17.0.0'
      force 'com.google.android.gms:play-services-basement:16.2.0'
      force 'com.google.firebase:firebase-iid:16.0.0'
      force 'com.google.firebase:firebase-auth:17.0.0'
    }
  }
}

14

我也遇到过这个问题,以下是我是如何解决的:

展开Graddle Scripts然后进入graddle.properties文件并添加以下两行:

注意:第一行android.useAndroidX=true可能已经存在,不需要重复添加。

android.useAndroidX=true android.enableJetifier=true


10
只需在gradle.properties文件中添加以下2行:
android.useAndroidX=true

android.enableJetifier=true

现在您的项目不会出现任何重复类错误,而且可以正常工作,

谢谢。


9

已解决 只需将您的Gradle属性设置为以下方式


6

我遇到了同样的问题。

android.enableJetifier=true

只需将此行粘贴到gradleproperties文件中即可。


你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

3
我曾经遇到过同样的问题,这是我解决它的方法。
android.enableJetifier=true

3

enter image description here

取自这里。AndroidX是安卓团队用于开发、测试、打包、版本控制和发布Jetpack库的开源项目。

3
就像@Ahmed所说的那样,解决方案是实现AndroidX,这对我很有效。然而,这并不是一条容易的路,并且需要一点耐心...这些是我所做的步骤:
  • 首先,非常重要的是您在不同的分支中完成所有这些更改或备份您的项目。

  • 您需要拥有Android Gradle插件版本3.5.1。因此,在build.gradle中设置:dependencies { classpath'com.android.tools.build:gradle:3.5.1' ...

  • 使用Android Studio工具迁移到AndroidX: Refactor-->迁移到AndroidX...
  • 当它完成时,它已经完成了所有必要的修改,但可能您无法正确部署项目,因为您发现了任何错误。这些是我发现的问题和解决方案:

  • 如果您使用Kotlin,则在build.gradle中设置:buildscript { ext.kotlin_version ='1.3.10' ...compile"org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

  • 如果您使用Butterknife,请使用10.0.0版本

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接