Yas
Yas

Reputation: 23

Multiple versions of library with Gradle

I have the problem that Gradle is pulling in two versions of a library, which is causing runtime issues. These versions are both necessary, and are dependencies of two of the dependencies I have. Library A needs Library C version x, and Library B needs Library C version y. At runtime, the incorrect version of Library C is used, causing a NoSuchFieldError. Is this something I can resolve in Gradle? Or is it more of an issue for my IDE/JVM options?

Upvotes: 1

Views: 2687

Answers (1)

jjathman
jjathman

Reputation: 12624

In general Gradle will do the right thing by choosing the latest version of a library when there are dependency conflicts. However for various reasons this may not always work correctly. To get around this you can tell Gradle explicitly to not include a certain transitive dependency during its resolution. Here is an example:

compile (group:'com.project', name:'library', version:'1.0')  {
    // These lines will exclude these other libraries from being included
    exclude module: 'groovy-all'
    exclude module: 'log4j'
    exclude module: 'commons-lang'
}

You can make this more fine grained if you need to, but I've found excluding modules seems to work well for me.

Upvotes: 2

Related Questions