Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Large build time using Android Studio 3.1

I am using android studio v 3.1. Operating System - Windows 10. Core i5 with 16 GB ram. Below is the result of build profiling

:app:transformClassesWithDesugarForDevelopmentQuickbuild 35.137s

:app:compileDevelopmentQuickbuildJavaWithJavac 31.917s

:app:transformClassesWithDexBuilderForDevelopmentQuickbuild 28.579s

:app:compileDevelopmentQuickbuildKotlin 20.145s

:app:transformClassesWithMultidexlistForDevelopmentQuickbuild 16.873s

:app:mergeDevelopmentQuickbuildResources 16.363s

:app:transformResourcesWithMergeJavaResForDevelopmentQuickbuild 7.958s

:app:transformNativeLibsWithMergeJniLibsForDevelopmentQuickbuild 6.483s

:app:processDevelopmentQuickbuildResources 4.835s

Projects gradle.properties has below configuration

org.gradle.parallel=true
org.gradle.configureondemand=true
org.gradle.daemon=true
android.enableBuildCache=true
kotlin.incremental=true

still for any single line of change build takes at least 2.3 minutes. This problem occurs only in windows and on ubuntu same configuration takes 15-20 seconds. What else can be done to reduce build time?

like image 844
Mangesh Kadam Avatar asked Mar 30 '18 08:03

Mangesh Kadam


2 Answers

In (Android Studio 3.1) in gradle.properties add these 2 lines :-

 android.enableD8.desugaring=true
 android.enableD8=true

Also for multidexing you can add

 multiDexEnabled true

to defaultConfig

like image 143
Santanu Sur Avatar answered Oct 10 '22 17:10

Santanu Sur


I wrote below all the usual tricks.

Also in my machines the Windows versus Linux gradle battle is won by the Linux. However increasing the heap size, may help you get closer to your ubuntu compile time.

Increase the heap size

Starting from the Android Studio 2.0, all dex in the process runs in a single VM and that VM is also shared with the gradle. This requires more memory to accommodate all toghther.

By default, the heap size under Windows is 1GB. You have to increase the heap size. The more RAM you have the more you can use.

For an 8Gb RAM developper machine i found that a 3GB heap size is best choice. For your 16GB machine you are free to experiment with a much more bigger heap size.

How to do it?

Add the line below to your gradle.properties:

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

Parallelise build

(I saw you already did it, but i mention here for anybody else to read.)

If you have multiple modules in you project, allow gradle to build your project in parallel.

How to do it?

Add the line below to your gradle.properties:

org.gradle.parallel=true

Configure on demand

(I saw you already did it, but i mention here for anybody else to read.)

Gradle provides — configure-on-demand flag that will tell gradle to only build the components that it really needs.

Basically, it tells Gradle to configure modules that are only relevant to the requested tasks instead of configuring all of them.

This setting is usually relevant for multiple modules projects but notice libraries your project may also benefit for using this flag.

For example Google I/O app has two components Android (contains the source code related to the android application) and Server (contains code related to backend server). With default graddle settings they are both configured when compilling.

How to do it?

Add the line below to your gradle.properties:

org.gradle.configureondemand=true

... or access Preferences > Build, Execution, Deployment > Compiler and check configure on demand option.

Enable Gradle daemon

(I saw you already did it, but i mention here for anybody else to read.)

Gradle runs on the Java Virtual Machine (JVM) and uses several supporting libraries that require a non-trivial initialization time. As a result, it can sometimes seem a little slow to start. The solution to this problem is the Gradle Daemon: a long-lived background process that executes your builds much more quickly than would otherwise be the case.

You won’t be able to see the time difference in your first build, but build time will decrease in subsequent builds AFTER gradle daemon is initialised.

If you are using the gradle version 3.0 or above, the gradle daemon is by default enabled. If you are running on older versions it's not.

How to do it?

Add the line below to your gradle.properties:

org.gradle.daemon=true
like image 41
Grigore Madalin Avatar answered Oct 10 '22 19:10

Grigore Madalin