How to Speed Up Gradle build in Android Studio?

22 Mar, 2021
Gradle is one of the most important files or extensions that are present in the Android Project. Gradle handles all the libraries and application IDs and many important components of any Android application. Every time when we run our application or we build our apk we will get to see the message as Gradle build running and many more. In some devices, Gradle works very fast whereas in some devices it takes so much time to load and build your application. 
10:03:51 Gradle build finished in 4 min 0 sec  
10:04:03 Session 'app': running  
10:10:11 Gradle build finished in 3 min 29 sec  
10:10:12 Session 'app': running  
10:20:24 Gradle build finished in 3 min 42 sec  
10:28:18 Gradle build finished in 3 min 40 sec  
10:28:19 Session 'app': running  
10:31:14 Gradle build finished in 2 min 56 sec   
10:31:14 Session 'app': running  
10:38:37 Gradle build finished in 3 min 30 sec  
10:42:17 Gradle build finished in 3 min 40 sec  
10:45:18 Gradle build finished in 3 min 1 sec  
10:48:49 Gradle build finished in 3 min 30 sec  
10:53:05 Gradle build finished in 3 min 22 sec  
10:57:10 Gradle build finished in 3 min 19 sec  
10:57:11 Session 'app': running  
So in this article, we will take a look at optimizing the speed of our Gradle in the Android Studio Project. We are going to discuss 4 different methods for doing this task. 
Method 1: Offline mode for Gradle
Method 2: Using a specific library for google play services
Method 3: Removing proxy
Method 4: Avoid using more external dependencies in your project
Many times when we are building any application or running it on our device or emulator. We will get to see that Gradle connects with our internet and downloads the files from the internet. This process sometimes takes so much time which will depend on your internet connection. So to avoid downloading these files we have to enable offline mode for our android studio for Gradle files to avoid downloading of these files. Below are the steps in which we will be enabling offline mode in our application. Click on the File option which is shown in the top bar of Android Studio. Inside that click on the Settings option. You can get to see the Settings option in the below screen. 
After clicking on the settings option you will get to see the below dialog. Inside this dialog navigate to the Build, Execution, Deployment option and then click on the Gradle option. After that uncheck the option for Download external annotations and dependencies and after that click on Apply option and then click on the OK option to proceed further. After that we have enabled offline mode for Gradle has been enabled in our android studio project. Now due to offline mode, the loading time for your Gradle will be reduced to a certain extent. 
When we are adding services provided by google in our application we add the below dependency in our build.gradle file. This dependency is having features of all the services present in it. So this dependency is quite heavy and due to this, it will load several times when we build our application. 
implementation ‘com.google.android.gms:play-services:8.3.0’
Instead of using this dependency, we should use the dependency of a specific service that we have to use in our application. Let’s suppose we have to use maps service in our application so for that we will be specifically adding the dependency for maps rather than adding the above dependency. This will only provide us the support for using maps. This dependency will take lesser time to load compared to the one which we have used above. 
implementation ‘com.google.android.gms:play-services-maps:8.3.0’
If Android Studio has a proxy server setting and can’t reach the server then it takes a long time to build, probably it’s trying to reach the proxy server and waiting for a timeout. By removing the proxy server setting it’s working fine. Steps to Removing proxy: File > Settings > Appearance & Behavior > System settings > HTTP Proxy.
When we add many dependencies in our Android Studio project it will take much time to compile your android studio project and also manages all the dependencies in your android studio project. As the Gradle file manages all the dependencies of our project so we should use fewer number dependencies which will help us to reduce the time required for the Gradle to sync each time when we create a new apk or run our application in an emulator or device.
Android-Studio
Picked
Android
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.