Open In App

Gradle Tips and Tricks For Android

Last Updated : 08 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Gradle is a leading built automation toolkit for Android that manages to build almost any type of software. It’s open-source and supports multi-language development and with the assistance of Gradle, you can do things like Compiling source code, packaging into binary code, manage dependencies, running automated tests, and deploying.

Gradle Tips and Tricks

1. Always Use Gradle Wrapper

Have you ever tried to build software and you found out that the version of the build tool isn’t compatible with the project?

Well, we have got good news for you because in Gradle we’ve got something called the Gradle wrapper which fixes that issue and this tip is to always use the Gradle wrapper why is that? Well, it’s really great because when we use the Gradle wrapper we’re using the version of Gradle that’s compatible specifically with the project and we don’t have any incompatibility issues. Using Gradle wrapper means:

  1. It removes any version incompatibility problems.
  2. you don’t have to have Gradle installed locally.

Gradle-wrapper. properties file already comes when you create a new project in Android studio but If you have installed it locally on your Windows machine then there are some command that is used to update Gradle wrapper

$ ./gradlew wrapper --gradle-version 7.2

To run the above command you will require Gradle installed on your machine. If you are on Mac, then you can use

brew install gradle

2. Add Build Dependencies

Dependencies can be described as something that assists in establishing an android project. A project in Android can have many functionalities and when you use libraries then a person must add dependencies. There are already some dependencies already specified when building a new project in the build.gradle file. In order to add a dependency then you have to go in Gradle Scripts -> build.gradle  and then there in dependencies section you have to add dependencies like shown below :

dependencies {
   // Building GFG project
   implementation 'androidx.core:core-ktx:1.6.0'
   implementation 'androidx.appcompat:appcompat:1.3.1'
   implementation 'com.google.android.material:material:1.4.0'
   implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
   testImplementation 'junit:junit:4.+'
   androidTestImplementation 'androidx.test.ext:junit:1.1.3'
   androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

3. View Dependency Graph

In order to view a dependency graph for your android project, you can run the resulting command.

$ gradle dependencies

4. Use Only Needed Plugins

Only use a plugin that is in use will help in decreasing the build time of our project Sometimes we use plugins that are not required in the configuration in Gradle which increases the build time of the project. So it is an important tip to double-check the plugin and write the only plugins that are getting used in the android project.

plugins {
   id 'com.android.application'
   id 'kotlin-android'
   id 'kotlin-android-extensions'
}

5. Centralizing Versioning Dependencies

If you are an android developer, then this tip is a must for you lets think of an example using the same libraries for different modules is a common case then updating every module will not be an ace process so you can centralize this versioning with extensions.

ext {
   buildToolsVersion = "25.0.2"  
   compileSdkVersion = 25  
   androidSupportLibraryVersion = "25.3.0"
}

This tip explained is widely used by Android programmers for ease of work.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads