Understanding how Gradle downloads dependencies from external repositories

I’ve been using Gradle in Android Studio and I’m interested in understanding how it downloads dependencies that are added to my build.gradle file.

For example, my understanding is that when I add compile 'com.squareup.retrofit2:retrofit:2.1.0' and sync my project, Gradle will download retrofit-2.1.0.jar from jcenter and add the .jar to my project as an external library.

It also looks like Gradle parses the retrofit-2.1.0.pom file in order to find and download retrofit’s dependencies, such as OkHttp, and adds those .jar files to my project as well.

The documentation shows that DependencyHandler is the first step in this process and it is implemented by DefaultDependencyHandler.

I’ve been going through the Gradle source code on github looking for the class that downloads the .jar from the web, but haven’t been able to find anything. Any links to the source code or documentation that covers understanding the process of downloading dependencies would be helpful.

What are you looking for exactly? There is a lot of code related to dependency resolution. Checking out ExternalResourceResolver might be a good entry point to a lot of the artifact resolution stuff. Depends on what you’re interested in, the two main categories of code being that related to what should Gradle download (dependency resolution) and how should it be downloaded (artifact resolution).

1 Like

Thanks for the response. This looks really helpful for understanding the artifact resolution stuff. Is there a class that’s a good entry point for dependency resolution? I’m looking for how gradle is exactly downloading the dependency from a repository on the internet. Then I want to look into how it finds and downloads additional dependencies of a dependency (if necessary).

Depends on the transport protocol, HTTP, SFTP or S3.

There’s really no single example of where this is done. It depends a lot on the type of dependency (project, client module, external) and repository type (ivy/maven). The entry point to all this madness is resolving a Configuration. Feel free to go down the rabbit hole, but beware, there be dragons.

1 Like