AndroidPub
Published in

AndroidPub

How to Manage Gradle Dependencies in Android Project

Source: Pixabay

In application development, the use of libraries is very important. Whether it’s a 3rd party library or standard library. On an Android project, adding a library can be done by adding it to build.gradle as a dependency. We can easily do this with the Gradle build system. You can see the official tutorial on this link.

The code above is an example of a library used in an application. Pretty neat and there are no problems in its implementation.

Basically, a new project on Android Studio only has 1 (one) module, the app. That way, even setting dependencies is pretty easy because it’s centered on a build.gradle (app). However, what if we want to apply modularization to our application?

Modularization is a technique in application development that allows us to separate functionality into independent modules. A modular application has many build.gradle files for each module. Therefore, we need to manage the dependencies in it. The goal is to make dependency declarations more neat and expressive. Also, to simplify maintenance of libraries later.

Steps by Steps to Manage Dependencies

We will group dependencies on a special module, called buildSrc. Let’s follow the following steps below:

  1. Create a new Android Studio project with Kotlin DSL as the build scripts. Follow this tutorial to migrate from Groovy to Kotlin DSL: Playing Gradle Script with Kotlin DSL in Android Project.
  2. Create a new folder named buildSrc in the main folder of the project. This folder will later become a module that will accommodate all dependencies.
  3. Inside buildSrc add several folders and files, so the structure is as follows:
    buildSrc
    ├── src
    │ └── main
    │ ─└── java
    │ ──└── Dependencies.kt
    ├── build.gradle.kts
  4. Add the Kotlin DSL plugin in the build.gradle.kts file:
    plugins {
    `kotlin-dsl`
    }
    repositories {
    jcenter()
    }
  5. Activate the plugin by doing Gradle synchronization.
  6. Next in the Dependencies.kt file, we will move all the dependencies used on the project. The method is quite simple. We will declaring all dependencies and their versions into several objects. We can also group objects by type. That way the code will look neater. Consider the following example:

7. Now we can use all dependencies more expressively. Here’s an example:

Next, change all application dependencies on build.gradle.kts (project) and build.gradle.kts (app).

Well, that’s what Gradle dependency management is like. Now all dependencies have been stored in buildSrc and can be used for all existing modules. When we want to add or update a dependency later, we no longer need to open modules one by one. Simply open buildSrc and add or update the desired dependencies.

For more details, you can see the following commit history:

Have any question? Feel free to drop your question or comment below.

--

--

The (retired) Pub(lication) for Android & Tech, focused on Development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store