执行任务':app:checkDebugDuplicateClasses'失败。Ionic4 Android

63

我目前正在开发一个ionic4应用,但最近在向Android真实设备构建应用程序时添加https://ionicframework.com/docs/native/fcm插件后,应用程序停止工作。

起初,它抱怨fabric密钥,然而,我从未打算在我的应用程序中使用fabric crashlytic。

错误日志:

* What went wrong:
Execution failed for task ':app:checkDebugDuplicateClasses'.
> 1 exception was raised by workers:
  java.lang.RuntimeException: Duplicate class android.support.v4.app.INotificationSideChannel found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.IResultReceiver found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.ResultReceiver found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.ResultReceiver$1 found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)

  Go to the documentation to learn how to <a href="d.android.com/r/tools/classpath-sync-errors">Fix dependency resolution errors</a>.


* 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.

* Get more help at https://help.gradle.org

BUILD FAILED in 21s

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.4.1/userguide/command_line_interface.html#sec:command_line_warnings
35 actionable tasks: 5 executed, 30 up-to-date
cmd: Command failed with exit code 1 Error output:
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:checkDebugDuplicateClasses'.
> 1 exception was raised by workers:
  java.lang.RuntimeException: Duplicate class android.support.v4.app.INotificationSideChannel found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.IResultReceiver found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.ResultReceiver found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.ResultReceiver$1 found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)
  Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:27.1.1)

  Go to the documentation to learn how to <a href="d.android.com/r/tools/classpath-sync-errors">Fix dependency resolution errors</a>.


* 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.

* Get more help at https://help.gradle.org

BUILD FAILED in 21s
[ERROR] An error occurred while running subprocess cordova.

        cordova run android exited with exit code 1.

        Re-running this command with the --verbose flag may provide more information.

android/app/build.gradle

buildscript {
    repositories {
        google()
        jcenter()
        maven { url 'https://maven.fabric.io/public' } // Fabrics Maven repository from cordova-plugin-firebase
    }
    dependencies {

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.android.tools.build:gradle:3.4.0+'
        classpath 'com.google.gms:google-services:4.2.0' // google-services dependency from cordova-plugin-firebase
        classpath 'io.fabric.tools:gradle:1.+' // fabric dependency from cordova-plugin-firebase
    }
}

allprojects {
    repositories {
      
        jcenter()
        google() // Google's Maven repository from cordova-plugin-firebase
    }
    //This replaces project.properties w.r.t. build settings
    project.ext {
      defaultBuildToolsVersion="28" //String
      defaultMinSdkVersion=19 //Integer - Minimum requirement is Android 4.4
      defaultTargetSdkVersion=28 //Integer - We ALWAYS target the latest by default
      defaultCompileSdkVersion=28 //Integer - We ALWAYS compile with the latest by default
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

cordova-plugin-firebase/raihan-build.gradle

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
       classpath 'com.android.tools.build:gradle:3.4.0+'
        classpath 'com.google.gms:google-services:4.2.0'
    }
}


dependencies {
    compile 'me.leolin:ShortcutBadger:1.1.4@aar'
    compile 'com.google.firebase:firebase-auth:+'
    compile('com.crashlytics.sdk.android:crashlytics:2.9.1@aar') {
       transitive = true
    }
}
cdvPluginPostBuildExtras.add({
    // apply plugin: 'com.google.gms.google-services'
    apply plugin: 'io.fabric'
})

cordova-plugin-fcm-with-dependecy-updated/raihan-FCMPlugin.gradle

buildscript {
    repositories {
       google()
        jcenter()
        mavenLocal()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.0+'
        classpath 'com.google.gms:google-services:4.2.0'
    }
}
repositories {
    google()
    jcenter()
}
dependencies {
    compile 'com.google.firebase:firebase-core:16.0.8'
}
// apply plugin: 'com.google.gms.google-services'
// class must be used instead of id(string) to be able to apply plugin from non-root gradle file
// apply plugin: com.google.gms.googleservices.GoogleServicesPlugin

android/project.properties

 target=android-27
    android.library.reference.1=CordovaLib
    android.library.reference.2=app
    cordova.gradle.include.1=cordova-android-support-gradle-release/raihan-cordova-android-support-gradle-release.gradle
    cordova.system.library.1=com.squareup.okhttp3:okhttp-urlconnection:3.10.0
    cordova.system.library.2=com.android.support:support-v4:24.1.1+
    cordova.system.library.3=com.soundcloud.android:android-crop:1.0.0@aar
    cordova.system.library.4=com.google.firebase:firebase-core:16.0.8
    cordova.system.library.4=com.google.firebase:firebase-iid:17.1.1
    cordova.system.library.5=com.google.firebase:firebase-messaging:17.5.0
    cordova.gradle.include.2=cordova-plugin-fcm-with-dependecy-updated/raihan-FCMPlugin.gradle
    cordova.gradle.include.3=cordova-support-google-services/raihan-build.gradle
    cordova.system.library.6=com.google.firebase:firebase-auth:16.2.0
    cordova.system.library.6=com.google.firebase:firebase-perf:16.2.4
    cordova.system.library.7=com.android.support:support-annotations:27.+
    cordova.system.library.8=com.android.support:appcompat-v7:23+
    cordova.system.library.8=com.google.firebase:firebase-iid:17.1.1
    cordova.system.library.8=com.google.android.gms:play-services-tagmanager:16.0.8
    cordova.system.library.8=com.google.firebase:firebase-config:16.4.0
    cordova.gradle.include.4=cordova-plugin-telerik-imagepicker/raihan-ignorelinterrors.gradle
    cordova.gradle.include.5=cordova-plugin-telerik-imagepicker/raihan-androidtarget.gradle
    cordova.gradle.include.6=cordova-android-support-gradle-release/raihan-cordova-android-support-gradle-release.gradle
    cordova.gradle.include.7=cordova-plugin-firebase/raihan-build.gradle

