Sunday, 23 March 2025

Pro Tips for Optimizing Android Gradle Builds

 Following the configuration changes per your system RAM to make gradle build faster for the android project.


Step:-1 In gradle.properties turn on below line.


org.gradle.parallel=true: Excellent for parallel task execution.


Step:-2 gradle=build -x lint -x lintVitalBasicRelease

Good for development builds, excluding time-consuming lint checks. Remember to run these before release!


Step3: This is important one,Increase jvmargs memory

org.gradle.jvmargs=-Xmx8g -XX:MaxMetaspaceSize=512m -Dkotlin.daemon.jvm.options="-Xmx8g" -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 -XX:+UseParallelGC:

  • -Xmx8g: 8GB heap for Gradle daemon. Suitable for larger projects.

  • -XX:MaxMetaspaceSize=512m: Metaspace size. Increase if you get OutOfMemoryError: Metaspace.

  • -Dkotlin.daemon.jvm.options="-Xmx8g": Correctly sets Kotlin daemon memory.

  • -XX:+HeapDumpOnOutOfMemoryError: Helps in debugging memory issues.

  • -Dfile.encoding=UTF-8: Good for file encoding.

  • -XX:+UseParallelGC: Enables parallel garbage collection.


Make sure the Heap memory should be increased based on host system ram range.


Step:4 Uses AndroidX,

android.useAndroidX=true

Step:5 Disables Jetifier.

android.enableJetifier=false: Disables Jetifier. This is good if you've migrated fully to AndroidX. If you still have dependencies using the old support library, this needs to be true (or the line removed, as the default is true).

Step:6 nonTransitiveRClass

android.nonTransitiveRClass=false: This can significantly improve build times, but can also cause runtime errors if not handled carefully.

Step:6 nonFinalResIds

android.nonFinalResIds=false: Prevents non-final resource IDs. Generally, keeping this false is safer, but there are potential performance gains in specific situations if set to true.


See below is your gradle.properties
org.gradle.parallel=true 
org.gradle.configuration-cache=true # Enable configuration caching 
org.gradle.caching=true # Enable the Gradle build cache
gradle=build -x lint -x lintVitalBasicRelease # Exclude lint for faster dev builds 
org.gradle.jvmargs=-Xmx8g -XX:MaxMetaspaceSize=512m -Dkotlin.daemon.jvm.options="-Xmx8g" -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 -XX:+UseParallelGC android.useAndroidX=true 
android.enableJetifier=false # Or true, depending on your dependencies android.defaults.buildfeatures.buildconfig=true 
android.nonTransitiveRClass=true # Enable non-transitive R classes (TEST THOROUGHLY) android.nonFinalResIds=false # Keep false unless you know what you're doing