如何排除 Gradle 中的传递 jar 依赖?

How to exclude transitive jar dependency in Gradle?

我的项目依赖于依赖项 A 和依赖项 B。

依赖项 A 也依赖于依赖项 B,但它的版本不同。

问题出在项目 A 的 build.gradle 依赖项 B 直接使用 file 和 fileTree 方法作为 jar 包含在内:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile files('libs/B.jar')
}

我的项目 build.gradle 看起来像这样:

dependencies {
    compile 'com.B:B:myvesion'
    compile('com.A:A:theirversion') {
        exclude module: 'B'
    }
}

但我的构建失败并出现以下异常:

Execution failed for task ':myproject:transformDexArchiveWithDexMergerForDebug'.
> com.android.build.api.transform.TransformException: com.android.dex.DexException: Multiple dex files define Lcom/B/someclass;

我想从我的项目中排除 B 的 jar 版本并使用我的版本。我该怎么做呢? exclude 指令似乎对我不起作用。

您不能为 jar 文件执行此操作。

您可以排除传递依赖项(在 pom 文件中描述)。 jar 文件不是传递依赖项。