Julian
Julian

Reputation: 21

How to add dependency on a jar in Gradle while swaping internal library dependency

I'm trying to add a jar dependency to my Android project:

dependencies {
  compile files('<SOME_JAR>')
}

But the jar is packaged with a library dependency I don't want to include. Instead I would like to declare a newer version of the library:

dependencies {
  compile '<NEW_VERSION_LIBRARY>'
  compile files('<SOME_JAR>') {
    exclude '<OLD_VERSION_LIBRARY>'
  }
}

Is it possible to achieve something like this in Gradle?

Upvotes: 1

Views: 112

Answers (1)

Craig Trader
Craig Trader

Reputation: 15669

If your dependencies are loading multiple versions of a given library, and you want to use a specific version, then you can do something like this, which will always use Groovy 2.4.5:

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.group == 'org.codehaus.groovy') {
            details.useVersion '2.4.5'
        }
    }
}

If you just want to keep from loading a particular dependency, because you'd prefer to use something else, then you want something like this, which will exclude the Apache Commons Logging and substitute SLF4J and Logback:

dependencies {
    compile ( "com.orientechnologies:orientdb-graphdb:2.0.7" ) { 
        exclude module:'commons-logging' 
    }
    compile 'org.slf4j:slf4j-api:1.7.12' 
    runtime 'org.slf4j:jcl-over-slf4j:1.7.12' 
    runtime 'ch.qos.logback:logback-classic:1.1.3'
}

Upvotes: 1

Related Questions