5

In my projects, I need to import third party jar file and 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. It always shows duplicate entry com/facebook/internal/BundleJSONConverter.

So, I want to exclude in Facebook or 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.

Lukas Körfer
  • 13,515
  • 7
  • 46
  • 62
saturngod
  • 24,649
  • 17
  • 62
  • 87

3 Answers3

5

The exclude method 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.

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

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. It allows you to alter included jars, e.g. to change packages or remove classes.

The following (shortened) example shows the usage of the plugin and some methods to alter the dependency 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'
}
Lukas Körfer
  • 13,515
  • 7
  • 46
  • 62
  • Could not get unknown property jarjar I am getting. What to do? – Nallamachu Oct 07 '20 at 04:56
  • @Nallamachu You need to include the *jar jar links* plugin. Sadly, it is not available via the Gradle Plugin Portal, so you cannot just use the `plugins` closure. Check out this full [example `build.gradle`](https://github.com/shevek/jarjar/blob/master/jarjar-gradle/example/build.gradle) instead. – Lukas Körfer Oct 07 '20 at 10:48
  • Can we use this library for jar files in libs folder? If yes, then what should be the value for from in the closure, say the jar file is libs/my-sample.jar? – Asdinesh Oct 05 '21 at 08:23
2

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.

Hope it works...

Ari
  • 326
  • 2
  • 5
  • 3
    To shorten this procedure: There is no need to rename or repack the archive. Any `jar` file is a `zip` file, you should be able to open it with various tools (e.g. 7-zip). – Lukas Körfer Jul 10 '17 at 00:23
  • 1
    Also, please document this `jar` file change! Otherwise you will run into problems when updating the external library. – Lukas Körfer Jul 10 '17 at 00:25
  • 2
    Note that the question was about doing it with gradle and not manually – ozma Feb 16 '21 at 06:49
0

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. 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:

android {
   sourceSets {
     main {
       java {
            filter.excludes = [
                    "com/package/Duplicated.java",                      
            ]
       }
     }
   }
}
narko
  • 3,645
  • 1
  • 28
  • 33