在模块类中找到重复的类android.support.v4.app.INotificationSideChannel?

Swe*_*gar 44 chat gradle androidx

我正在开发新闻应用程序,但从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
Run Code Online (Sandbox Code Playgroud)

在我的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'
}
Run Code Online (Sandbox Code Playgroud)

nic*_*e j 93

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

android.useAndroidX=true
android.enableJetifier=true
Run Code Online (Sandbox Code Playgroud)

  • @NathanF。这是因为混合了支持库。通过添加这些行androidX选择为您的支持库 (3认同)
  • 您可以添加更多有关此功能的更多信息吗? (2认同)
  • 这只会强制将对android.support。*的任何依赖替换为androidx。*的依赖(但是对我来说,这会导致编译错误,因为代码中使用了android.support),以防万一正在寻求修复而非解决方法,请检查[任务':app:checkDebugDuplicateClasses的执行失败”的答案](/sf/answers/3971567911/)(尽管该问题与Ionic有关,但该答案应该可行)。 (2认同)
  • @nicej 我可以通过从菜单栏中选择“重构”>“迁移到 AndroidX”来解决该问题。谢谢。 (2认同)
  • 也许这个解决方案不应该是您的首选,它往往会减慢项目构建速度,如文档中所述:“注意:截至 2021 年末,大多数库生态系统已经原生支持 AndroidX。这意味着您的项目是很可能已经直接使用 AndroidX 库,无需遵循本迁移指南中的步骤。此外,本指南中提到的 enableJetifier 标志可能会导致构建时间变慢,除非有必要,否则不应使用。 (2认同)

Pab*_*nso 20

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

android.useAndroidX=true

android.enableJetifier=true
Run Code Online (Sandbox Code Playgroud)

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

“包 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
Run Code Online (Sandbox Code Playgroud)

现在,我的项目 (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 等)。


Shr*_*sey 10

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

android.useAndroidX=true

android.enableJetifier=true
Run Code Online (Sandbox Code Playgroud)

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

谢谢


小智 10

我遇到过同样的问题。

android.enableJetifier=true

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


Top*_*ter 9

I run into something like this, and below is based on my other answer:

Your project (or one of its sub-projects) is referring to a dependency using the + plus-sign at its end, like com.google.firebase:firebase-auth:+, which means, use any higher version when possible, and that newer version is no longer using android.support libraries and instead is using androidx; to fix this issue follow the below steps.

Steps:

  1. Ensure the ANDROID_HOME environment-variable is set, and then, open a console window (like git-bash, because it keeps the whole command output), and cd into your android directory (for Ionic projects it should be platforms/android).
  2. First, List all dependencies by running below (in git-bash):
    ./gradlew :app:dependencies
    
    Run Code Online (Sandbox Code Playgroud)
  3. Copy the result into your preferred text-editor, and search for androidx.
  4. If you found something follow below steps, else you are done! (and you do not need to repeat these steps).
  5. Scroll up until you see -> at the end of any line, like for example 16.0.8 -> 19.0.0 or + -> 19.0.0, which both mean that the version was auto-resolved (to something higher than specified by you because of +).
  6. So, set the version down manually:
    • When possible, in your project (or sub-project) find and replace the + sign with a specific version.
    • Or, force a specific version of the dependencies like mentioned below.
  7. At last, repeat above steps (but instead of step one just clear the console).

To Force specific version of the dependencies add to your root build.gradle file something like below (which is what worked for me) but of course edit below and add your own rules (because these might not work for your case):

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'
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 9

我遇到了同样的问题,这就是我解决它的方法

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

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

android.useAndroidX=true android.enableJetifier=true


ili*_*ocn 9

解决了 只需这样设置你的 Gradle 属性即可