Is it possible to add a runtime dependency dynamically using the version of a compile dependency

Hi we have some 3rd party JAR’s which use SLF4J for logging, and so have a dependency on slf4j-api. They don’t have a dependency on the implementation JAR so it’s up to us to add this (we use log4j so in our case we want to add the log4j binding).

Is there any way I can add the slf4j-log4j binding dynamically? I wanted to avoid hard-coding the version to use as I assume it should match the verison of the api JAR. As the slf4j-api jar is a transitive dependency this may change on future releases of the 3rd party JAR’s we use.

Here’s an example of what I’m trying to do. However this doesn’t work as configurations.allDependencies.findAll doesn’t list any of the transitive dependencies (only the direct ones). Is what I’m trying to do possible?

dependencies {
    //ehcache-core has a dependency on slf4j-api
    compile 'net.sf.ehcache:ehcache-core:2.5.1'
          //try to find if slf4j-api is a compile dependency and if so
     //add a runtime dependency on the log4j binding (using the same version)
    configurations.compile.allDependencies.findAll { dep -> dep.name == 'slf4j-api' }.each { dep ->
          runtime "org.slf4j:slf4j-log4j12:$dep.version"
    }
}

This seems to work:

configurations.compile.resolvedConfiguration.firstLevelModuleDependencies.each { dep ->

dep.children.findAll { child -> child.moduleName == ‘slf4j-api’ }.each { child ->

runtime “org.slf4j:slf4j-log4j12:$child.moduleVersion”

}

}