Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android studio is slow in gradle building

Android studio is getting slow in grade building process.I noticed this problem after updating to new version 3.5.Is there any ways to speed up the building process?

like image 841
athul Avatar asked Sep 01 '19 11:09

athul


People also ask

Why is my Gradle build slow?

Resources take up a sizable amount of space in your APK, and packaging all these resources slows down your build. For development builds, you can tell Gradle only to package the resources you care about, for the device you are developing on.

Why does Gradle project take so long to sync?

Check your Internet connection. If internet speed is very slow gradle build will also take long time to build. I check by change my wifi internet connection with another one good speed connection.

Why does Android Studio take so long?

According to Android Studio's official system requirements, it takes at minimum 3 GB RAM to run smoothly. Honestly, its a lot and I believe that is the biggest cause of being it too slow all the time. The android developers are always complaining about the speed of Android Studio and how its slow ALL THE TIME.


Video Answer


2 Answers

1- Make sure you’re using the latest version of Gradle. Generally with every new update there is a significant improvement in performance. Note: Java 1.8 is faster than 1.6. Make sure it’s updated too.

2- Try to minimize the use of modules. There are many cases where we need to fork the library to modify it to fit according to our needs. A module takes 4x greater time than a jar or aar dependency. This happens due to the fact that the module needs to be built from the scratch every time.

3- Enable gradle Offline Work from Preferences-> Build, Execution, Deployment-> Build Tools-> Gradle. This will not allow the gradle to access the network during build and force it to resolve the dependencies from the cache itself.

Note: This only works if all the dependencies are downloaded and stored in the cache once. If you need to modify or add a new dependency you’ll have to disable this option else the build would fail.

4-Open up the gradle.properties file from the root of your project. Add the following lines of code in it.

org.gradle.daemon=true

Gradle daemon is a background process. Adding this would consume some extra memory while building.

org.gradle.parallel=true

The above line of code enables compilation of multiple modules at the same time. Besides that it also gives us other benefits such as;

Re-using the configuration for unchanged projects Project-level is up-to-date checks Using pre-built artifacts in the place of building dependent projects Adding the following line of code also aids us in speeding up the build.

org.gradle.configureondemand=true

Another important property is;

org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

The above line is used to allow Java compilers to have available memory up to 2 GB (2048 MB). It should only be used if you have available memory more than 2 GB.

This is how the gradle.properties file should look like:

enter image description here

5- Avoid dynamic dependencies such as compile 'com.google.maps.android:android-maps-utils:0.4+'. Dynamic Dependencies slow down your build since they keep searching for the latest builds every time. To improve the performance we need to fix the version in place.

6- Use only those dependencies that you need. For example google maps dependency, instead of importing , like :

 implementation 'com.google.android.gms:play-services:17.0.0' 
 implementation 'com.google.android.gms:play-services-maps:17.0.0'
like image 131
mohammadReza Abiri Avatar answered Oct 04 '22 01:10

mohammadReza Abiri


Gradle build speed depends on a lot of factors including the specification of your machine as well as your build type and android studio settings. You can check out this article on how to reduce your build time or go through the steps in the Android developer website.


Personal experience:

When I faced this issue, enabling offline mode drastically reduced my build time. The only problem is that I have to toggle this setting (on and off) every time I want to add a new dependency and this almost made me go berserk on several occasions. However, if properly handled, this helps a great deal.

I hope this helps. Merry coding!

like image 34
Taslim Oseni Avatar answered Oct 04 '22 01:10

Taslim Oseni