Why does the custom Configuration not automatically download dependencies?

android
buildship
plugins
(任强强) #1

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?
0 Likes

(James Justinic) #2

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.

0 Likes

(任强强) #3

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!

0 Likes

(James Justinic) #4

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?

0 Likes

(任强强) #5

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!

0 Likes