修复 android build.gradle 中的重复类

问题描述

我正在开发一个关于 react-native 的应用程序。 当我尝试编译我的应用程序时,Gradle 提出了一个重复的类问题。

这是错误日志:

Execution Failed for task ':app:checkLocalDebugDuplicateClasses'.
> 1 exception was raised by workers:
  java.lang.RuntimeException: Duplicate class org.apache.commons.io.ByteOrderMark found in modules commons-io-2.4.0.jar (org.lucee:commons-io:2.4.0) and commons-io-2.6.jar (commons-io:commons-io:2.6)
  Duplicate class org.apache.commons.io.Charsets found in modules commons-io-2.4.0.jar (org.lucee:commons-io:2.4.0) and commons-io-2.6.jar (commons-io:commons-io:2.6)
... (and many more)

我查找了在线解决方案,我想从构建中排除一个依赖项,即较低版本的依赖项。这样,我认为构建会起作用。

我运行 app:dependencies 来查找哪些包正在使用此重复依赖项:

--- com.facebook.react:react-native:+ -> 0.63.4
|   ...
+--- project :expo-constants
|    ...
|    \--- commons-io:commons-io:2.6
+--- project :expo-file-system
|    ...
|    +--- commons-io:commons-io:1.4 -> 2.6
|    ...
+--- com.bridgefy:android-sdk:1.1.28
|    ...
|    +--- org.lucee:commons-io:2.4.0
|    ...

现在,我将其添加到我的 app/build.gradle 文件中:

dependencies {
    implementation filetree(dir: "libs",include: ["*.jar"])
    implementation "com.facebook.react:react-native:+"  // From node_modules

    addUnimodulesDependencies()

    if (enableHermes) {
        def hermesPath = "../../node_modules/hermes-engine/android/";
        debugImplementation files(hermesPath + "hermes-debug.aar")
        releaseImplementation files(hermesPath + "hermes-release.aar")
    } else {
        implementation jscFlavor
    }
    implementation 'com.android.support:multidex:1.0.3'

    // I ADDED THIS LINE BELOW
    implementation('com.bridgefy:android-sdk:1.1.28') {
        exclude group: 'org.lucee',module: 'commons-io'
    }
}

错误不会随之消失...我认为该模块并未排除在外,但我认为这是方法。请问如何解决

解决方法

我使用此配置解决了我的问题,app/build.gradle :

...
configurations {
    compile.exclude group: 'org.lucee',module: 'commons-io'
}
...

我的错误是我在 dependencies {} 块内写了这一行。