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

Bab*_*yan 0 java android dependency-management gradle

我的项目依赖于依赖关系A和依赖关系B.

依赖关系A还取决于依赖关系B,但它是一个不同的版本.

问题在于项目A的build.gradle依赖关系B直接使用文件和fileTree方法包含在jar中:

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

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

dependencies {
    compile 'com.B:B:myvesion'
    compile('com.A:A:theirversion') {
        exclude module: 'B'
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我的构建因此异常而失败:

Execution failed for task ':myproject:transformDexArchiveWithDexMergerForDebug'.
> com.android.build.api.transform.TransformException: com.android.dex.DexException: Multiple dex files define Lcom/B/someclass;
Run Code Online (Sandbox Code Playgroud)

我想jar从我的项目中排除B 的版本并使用我的版本.我该怎么做呢?好像exclude指令在我的情况下不起作用.

Gab*_*tti 5

你不能为jar文件做这件事.

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