As an Android developer, you might have skilled the frustration of managing dependencies in your mission. It may be particularly difficult when you’ve gotten a number of modules in your mission, because it’s simple to finish up with completely different variations of the identical dependency in numerous modules. This may result in conflicts and unpredictable conduct.
One solution to remedy this drawback is to create a separate
buildSrc module in your mission. The
buildSrc module is a particular module that’s used to retailer your mission’s construct logic, together with dependencies. By retaining your dependencies in a single place, you may be certain that all modules in your mission are utilizing the identical model of a dependency.
There are after all other ways of fixing this drawback, and which method is finest for you’ll rely in your particular wants and preferences.
After I posted this weblog submit, I figured that most individuals nowadays desire utilizing Gradle model catalog. I personally discover the buildSrc simple to take care of.
To create a
buildSrc module, you will must do the next:
- Open your mission.
2. From the Venture pane, change viewing your recordsdata from Android to Venture Information.
Whenever you view the recordsdata as Android recordsdata, they’re organized in accordance with their perform inside the Android working system, comparable to by exercise or service. Whenever you view the recordsdata as Venture recordsdata, they’re organized in accordance with their location within the file system.
Right here’s an instance of how the recordsdata would present while you view the recordsdata as Venture recordsdata — they’re organized in accordance with their location within the file system. 👇
3. Now, create a brand new listing out of your mission root folder, And title it
4. Now we have to create a brand new gradle file inside this listing.
It will assist flip this from a easy listing to a module. This will even then assist compile the dependencies from this module into our different gradle recordsdata in numerous modules.
By turning a listing right into a module, you may 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 for creating reusable code that may be shared throughout a number of apps or libraries.
Right here’s the way you do it:
a) Create a brand new file and title it
b) Paste the next code in your new file:
c) Sync the mission to make the brand new adjustments work throughout the mission.
When you discover, this provides a bit of blue squared icon on the backside proper of your
buildSrc folder icon and in addition add construct listing inside it.
This implies the sync was profitable in turning the listing right into a module.
5. Time so as to add dependencies!
a) In your
buildSrc, create a brand new
That is used to retailer all of your Kotlin supply code for this module — together with how we are going to declare dependencies.
6. Lastly, contained in the
src/primary/kotlin listing, create a brand new Kotlin object class named,
7. Paste the dependencies you’d like in your mission.
Make it in order that the dependency model has its personal variable. This ensures that we use the identical model for all the same dependencies. For example, compose-ui, compose-ui-tooling, compose-foundation, and many others all use the identical model so that you received’t run into conflicts or errors by utilizing completely different variations for various dependencies.
8. Now all you want is to go to different modules’ gradle file, and substitute your already outlined dependencies with those we outlined within the
Deps.kt object class.
implementation 'androidx.compose.ui:ui' will be changed by
And…congratulations! You now have a extra scalable, structured, and maintainable mission in your Android app.
By retaining your dependencies within the
buildSrc module, you may keep away from the chance of retaining completely different variations of dependencies in numerous modules. This may make it simpler to handle your mission and be certain that the whole lot is working as meant. Plus, it could make it simpler to refactor your code and make adjustments, as you will solely must replace the dependencies in a single place.
Total, making a
buildSrc module is an easy and efficient solution to handle dependencies in your Android mission. It could possibly prevent time and trouble in the long term, and assist you create a extra secure and dependable app.
How to Avoid Dependency Conflicts in Android Multi-Module Apps | by Waseef Akhtar | Jan, 2023