'gradle exclude group from compile

I am getting this duplicate error when building my app:

addJar(...facebookadapter-4.0.3.jar): entry
duplicate entry: com/facebook/ads/AbstractAdListener.class

The reason I am getting this is that my app compiles Facebook modules one belongs to an adapter and one to its original SDK:

compile ('com.ironsource.adapters:facebookadapter:4.0.3@jar') compile 'com.facebook.android:audience-network-sdk:4.27.0' compile 'com.google.ads.mediation:facebook:4.27.0.0'

So as a solution, I am trying to exclude this group from the module which contains the adapter jar compile statement:

compile ('com.ironsource.adapters:facebookadapter:4.0.3@jar') {
    exclude (group: 'com/facebook/ads')
}

But, when I build my app again it fails to state the same reason from the same adapter

Any idea why the classes are not being excluded?



Solution 1:[1]

Looks like the exclude block syntax is incorrect. Try

compile ('com.ironsource.adapters:facebookadapter:4.0.3@jar') {
  exclude group: 'com.facebook.ads'
}

Update

So it looks like the facebookadapter contains this class inside. You can not exclude a class from a jar file, exclusion only works on per-dependency level.

If you absolutely need to have this adapter, you can try to exclude facebook ads transitive dependency from all the other dependencies.

configurations {
    all*.exclude group: 'com.facebook.ads'
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rmtheis