我已经提供了上述文件,请告诉我是否还需要上传其他内容。
19个回答

57
  1. 进入gradle.properties(项目属性)

  2. 添加
    android.enableJetifier=true

  3. 大多数情况下,android.useAndroidX=true已存在。请检查gradle.properties(项目属性),如果不存在,请添加
    android.useAndroidX=true -> 看起来像这个图片所示:

enter image description here


请查看有关更多详细信息的类似完整答案 - Top-Master
谢谢,android.enableJetifier=true对我很有帮助。 - New_developer

24

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

步骤:

  1. 请确保设置了 ANDROID_HOME 环境变量,然后打开控制台(对于 Windows,使用 git-bash),并切换到你的 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'
    }
  }
}

3
那就是对我起作用的解决方案,你救了我的一天。 - Abdu4
3
我使用 Cordova 8 的一个可行替代方案是启用 AndroidX 支持,方法是添加 https://github.com/dpa99c/cordova-plugin-androidx,并且再添加 https://github.com/dpa99c/cordova-plugin-androidx-adapter,这是一个更新依赖项的 shim。 - MStoner
你可以通过执行 ./gradlew :app:dependencies >> ~/output && code ~/output 命令将输出轻松添加到文件中。 - Alita
1
我永远感激不尽。我不得不删除所有的androidx依赖项(几乎都是由firebase和其他一些东西引起的)。我还注意到有很多顶级androidx依赖项(好像我包含了它们),但当我禁用firebase时它们就消失了。它们是某种“反射”。此外,当“multiDexEnabled”为true时,Android Studio会隐式地使用multidex。只需使用不依赖于androidx的1.0.3 multidex即可。重复此过程,直到您没有任何androidx依赖项,然后您就完成了。 - ci0ccarellia

17
我遇到的问题与 React Native 中的某些类似。
Execution failed for task ':app:checkDebugDuplicateClasses'.

工作人员引发了1个异常: java.lang.RuntimeException: java.lang.RuntimeException: 在jetified-kotlin-stdlib-1.8.0.jar(org.jetbrains.kotlin:kotlin-stdlib:1.8.0)和jetified-kotlin-stdlib-jdk8-1.6.21.jar(org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.21)模块中找到重复的类kotlin.collections.jdk8.CollectionsJDK8Kt。 在jetified-kotlin-stdlib-1.8.0.jar(org.jetbrains.kotlin:kotlin-stdlib:1.8.0)和jetified-kotlin-stdlib-jdk7-1.6.21.jar(org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.21)模块中找到重复的类kotlin.internal.jdk7.JDK7PlatformImplementations。这意味着Kotlin标准库的两个不同版本之间存在冲突。错误消息明确指出两个版本中都存在重复的类"kotlin.collections.jdk8.CollectionsJDK8Kt"。

解决方案在这里 有两种方法,但我发现这个更容易:进入android/app/build.gradle并更新依赖项

dependencies {
constraints {
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0") {
        because("kotlin-stdlib-jdk7 is now a part of kotlin-stdlib")
    }
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0") {
        because("kotlin-stdlib-jdk8 is now a part of kotlin-stdlib")
    }
}

15

只需将以下行添加到您的gradle.properties文件中:

android.useAndroidX=true
android.enableJetifier=true

请注意检查,不要重复任何已存在的行(并确保现有行为true)。
最后,只需重新构建您的项目。
但有时您可能需要手动删除build文件夹。

细节

您的项目(或其子项目或依赖项之一)不再使用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


删除 build 文件夹成功。 - C.F.G

8

我遇到了同样的问题,我的解决方案如下:

  1. Go to gradle.properties files

  2. Add these two lines :

    android.useAndroidX=true
    android.enableJetifier=true
    
  3. Rebuild your project.

注意:

如果您的项目已经显示android.useAndroidX=true,则只需添加android.enableJetifier=true并重新构建您的项目即可。


6

对我而言,只需安装插件“cordova-plugin-androidx”和“cordova-plugin-androidx-adapter”即可解决这个问题:

$ ionic cordova plugin add cordova-plugin-androidx
$ ionic cordova plugin add cordova-plugin-androidx-adapter

3

我认为你在gradle.properties中多次使用了相同的标签,如下所示:

    org.gradle.jvmargs=-Xmx4g -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
    org.gradle.jvmargs=-Xmx4608m

2

遇到了同样的问题。对我起作用的是将以下内容添加到我的android/app/build.gradle文件中:

configurations.all {
    resolutionStrategy {
        eachDependency {
            if ((requested.group == "org.jetbrains.kotlin") && (requested.name.startsWith("kotlin-stdlib"))) {
                useVersion("1.8.0")
        }
    }
}

只需将useVersion("X.X.X")替换为您需要的版本即可。

1

我曾经有同样的问题,但是我按照以下方式解决了它。 在 build.gradle 文件中实现代码

def lifecycle_version = "2.4.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"

本文中确认


1
在我的情况下,我将app级别的build.gradle文件中依赖部分下的这个实现删除了 - implementation 'com.google.android.gms:play-services-ads:19.2.0' 删除或注释掉它!!

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