简体   繁体   English

在 gradle Android Studio 中排除一个 class 文件

[英]Exclude one class file in gradle Android Studio

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 BundleJSONConverter class. So, I cannot do generate signed APK.两者都包含相同BundleJSONConverter class。因此,我无法生成签名的 APK。 It always shows duplicate entry com/facebook/internal/BundleJSONConverter.它总是显示重复条目 com/facebook/internal/BundleJSONConverter。

So, I want to exclude in Facebook or SkinSDK.jar.所以,我想在 Facebook 或 SkinSDK.jar 中排除。 I tried like我试过

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.它不工作并显示相同的错误。

The exclude method of the configuration closure for a dependency excludes transitive dependencies. 依赖项的配置闭包的exclude方法排除了传递依赖项。 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 . 您可以在其Maven存储库信息页面上查看'com.facebook.android:facebook-android-sdk:4.14.0'模块的传递依赖关系。

If the BundleJSONConverter class exists in a transitive dependency, you can exclude the specific module in the same way you are trying now. 如果BundleJSONConverter类存在于传递依赖项中,则可以使用与现在尝试相同的方式排除特定模块。 Just specify the group , the module and the version , like you do for dependencies. 只需指定groupmoduleversion ,就像对依赖项一样。

If you just want to exclude one class for a dependency jar, take a look at the jar jar links tool and its Gradle plugin. 如果您只想为依赖关系jar排除一个类,请查看jar jar链接工具及其Gradle插件。 It allows you to alter included jars, eg to change packages or remove classes. 它允许您更改包含的jar,例如更改包或删除类。

The following (shortened) example shows 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'
}

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 重命名jar文件(在你的情况下为SkinSDK.jar):. zip而不是.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. 转到zip文件的“内部”(我正在使用DoubleCommander,还有许多其他实用程序),或将其解压缩到临时文件夹。

  3. Delete the duplicate class that causes the problem. 删除导致问题的重复类。 Go "outside" the zip file. 转到zip文件的“外部”。

  4. Rename (or re-pack) the file from .zip to .jar . 将文件从.zip重命名(或重新打包)到.jar。 Compile. 编译。

Hope it works... 希望它有效......

I had a similar problem with duplicated classes after importing a jar. In my case, the conflict was between a class in that jar and a class in my own project.在导入 jar 后,我遇到了类似的重复类问题。在我的例子中,冲突发生在 jar 中的 class 和我自己项目中的 class 之间。 Below I share the solution you can use to discard classes that you have available in your own source tree, assuming the one in the jar is the right one to use:下面我分享了您可以用来丢弃您自己的源代码树中可用的类的解决方案,假设 jar 中的那个是正确的:

android {
   sourceSets {
     main {
       java {
            filter.excludes = [
                    "com/package/Duplicated.java",                      
            ]
       }
     }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM