Gary Bak
Gary Bak

Reputation: 4798

Duplicate class names in two external third party obfuscated libraries

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?

Upvotes: 9

Views: 2450

Answers (2)

Jim E-H
Jim E-H

Reputation: 353

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

Upvotes: 2

lance-java
lance-java

Reputation: 27996

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

Upvotes: 4

Related Questions