Manage your Android project gradle
Maintain your gradle dependencies in a proper way
While developing android apps, all we think about writing clean code. Have you ever thought how we can manage our android project gradle in clean and smart way?
Recently by contributing in android projects, seen that developers are following different architecture patterns like MVP, MVVM to write clean code. But except some projects they are not give their focus or time to manage project gradle.
Lets see how we can do.
In this post, we’re going to focus on how we can manage our project gradle in clean and smart way.
Lets assume that currently we have one project. And in app level build.gradle
we have below dependencies{ … }
section with different dependencies:
Lets follow the below steps:
Step 1:
Create ext{..} in your project level build.gradle at bottom as below:
ext{
...
}
Step 2:
Declare all required variables with values in above created ext{ .. }
as below:
Here we added variables includes SDK versions, library versions etc from the our gradle dependencies.
Step 3:
Now create one array in ext{..}
which contain our dependencies as below:
- Here we have created
commonDependency
and added our all dependencies with respective version and access variable name. - Here from the above
commonDependency
list, consider first kotlin dependency line, wherekotlin
is consider as access variable. - Here we have declared other
firebaseDependency
array where we have added firebase related dependencies to put in single group for firebase. It is good practice to add related dependencies in different group array.
Now our dependencies are ready to use in app level build.gradle.
Lets use all declared dependencies in app.
Step 4:
- Now in App level
build.gradle
we need to access above created ext to use dependencies:
- Create global scope variable above
android { ... }
section. Here we have createddef config = ...
which will be use for accessingext
members.
Step 5:
- Now we can access all
ext
members using createdconfig
variable.
Check below how we are accessingminSdkVersion
&targetSdkVersion
to set value of sdk versions usingconfig
:
Step 6:
- Now set the dependencies using
config
independencies
section as below:
- Using
config
variable we can accesscommonDependency
andfirebaseDependency
array to use library gradle dependencies. - Following with the
config
variable and array name (e.gcommonDependency
orfirebaseDependency
) using Access name you can add required dependency.
Done. Now Sync your project and enjoy new build!
Advantages:
- Manage same version value for group dependencies. Like we have used same version variable ( e.g
firebaseVer='16.0.4'
for all firebase gradle dependencies. - Update the
ext
variable value which will be reflect all over the project.
Demo project:
Want to Contribute?
Thanks for reading this article. Be sure to clap/recommend as much as you can and also share with your friends. It means a lot to me.
For more about programming, follow me and Mindorks, so you’ll get notified when we write new posts.
Check out all the Mindorks best articles here.
Also, Let’s become friends on Twitter, Linkedin, Github, and Facebook.