sam_c
sam_c

Reputation: 810

What's happening under the hood when a library dependency is excluded in gradle?

Today I started building a library for my company so we could avoid some duplicate code across a few of our apps. The library uses a few dependencies that are duplicated in the apps. In this case, Retrofit and Eventbus. Anyhow when I included my library it looked something like this:

implementation (project(":mylib")

Knowing that I would be using Eventbus as well as some other dependencies, I wanted to test exluding them in order to avoid duplicate dependencies. So I added in Eventbus to the app's gradle file and I excluded Eventbus from my library.

implementation 'org.greenrobot:eventbus:3.0.0'
implementation (project(":mylib")){
    exclude module: 'eventbus'
}

Now I am fully aware that I could just not include Eventbus in the app by just importing mylib, and certainly I have excluded dependencies to avoid build conflicts in the past so I am familiar with the usage, but I just wanted to experiment with it because it occurred to me that I really don't know what's happening under the hood with this exclusion. The term exclusion implies (to me) that this dependency isn't compiled period at the point of exclusion, however, this can't be the case because the 3rd party library (Eventbus in this case) still seems to execute just fine in mylib and if it didn't compile there would be crashing code world wide because of weird library dependency issues. So what exactly is happening here? Is it the case that it still compiles for the library, and it still compiles for the app, but somehow these dependencies are just separated? If so, it seems like that isn't all that efficient because you would still be duplicating all the methods in the project.

Clearly I have a limited knowledge of this, so if someone could clarify this I would very much appreciate it.

Upvotes: 1

Views: 647

Answers (1)

Amaksoft
Amaksoft

Reputation: 1004

AAR/JAR library doesn't include dependencies's classes in archive, it only includes your code. Normally they are stored in maven-like repositories with .pom file that contains the list of its dependencies. When you add it to your project in build.gradle file it just downloads the AAR/JAR, parses .pom file and downloads all dependencies listed (recursively repeating the process for each library until it got all the dependencies). Which means that you don't have to exclude module if it also depends on same libraries that your app uses.

Wheh you add library as local module implementation (project(":mylib") it does exactly the same thing, but for each dependency listed in subproject's build.gradle

If you exclude transitive dependency it just makes ignore the artifacts with specified name while parsing .pom files

Upvotes: 3

Related Questions