How to Avoid Dependency Conflicts in Android Multi-Module Apps | by Waseef Akhtar | Jan, 2023 | Nest Tech

Posted on

As an Android developer, you’ll have skilled the frustration of managing dependencies in your mission. It may be particularly difficult when you have got a number of modules in your mission, as it is simple to finish up with totally different variations of the identical dependency in several modules. This could result in conflicts and unpredictable habits.

One solution to resolve this downside is to create a buildSrc module in your mission. He buildSrc module is a particular module used to retailer your mission’s construct logic, together with dependencies. By retaining your dependencies in a single place, you possibly can make sure that all modules in your mission use the identical model of a dependency.

In fact, there are alternative ways to resolve this downside, and which strategy is finest for you’ll rely in your particular wants and preferences.

After I revealed this weblog submit, I believed that most individuals as of late favor to make use of the Gradle launch catalog. I personally discover buildSrc simple to keep up.

So..

To create a buildSrc module, you have to to do the next:

  1. Open your mission.

2. Within the Venture panel, swap the show out of your Android information to Venture Information.

While you view information as Android information, they’re organized by operate inside the Android working system, resembling by exercise or service. While you view information as mission information, they’re organized by their location on the file system.

That is an instance of how the information would seem once you view them as mission information: they’re organized by their location within the file system. 👇

3. Now, create a brand new listing from the basis folder of your mission, and provides it a reputation buildSrc.

4. Now we have to create a brand new gradle file inside this listing.

This can assist convert this from a easy listing to a module. This may even assist construct this module’s dependencies into our different gradle information in several modules.

By changing a listing to a module, you possibly can create a separate unit of code inside your mission that may be compiled and run independently. This may be helpful for organizing your mission and creating reusable code that may be shared throughout a number of purposes or libraries.

That is the way you do it:

a) Create a brand new file and provides it a reputation construct.gradle.kts.

b) Paste the next code into your new file:

c) Synchronize the mission in order that the brand new modifications work throughout all the mission.

For those who discover, this provides somewhat sq. blue icon to the underside proper of your buildSrc folder icon and likewise add the construct listing inside it.

Which means the sync was profitable changing the listing to a module.

5. It is time to add dependencies!

a) in his buildSrccreate a brand new src/predominant/kotlin listing.

That is used to retailer all your Kotlin supply code for this module, together with how we’ll declare dependencies.

6. Lastly, contained in the src/predominant/kotlin listing, create a brand new Kotlin object class known as, Depsboth Dependencies.

7. Paste the dependencies you need on your mission.

Make the dependency model have its personal variable. This ensures that we use the identical model for all related dependencies. For instance, compose-ui, compose-ui-tooling, compose-foundation, and so on. all use the identical model, so you will not run into conflicts or errors utilizing totally different variations for various dependencies.

8. Now all you want is to go to the opposite modules gradle file and exchange their already outlined dependencies with those we outlined within the Deps.kt object class

For instance:

implementation 'androidx.compose.ui:ui' could be changed by implementation(Deps.composeUi)

And… congratulations! You now have a extra scalable, structured, and maintainable mission on your Android app.

By retaining your dependencies on the buildSrc module, you possibly can keep away from the chance of sustaining totally different variations of dependencies in several modules. This could make it simpler to handle your mission and make sure that every little thing works as anticipated. Additionally, it could actually make it simpler to refactor your code and make modifications, because you solely have to replace dependencies in a single place.

Generally, create a buildSrc The module is a straightforward and efficient solution to handle dependencies in your Android mission. It could possibly prevent time and problem in the long term and assist you construct a extra steady and dependable software.



How to Avoid Dependency Conflicts in Android Multi-Module Apps | by Waseef Akhtar | Jan, 2023