Java – Gradle copies classes in dependencies

Gradle copies classes in dependencies… here is a solution to the problem.

Gradle copies classes in dependencies

I’m building an android app and using the ReCaptcha and EasyWSDL repository. Both repositories contain okhttp and okio jars, and an error occurs when launching an application with duplicate classes. However, these are not the same. If I exclude okhttp (added by the recaptcha repository) from the project, recaptcha does not have the specific method described in okhttp.jar added by recaptcha.

Code fragment: http://pastebin.com/fDuYUH76

Not sure how to post here. Formatting crashes.

Solution

Gradle does not resolve conflicts caused by two different jars with shared classes. If you have two jars with the same artifactId and different versions, it will only use one of them. If you have two different artifactIds, gradle can’t do anything about it.

If using both jars causes your application to crash, you can only use one of them. If your application doesn’t run with just one of these jars, it might not build the way you expect.

By the way: it’s not a matter of having multiple repositories, it’s a matter of having conflicting jars on the classpath.

Related Problems and Solutions