Blog Infos
Author
Published
Topics
Published
Topics

 

As an Android developer, you may have experienced the frustration of managing dependencies in your project. It can be especially challenging when you have multiple modules in your project, as it’s easy to end up with different versions of the same dependency in different modules. This can lead to conflicts and unpredictable behavior.

One way to solve this problem is to create a separate buildSrc module in your project. The buildSrc module is a special module that is used to store your project’s build logic, including dependencies. By keeping your dependencies in one place, you can ensure that all modules in your project are using the same version of a dependency.

There are of course different ways of solving this problem, and which approach is best for you will depend on your specific needs and preferences.

When I posted this blog post, I figured that most people these days prefer using Gradle version catalog. I personally find the buildSrc easy to maintain.

So..

To create a buildSrc module, you’ll need to do the following:

  1. Open your project.

 

2. From the Project pane, change viewing your files from Android to Project Files.

When you view the files as Android files, they are organized according to their function within the Android operating system, such as by activity or service. When you view the files as Project files, they are organized according to their location in the file system.

Here’s an example of how the files would show when you view the files as Project files — they are organized according to their location in the file system. 👇

3. Now, create a new directory from your project root folder, And name it buildSrc.

4. Now we need to create a new gradle file inside this directory.

This will help turn this from a simple directory to a module. This will also then help compile the dependencies from this module into our other gradle files in different modules.

By turning a directory into a module, you can create a separate unit of code within your project that can be compiled and run independently. This can be useful for organizing your project and for creating reusable code that can be shared across multiple apps or libraries.

Here’s how you do it:

a) Create a new file and name it build.gradle.kts.

b) Paste the following code in your new file:

Job Offers

Job Offers


    Android Test Automation Engineer

    Komoot
    Remote
    • Full Time
    apply now

    Senior Android Software Engineer (f/m/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now

    Android Software Engineer (f/m/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now

OUR VIDEO RECOMMENDATION

,

At long last we have Kotlin first at Meta!

Android started to support Kotlin 5 years ago and became the first-choice language three years ago. But Meta just announced Kotlin as the preferred and default language for our Android code base only 3 months…
Watch Video

At long last we have Kotlin first at Meta!

Peng Jiang & Sergei Rybalkin
Software Engineer & Kotlin
Meta

At long last we have Kotlin first at Meta!

Peng Jiang & Serge ...
Software Engineer & ...
Meta

At long last we have Kotlin first at Meta!

Peng Jiang & Ser ...
Software Engineer & Kotli ...
Meta

Jobs

c) Sync the project to make the new changes work across the project.

 

If you notice, this adds a little blue squared icon at the bottom right of your buildSrc folder icon and also add build directory inside it.

This means the sync was successful in turning the directory into a module.

5. Time to add dependencies!

a) In your buildSrc, create a new src/main/kotlin directory.

This is used to store all your Kotlin source code for this module — including how we will declare dependencies.

6. Finally, inside the src/main/kotlin directory, create a new Kotlin object class named, Deps, or Dependencies.

7. Paste the dependencies you’d like for your project.

Make it so that the dependency version has its own variable. This ensures that we use the same version for all the similar dependencies. For instance, compose-ui, compose-ui-tooling, compose-foundation, etc all use the same version so you won’t run into conflicts or errors by using different versions for different dependencies.

8. Now all you need is to go to other modules’ gradle file, and replace your already defined dependencies with the ones we defined in the Deps.kt object class.

For instance:

implementation 'androidx.compose.ui:ui' can be replaced by implementation(Deps.composeUi)

And…congratulations! You now have a more scalable, structured, and maintainable project for your Android app.

By keeping your dependencies in the buildSrc module, you can avoid the risk of keeping different versions of dependencies in different modules. This can make it easier to manage your project and ensure that everything is working as intended. Plus, it can make it easier to refactor your code and make changes, as you’ll only need to update the dependencies in one place.

Overall, creating a buildSrc module is a simple and effective way to manage dependencies in your Android project. It can save you time and hassle in the long run, and help you create a more stable and reliable app.

Awesome that you came this far! 👏 Now I’d love to know what the most annoying part of this post was or if it was of any help to you. Either ways, you can drop me a DM on: www.twitter.com/waseefakhtar ✌️

Happy Coding! 💻

This article was originally published on proandroiddev.com on January 08, 2023

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
Hi, today I come to you with a quick tip on how to update…
READ MORE
blog
Automation is a key point of Software Testing once it make possible to reproduce…
READ MORE
blog
Drag and Drop reordering in Recyclerview can be achieved with ItemTouchHelper (checkout implementation reference).…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu