【问题标题】:Duplicate class android.support.v4.app.INotificationSideChannel found in modules classes?在模块类中找到重复的类 android.support.v4.app.INotificationSideChannel?
【发布时间】:2019-09-18 11:00:08
【问题描述】:

我正在开发一个新闻应用程序,但我从 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'
}

【问题讨论】:

标签: gradle chat androidx


【解决方案1】:

您可以在gradle.properties 文件中添加以下两行:

android.useAndroidX=true
android.enableJetifier=true

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


详情:

如果您想在新项目中使用androidx-namespaced 库,您需要将编译 SDK 设置为 Android 9.0(API 级别 28)或更高版本,并将上述两个 Android Gradle 插件标志设置为 true .

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

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

【讨论】:

  • 你能补充一点关于这到底是做什么的信息吗?
  • @NathanF。这是因为混合支持库。通过添加这些行 androidX 选择作为您的支持库
  • 这只会用androidx.* 依赖项强制替换对android.support.* 的任何依赖项(但对我来说,它只是导致编译错误,因为代码中使用了android.support),以防有人正在寻求修复而不是解决方法检查Execution failed for task ':app:checkDebugDuplicateClasses' 的答案(尽管该问题是关于 Ionic 的答案应该有效)。
  • @nicej 我可以通过从菜单栏中选择“重构”>“迁移到 AndroidX”来解决这个问题。谢谢。
  • @MarkT 这只是androidX和你的项目之间的差异,没有更多
【解决方案2】:

将这些行添加到 gradle.properties 文件可以解决“重复类”错误:

android.useAndroidX=true

android.enableJetifier=true

但是,这在我的项目中产生了新的错误:

“包android.support.annotation不存在”

“找不到符号类片段”

“包android.support.v4.content不存在”

“程序类型已经存在”

等等。

但是,重构对我有用:

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 等)。

【讨论】:

    【解决方案3】:

    我也遇到了同样的问题,我就是这样解决的

    展开 Gradle Scripts 然后转到 graddle.properties 并添加这两行

    注意:也许第一行 android.useAndroidX=true 已经存在,所以不要重复。

    android.useAndroidX=true android.enableJetifier=true

    【讨论】:

      【解决方案4】:

      我遇到了这样的事情,下面是基于我的other answer

      您的项目(或其子项目之一)在其末尾使用 + 加号来引用依赖项,例如 com.google.firebase:firebase-auth:+,这意味着尽可能使用任何更高版本,并且较新的版本不再使用android.support 库,而是使用androidx;要解决此问题,请按照以下步骤操作。

      步骤:

      1. 确保设置了ANDROID_HOME 环境变量,然后打开一个控制台窗口(如git-bash,因为它保留了整个命令输出),并将cd 放入项目的android 目录(对于Ionic项目应该是platforms/android)。
      2. 首先,通过以下运行(在 git-bash 中)列出所有依赖项
        ./gradlew :app:dependencies
        
      3. 将结果复制到您喜欢的文本编辑器中,然后搜索androidx
      4. 如果您发现了某些内容,请按照以下步骤操作,否则您就完成了! (您无需重复这些步骤)。
      5. 向上滚动直到您在任何行的末尾看到 ->,例如 16.0.8 -> 19.0.0+ -> 19.0.0,其中两者都表示版本是自动解析的(由于+,版本高于您指定的版本)。
      6. 所以,手动设置版本:
        • 如果可能,请在您的项目(或子项目)中找到 + 符号并将其替换为特定版本。
        • 或者,强制使用特定版本的依赖项,如下所述(当然,在复制依赖项 ID 之后,您在上一步中找到)。
      7. 最后,重复上述步骤(但不是第一步,而是 clear 控制台)。

      强制特定版本的依赖项添加到您的根 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'
          }
        }
      }
      

      【讨论】:

        【解决方案5】:

        取自here。 AndroidX 是 Android 团队用于在 Jetpack 中开发、测试、打包、版本和发布库的开源项目。

        【讨论】:

          【解决方案6】:

          就像@Ahmed 所说,解决方案是实现 AndroidX,它对我有用。然而,这不是一个简单的方法,它需要一点耐心......这些是我所做的步骤:

          • 首先,非常重要您在不同的分支中进行所有这些更改或备份您的项目。

          • 您需要安装 Android Gradle 插件版本 3.5.1。所以,在 build.gradle 中设置:dependencies { classpath 'com.android.tools.build:gradle:3.5.1' ...

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

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

          • 如果使用destination方法,添加“文件”参数:destination file("$reportsDir/checkstyle/checkstyle.xml")
          • 如果你使用Butterknife,请使用10.0.0版本

          【讨论】:

            【解决方案7】:

            只需以这种方式设置您的 Gradle 属性

            【讨论】:

              【解决方案8】:

              只有当您可以降级到特定版本时,Top-Master 的答案才有效。就我而言,我有一个 React Native 应用程序,并且我使用的库之一已迁移到 AndroidX。以前的版本在 iOS 上有问题,所以我不得不使用最新的版本。我必须做的是:

              1. 将 Android 项目迁移到 AndroidX(重构 > 迁移到 AndroidX)。
              2. 使用jetifynode_module 依赖项转换为AndroidX。对于 React Native 0.60 及更高版本,jetify 会自动运行,因此您不必安装此库。

              【讨论】:

                【解决方案9】:

                我遇到了同样的问题。我通过将以下行添加到 gradle.properties 文件中来解决它:

                Gradle 脚本> gradle.properties

                android.useAndroidX=true  
                android.enableJetifier=true
                

                【讨论】:

                • 嗨!欢迎来到 StackOverflow。虽然我知道您试图提供帮助,但该帖子上至少还有 2 个其他答案表明了相同的解决方案。通常,如果“重复”答案比其他答案添加更多内容,则更受欢迎:)
                【解决方案10】:

                只需在 gradle.properties 文件中添加这两行,

                android.useAndroidX=true
                
                android.enableJetifier=true
                

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

                谢谢

                【讨论】:

                  猜你喜欢
                  • 2020-04-16
                  • 1970-01-01
                  • 2019-09-12
                  • 1970-01-01
                  • 1970-01-01
                  • 2020-03-15
                  • 2019-02-02
                  • 2019-01-25
                  • 2020-01-22
                  相关资源
                  最近更新 更多