Substituting artifact dependencies with project dependencies

I’m trying to automatically substitute artifact dependencies with project dependencies as in the example in 23.8.3.3. Conditionally substituting a dependency. I want to do it in the build.gradle of the root project, in a allprojects {} or in a subprojects {} block. But when I do that, I get warnings like this:

Changed dependencies of configuration ‘:project:compile’ after it has been included in dependency resolution. This behaviour has been deprecated and is scheduled to be removed in Gradle 3.0

I tried to put the substitution in an afterEvaluate {} block, but then I get warnings about changing the strategy of configuration after it has been resolved.

Is there a valid way to do this, or would I need to add the substitution code in each subprojects build file, after the dependencies declarations?