8

I have a project that includes two external third party libraries. Both these libraries have the class file names obfuscated and gradle barfs when trying to build the project.

Duplicate class a.a.a.a.a.b found in modules...

I can't exclude any classes, as they are not duplicates, nor do I see a way for gradle to prefix the names.

Any easy fix for this?

Gary Bak
  • 4,746
  • 4
  • 22
  • 39

2 Answers2

3

You could use the shadow plugin to relocate the offending packages. You'd then depend on the shadowed jar instead of the original

plugins {
   id "com.github.johnrengelman.shadow" version "5.2.0"
}
configurations {
   shadowMe { transitive = false } 
} 
dependencies {
   shadowMe 'foo:jar-to-shadow:1.0'
   compile files({tasks.shadowJar})
}
task shadowJar(type: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
   archiveBaseName = 'shadowed-foo' 
   relocate 'a.a.a.a.a', 'shadow.a.a.a.a'
   from zipTree(configurations.shadowMe.singleFile)
}

None of this tested and possibly needs some tweaking but hopefully you get the idea

lance-java
  • 25,497
  • 4
  • 59
  • 101
  • Thanks, I ran into the shadow plugin while researching and thought there must be a better way. – Gary Bak Jan 30 '20 at 17:53
  • The only other solution would involve loading each jar with a different classloader (or loading each in a separate jvm) – lance-java Jan 30 '20 at 19:22
2

I had this issue with Install Referrer and ARCore (both from Google!)

I wasn't able to get ShadowJar working with an aar library (which contains a classes.jar). Maybe there's some way to do it, but I couldn't figure it out.

Instead, I downloaded and unzipped the aar, used jarjar to rename the offending class in classes.jar, and zipped it back into an aar. Then I put my modified aar file in my libs directory and modified my dependency to reference that local file instead of a maven library.

More detailed explanation:
To fix the classes.jar, I renamed it to classes-original.jar, created a jarjar.rules file containing:

rule a.a.a relocated-a.a.a

and ran:

java -jar jarjar-1.4.jar process jarjar.rules classes-original.jar classes.jar
Jim E-H
  • 333
  • 1
  • 3
  • 8
  • 1
    Hello, i'm currently running into the exact same issue with installReferrer and ARCore (with Unity). Do you remember the steps/commands you've used to resolve the problem? If so, do you mind sharing them? Would be greatly appreciated. Thanks in advance. P.S: In my case. the duplicate class is "a.a.a". Was it the same for you? – Berouminum Sep 01 '21 at 15:07
  • 1
    In case anyone else runs into this problem: Usage of the tool is explained in this answer: https://stackoverflow.com/a/33592560 – Berouminum Sep 02 '21 at 07:09