Why does the custom Configuration not automatically download dependencies?

I don’t understand Configuration very much, I hope someone can help me, thank you.

I have customized a Configuration called customConfig, which is used as follows:

...
configurations {
     customConfig
}
dependencies {
    customConfig 'io.reactivex.rxjava2:rxjava:2.2.2'
}
...

I clicked the sync button in AndroidStudio, but did not trigger the download of reactivex, but if I use the implementation that comes with gradle, it will be downloaded automatically when I click sync:

implementation 'io.reactivex.rxjava2:rxjava:2.2.2'

So I want to know:

  • Why does my custom Configuration not automatically download dependencies?
  • Why does my gradle’s own implementation automatically download dependencies?
  • How do I control the custom Configuration to let it download dependencies?
  • What is the timing of the gradle configuration that triggers the download? Which classes do the download logic specifically?

Dependencies are downloaded (if not already cached) when the configuration is resolved. Configurations are not automatically resolved. In most cases, the resolution occurs when a task has been configured to consume the configuration.

For example, the compileJava task depends on the compileClasspath configuration which extends implementation. The implementation dependencies are downloaded due to the compilation task requiring the contents of the configuration.

You should not need to explicitly resolve the custom configuration. Rather, the task that actually needs this should consume it and trigger resolution.

In the case of the IDE, the source set is being mapped to the IDE’s compilation mechanics and the source set has a compile classpath set. The resolution occurs more eagerly here as this is needed to set up the model in the IDE.

as you say:
In most cases, the resolution occurs when a task has been configured to consume the configuration.

and

You should not need to explicitly resolve the custom configuration. Rather, the task that actually needs this should consume it and trigger resolution.

I still don’t understand a bit,How can I consume a Configuration? Could you show me some code? Thanks very much!

To give you a meaningful code example, you would need to explain more about what you’re trying to do. What is the purpose of your customConfig configuration that makes you want to separate it from the already provided configurations? What do you need to do with these artifacts in customConfig?

Actually, now I just want to figure out two things:

  • How to trigger dependent downloads
  • How to add dependent libraries to the classpath

For the first point, you said that the Configuration needs to be resolved to trigger the dependent download, so I want to know how to operate in the code to make Configuration resolved? I tried project.configurations.findByName('customConfig').asPath to trigger Configuration to be resolved. Is there any other similar way to achieve this effect?

For the second point, I know there is a JavaCompile task, but I am not too sure how to add a classpath that depends on JavaCompile, so could you tell me how to add it to the classpath or is there any other way to do it?

thanks very much!

There are multiple methods on a Configuration that would cause the Configuration to be resolved. The javadoc of Configuration states which methods resolve the Configuration. If your intention is to resolve though, resolve() is pretty intention revealing in the code.

There is a FileCollection property called classpath on the JavaCompile tasks. You can manipulate this or properties of the SourceSet or just use the Configurations that are provided.

You generally shouldn’t be manually resolving configurations or customizing the task classpath in a buildscript without a really specific use case though. There are almost always higher level constructs you should be working with here. However, without knowing what you’re trying to do, it’s hard speculate what the best way to do it would be in your case.