Nowadays modularization of Android applications is a hot topic in the Android community. Management of each module’s dependencies is getting more important than ever.

I’ve used different approaches to manage project’s dependencies like using root project’s ext block or reference dependencies from separate file through all modules. In my last project I found new way of managing project’s dependencies which is really easy and allow to code completion in build.gradle. Before starting the new approach lets recap existing approaches.

First Approach Using ext block

We need to define required dependencies in project’s build.gradle to make that variables accessible through all modules

We defined required dependencies in buildscript ext block and update other module’s dependencies by the that variables.

Second Approach Referencing From Separate File

It’s similar the first approach but it look more concise because all dependency & versions are referenced from separate file.

buildscript {

apply from: ‘dependencies.gradle’

}

reference dependencies.gradle file in buildscript block to make accessible that variable through all modules and update module’s dependencies by that variables.

New Approach Using buildSrc

With this approach we can able to use code completion support in all module of build.gradle file. We can define our dependencies using Kotlin or Java by the our project structure.

Using Java

Firstly we need to create buildSrc directory in the root directory of project. If we will use java, we need to create src/main/java/Dependency class and define our dependencies

We named our class as Dependencies but you can name it what ever you want

We almost completed all required thing except reference our define dependency from build.gradle file.

Using Kotlin

We need to create buildSrc directory in the root directory of project and create build.gradle.kts in buildSrc directory. And then append kotlin-dsl plugin in build.gradle.kts.

plugins{

`kotlin-dsl`

}

As a last step reference dependency from Deps object

P.S. Android Studio 3.3.2's buildSrc code completion feature is broken but it works on previous versions.

Credits

https://jrebel.com/rebellabs/using-buildsrc-for-custom-logic-in-gradle-builds/

https://caster.io/lessons/gradle-dependency-management-using-kotlin-and-buildsrc-for-buildgradle-autocomplete-in-android-studio.