java 在gradle Android Studio中排除一个类文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44913406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:24:49  来源:igfitidea点击:

Exclude one class file in gradle Android Studio

javaandroidgradlefacebook-sdk-4.x

提问by saturngod

In my projects, I need to import third party jar file and Facebook SDK.

在我的项目中,我需要导入第三方 jar 文件和 Facebook SDK。

compile files('libs/SkinSDK.jar')
compile 'com.facebook.android:facebook-android-sdk:4.14.0'

Both include same BundleJSONConverterclass. So, I cannot do generate signed APK. It always shows duplicate entry com/facebook/internal/BundleJSONConverter.

两者都包含同一个BundleJSONConverter类。所以,我无法生成签名的 APK。它总是显示重复的条目 com/facebook/internal/BundleJSONConverter。

So, I want to exclude in Facebook or SkinSDK.jar. I tried like

所以,我想在 Facebook 或 SkinSDK.jar 中排除。我试过

compile ('com.facebook.android:facebook-android-sdk:4.14.0') {
    exclude group: 'com.facebook.internal', module: 'BundleJSONConverter'
}

It's not working and showing same error.

它不起作用并显示相同的错误。

回答by Lukas K?rfer

The excludemethod of the configuration closure for a dependency excludes transitive dependencies. So, if your module dependency depends on other modules, you can exclude them from your build. You can check out the transitive dependencies of the 'com.facebook.android:facebook-android-sdk:4.14.0'module on its Maven repository info page.

exclude依赖项的配置闭包的方法不包括传递依赖项。所以,如果你的模块依赖依赖于其他模块,你可以从你的构建中排除它们。您可以'com.facebook.android:facebook-android-sdk:4.14.0'在其Maven 存储库信息页面上查看模块的传递依赖项。

If the BundleJSONConverterclass exists in a transitive dependency, you can exclude the specific module in the same way you are trying now. Just specify the group, the moduleand the version, like you do for dependencies.

如果BundleJSONConverter该类存在于可传递依赖项中,您可以按照现在尝试的相同方式排除特定模块。只需指定groupmoduleversion,就像您对依赖项所做的一样。

If you just want to exclude one class for a dependency jar, take a look at the jar jar linkstool and its Gradle plugin. It allows you to alter included jars, e.g. to change packages or remove classes.

如果您只想为依赖项 jar 排除一个类,请查看jar jar 链接工具及其 Gradle 插件。它允许您更改包含的 jar,例如更改包或删除类。

The following (shortened) exampleshows the usage of the plugin and some methods to alter the dependency jar:

以下(缩短的)示例显示了插件的用法和一些更改依赖项 jar 的方法:

compile jarjar.repackage {
    from 'org.apache.hive:hive-exec:0.13.0.2.1.5.0-695'

    archiveBypass "commons*.jar"
    archiveExclude "slf4j*.jar"

    classDelete "org.apache.thrift.**"
    classRename 'org.json.**', 'org.anarres.hive.json.@1'
}

回答by Ari

Bumped into similar situation. This is what I did, not elegant as I hoped, but it works:

遇到了类似的情况。这就是我所做的,并不像我希望的那样优雅,但它有效:

  1. Rename the jar file (SkinSDK.jar in your case): .zip instead of .jar

  2. Go "inside" the zip file (I'm using DoubleCommander, there are many other utilities for that), or extract it to a temporary folder.

  3. Delete the duplicate class that causes the problem. Go "outside" the zip file.

  4. Rename (or re-pack) the file from .zip to .jar . Compile.

  1. 重命名 jar 文件(在您的情况下为 SkinSDK.jar):.zip 而不是 .jar

  2. 进入 zip 文件的“内部”(我使用的是 DoubleCommander,还有许多其他实用程序),或者将其解压缩到一个临时文件夹。

  3. 删除导致问题的重复类。转到 zip 文件的“外部”。

  4. 将文件从 .zip 重命名(或重新打包)为 .jar 。编译。

Hope it works...

希望它有效...