How to achieve cascading dependencies in Gradle for Android Studio?

Hi. I am working with Android Studio, and I have a project level build.gradle file, an app level build file, and feature module level build file.

Instead of defining the same application dependency in all the modules like the hilt dependency, is there a way to avoid this redundancy by just defining this application dependency in the top level project build.gradle file? It is my understanding that application dependencies cannot be included in the project level build.gradle file. If that’s the case, is there any other way I can achieve this?

Thanks!

You might find the advice out there to use allprojects { ... } or subprojects { ... } but you should not follow it. It is discouraged bad practice for various reasons.

If it is just about one dependency, I would just duplicate it in the build scripts.

If it is about multiple dependencies you want to add to multiple projects, you could for example use a bundle in a version catalog and then use that bundle in the build scripts.

If it is not only about dependencies, but also about common configuration, you should have a look at convention plugins in buildSrc or an included build like gradle/build-logic, possibly implemented by using precompiled script plugins.

Great! Thanks for the suggestions. Yes, I was referring to multiple dependencies. I’ll try out the suggestions and report back if I have any issues or need advice. Thanks!

1 Like

I was already using buildSrc, but I want to avoid repeating the target dependencies specified in buildSrc repeatedly in each of the dozens of feature module build files in my project. And changing the name of the target in buildSrc would mean that I would have to rename each of those references in the module build files. I could use a bundle in a version catalog, but I’m still repeatedly specifying the bundle in each module build file which defeats the purpose of my goal. what can I do at this point? Thanks!

If you have buildSrc I guess you have convention plugins there.
If you have a convention plugin that is applied to all the relevant projects, add it there.
If not, create one and apply that.
Of course that will not remove the need to have one line in all of the project modules.
But that’s how it is.
Anything the does not require that is bad practice and discouraged.