Contents
Master Gradle dependency management with Kotlin + buildSrc for Android
If you find your Gradle file too much cluttered and not organised properly then this post is for you.However it depends on developers that how they manage their code but most of the time due to strict deadlines we don’t care about the most important part “build.gradle”. The task gets more tedious as our project grows and there are more number of dependencies.So lets start with “Master Gradle dependency management with Kotlin + buildSrc for Android”.
The main reason that we need to do dependency management is our Android project is always containing the multi module project which definitely increases the chances of duplicate dependency and cluttered gradle which increases the size of apk. And also as you scale your app in future at that time you think that what the hell we have done.
So here I am telling how we can manage the gradle in our app:
- Managing manually
- Following recommended way by Google using “ext” (in their projects source code).
- Following latest Kotlin + buildSrc
Kotlin KTX -set of extension for Android app dev
1. Manually managing each module build.gradle
Most of the time we follow we manual method and how it looks like : Module app->build.gradle
Now if you have used another library module_two->build.gradle then it looks like
If you have noticed we have “com.android.support” multiple times and if we have more libraries then what?(yak)
2. Following “Google recommended way” using “Gradle extra/ext”
If you follow this gradle then you will be more clear about how Google recommends to manage the gradle in the projects.If you have the habit of referring the open source codes for many famous projects of Google or any other libraries then you can find out that they have not included the version of dependency directly in the app->build.gradle file they manage in the Root->build.gradle.
So whats new and good in this you ask?
If you remember the case that I discussed in manual method regarding support library version number then you come to know that :(we need to manage same version for support library) so for doing this,
“If I need to upgrade support version from 24.2.1 to 27.0.2 then I have to go to each place and update version number“. So now you have saved lots of time and your code is more managed because you have to mention it only once.
Root-level build.gradle
Now how your module_one and module_two gradle will look like:
Well you can say thanks for that but not yet, you should be thinking that what’s Android studio helping me here like I have habit of pressing ctrl+space in any part of file even if its gradle , so don’t worry how about the autocomplete suggestions you will get as in Kotlin or java file same in Gradle then here come our third part:
3. Kotlin plus” buildSrc” makes the autocomplete in gradle
Where you need to start with:
To add the buildSrc
module to your project, all you need to do is to:
- Create a buildSrc folder in the root of your Android project.
- After you sync your Gradle files (using Sync Project with Gradle Files), Android Studio will detect the folder and populate it with the standard build folders (build/ and .gradle/).
You just need 2 files in your buildSrc module:
- build.gradle.kts
- Kotlin Code (In this case,
Dependencies.kt
)
Now add folders inside buildSrc folder in the order of src->main->java->Dependencies.kt as seen in the screenshot below to manage dependencies and get IDE completion support.
Note: Please remember not to add “buildSrc” folder in root->”settings.gradle”
buildSrc/build.gradle.kts
buildSrc/src/main/java/Dependencies.kt
After we have done a Gradle Sync, based on changes above, we can now access any of the values in Android Studio.
The result looks very similar to what “ext” block, but we have autocomplete and click support feature.
And lets see how the autocomplete feature will look like:
Conclusion
I highly recommend and appreciate the “Kotlin + buildSrc” option. Managing Gradle dependencies is a pain, and having autocomplete and click support is a game changer.
Please tell us more in the comment section what you feel like and if any issue you are facing.
You can also access the whole project on my github page:
https://github.com/askfortricks/KotlinKtxApplication
Ravi Yadav is an Android developer whose passion is to develop Android applications and sharing his work. He loves to post the tutorials which he created while learning any new latest technology topic.He loves to share about the latest technology happenings and other tricks that will be helpful for readers of Askfortricks.
Feeling glad to receive your comment