47

here's my config:

MBP 2015, 8gb RAM, SSD

Android Studio 2.2.3

In average, the build lasts 7/8 minutes, sometimes 10/12 minutes

I have tried EVERYTHING I could find online but none seem to work:

--- custom VM option:

-Xms1024m
-Xmx8192m
-XX:MaxPermSize=1024m
-XX:ReservedCodeCacheSize=440m
-XX:+UseCompressedOops
-XX:+HeapDumpOnOutOfMemoryError
-Dfile.encoding=UTF-8

--- Compiler / configure on demand

--- Compiler / compile modules in parallel

--- Gradle / work offline

--- gradle.properties file:

org.gradle.daemon=true
org.gradle.jvmargs=-Xmx8192m -XX:MaxPermSize=1024m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 -noverify
org.gradle.parallel=true
org.gradle.configureondemand=true
android.enableBuildCache=true

Does anyone have any idea what's going on because it is just ridiculous

UPDATE:

Since then I switched to a 16gb RAM, there is obviously some improvement, I was under the impression that it would be less than a minute or so, but It takes 3-4 minutes, which is still not acceptable: here's part of the Gradle log that I found takes too long:

git log --pretty=format:'%h %s'
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preAlphaBuild UP-TO-DATE
:app:preBirdsBuild UP-TO-DATE
:app:preKfgBuild UP-TO-DATE
:app:prePenBuild UP-TO-DATE
:app:prePrelaunchBuild UP-TO-DATE
:app:prePtr1Build UP-TO-DATE
:app:prePtr3Build UP-TO-DATE
:app:preReleaseBuild UP-TO-DATE
:app:prepareCnPedantSweetalertLibrary13Library
:app:prepareComAndroidSupportAnimatedVectorDrawable2511Library
:app:prepareComAndroidSupportAppcompatV72511Library
:app:prepareComAndroidSupportCardviewV72511Library
:app:prepareComAndroidSupportDesign2511Library
:app:prepareComAndroidSupportMultidex101Library
:app:prepareComAndroidSupportRecyclerviewV72511Library
:app:prepareComAndroidSupportSupportCompat2511Library
:app:prepareComAndroidSupportSupportCoreUi2511Library
:app:prepareComAndroidSupportSupportCoreUtils2511Library
:app:prepareComAndroidSupportSupportFragment2511Library
:app:prepareComAndroidSupportSupportMediaCompat2511Library
:app:prepareComAndroidSupportSupportV132511Library
:app:prepareComAndroidSupportSupportV42511Library
:app:prepareComAndroidSupportSupportVectorDrawable2511Library
:app:prepareComAndroidSupportTransition2511Library
:app:prepareComCrashlyticsSdkAndroidAnswers1310Library
:app:prepareComCrashlyticsSdkAndroidBeta122Library
:app:prepareComCrashlyticsSdkAndroidCrashlytics265Library
:app:prepareComCrashlyticsSdkAndroidCrashlyticsCore2314Library
:app:prepareComDevbracketsAndroidExomedia311Library
:app:prepareComEftimoffAndroidViewpagerTransformers101Library
:app:prepareComGithubCastorflexSmoothprogressbarLibrary110Library
:app:prepareComGithubHotchemiPermissionsdispatcher230Library
:app:prepareComGithubKibotuAndroidAnimationsActions107Library
:app:prepareComGithubKibotuBloodHound105Library
:app:prepareComGithubKibotuCommonAndroidUtils072Library
:app:prepareComGithubKibotuGooglePlayForceUpdate108Library
:app:prepareComGithubKibotuNetKibotuAndroidDeviceinfo121Library
:app:prepareComGithubKibotuOpenAndroidSSLWrapper107Library
:app:prepareComGithubKibotuRecyclerViewPresenter141Library
:app:prepareComGithubKibotuShareIntentBuilderV003Library
:app:prepareComGithubKibotuTimeBomb110Library
:app:prepareComGithubKibotuUrlShortener107Library
:app:prepareComGithubOrhanobutHawk123Library
:app:prepareComGithubOzodrukhCircularReveal201Library
:app:prepareComGoogleAndroidExoplayerExoplayerR1513Library
:app:prepareComGoogleAndroidGmsPlayServicesAds1001Library
:app:prepareComGoogleAndroidGmsPlayServicesAdsLite1001Library
:app:prepareComGoogleAndroidGmsPlayServicesAnalytics1001Library
:app:prepareComGoogleAndroidGmsPlayServicesAnalyticsImpl1001Library
:app:prepareComGoogleAndroidGmsPlayServicesBase1001Library
:app:prepareComGoogleAndroidGmsPlayServicesBasement1001Library
:app:prepareComGoogleAndroidGmsPlayServicesClearcut1001Library
:app:prepareComGoogleAndroidGmsPlayServicesGass1001Library
:app:prepareComGoogleAndroidGmsPlayServicesGcm1001Library
:app:prepareComGoogleAndroidGmsPlayServicesIid1001Library
:app:prepareComGoogleAndroidGmsPlayServicesLocation1001Library
:app:prepareComGoogleAndroidGmsPlayServicesTagmanagerV4Impl1001Library
:app:prepareComGoogleAndroidGmsPlayServicesTasks1001Library
:app:prepareComGoogleFirebaseFirebaseAnalytics1001Library
:app:prepareComGoogleFirebaseFirebaseAnalyticsImpl1001Library
:app:prepareComGoogleFirebaseFirebaseCommon1001Library
:app:prepareComGoogleFirebaseFirebaseCore1001Library
:app:prepareComGoogleFirebaseFirebaseIid1001Library
:app:prepareComGoogleFirebaseFirebaseMessaging1001Library
:app:prepareComJakewhartonButterknife840Library
:app:prepareComJakewhartonProcessPhoenix110Library
:app:prepareComMikepenzFastadapter210Library
:app:prepareComMikepenzIconicsCore280Library
:app:prepareComMikepenzMaterialdrawer581Library
:app:prepareComMikepenzMaterialize100Library
:app:prepareComMukeshMarkdownview100Library
:app:prepareComNightonkeJellytogglebutton102Library
:app:prepareComNightonkeWowoviewpager102Library
:app:prepareComOgaclejapanSmarttablayoutLibrary161Library
:app:prepareComOgaclejapanSmarttablayoutUtilsV4161Library
:app:prepareComPnikosisMaterialishProgress10Library
:app:prepareComSbrukhandaFragmentviewpagerFragmentviewpager100Library
:app:prepareComShawnlinNumberPicker101Library
:app:prepareComSloydevPreferator100Library
:app:prepareComYydcdutRxmarkdown005Library
:app:prepareComZplesacConnectionbuddy140Library
:app:prepareHanksXyzHtextviewLibrary015Library
:app:prepareIoFabricSdkAndroidFabric1314Library
:app:prepareIoNlopezSmartlocationLibrary329Library
:app:prepareIoReactivexRxandroid121Library
:app:prepareJpWasabeefGlideTransformations201Library
:app:prepareJpWasabeefRecyclerviewAnimators225Library
:app:prepareMeGrantlandAutofittextview021Library
:app:prepareMeRelexCircleindicator122Library
:app:prepareNetDanlewAndroidJoda297Library
:app:prepareUkCoChrisjenxCalligraphy220Library
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig
:app:mergeDebugShaders UP-TO-DATE
:app:compileDebugShaders UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:fabricGenerateResourcesDebug
:app:generateDebugResValues UP-TO-DATE
:app:processDebugGoogleServices
Parsing json file: ../google-services.json
:app:generateDebugResources
:app:mergeDebugResources
:app:processDebugResources
:app:generateDebugSources
:app:incrementalDebugJavaCompilationSafeguard
:app:javaPreCompileDebug
:app:compileDebugKotlin

up until here is fine, but the :app:compileDebugKotlin takes around 60+ seconds ...

and then,

...
...
:app:copyDebugKotlinClasses
:app:compileDebugNdk NO-SOURCE
:app:compileDebugSources
:app:transformClassesWithRetrolambdaForDebug
:app:transformClassesWithDexForDebug

This last line stays for so long as well.

TootsieRockNRoll
  • 3,218
  • 2
  • 25
  • 50

8 Answers8

58

For Kotlin users:

Incremental builds

One of the most important performance features of a compiler is its use of incremental compilation. A normal build will recompile all source files in a project, but an incremental build will keep track of which files have changed since the last build and only recompile those files and the files that depend on them. This can have a huge impact on compile times, especially for large projects.

Incremental builds were added to Kotlin in version 1.0.2, and you can enable them by adding kotlin.incremental=true to your gradle.properties...

See "Kotlin vs Java: Compilation speed" for more information.

Also adding this code to gradle.properties could be useful:

# Add this in your global gradle.properties file 
# at ~/.gradle/gradle.properties
# Enable Gradle Daemon
org.gradle.daemon=true
# Enable Configure on demand
org.gradle.configureondemand=true
# Enable parallel builds
org.gradle.parallel=true
# Enable Build Cache
android.enableBuildCache=true
# Enable simple gradle caching
org.gradle.caching=true
# Increase memory allotted to JVM
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m - 
XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

And if all of your dependencies are downloaded from the repo center you can use this to make gradle offline form:

File->Setting->Build, Execution, Deploymennt->Gradle
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Ali mohammadi
  • 1,839
  • 26
  • 27
  • 2
    Finally something that helped! Thanks! – A1m Feb 01 '19 at 03:22
  • 5
    Thank you! It is really stunning, I improved my build time from 2 minutes to 2 seconds – J. Hegg Jul 16 '19 at 10:14
  • 5
    stuff like this should really be the default – felix-ht May 26 '20 at 19:50
  • Should REALLY be the default. WHY Intellij?! – John Glen Feb 24 '21 at 01:09
  • 2
    This made my project, which has very many modules, R8 build time from 20 minutes to 4 minutes 30 seconds. This should really be a default. – March3April4 May 14 '21 at 09:04
  • 1
    In 2022, how many of these settings are still necessary (because some might now be defaults or obsolete)? – Mark Apr 11 '22 at 01:59
  • 2
    In 2022: android.useAndroidX=true # Automatically convert third-party libraries to use AndroidX android.enableJetifier=true # Kotlin code style for this project: "official" or "obsolete": kotlin.code.style=official # Enable Gradle Daemon org.gradle.daemon=true # Enable Configure on demand org.gradle.configureondemand=true # Enable parallel builds org.gradle.parallel=true # Enable simple gradle caching org.gradle.caching=true # Increase memory allotted to JVM org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 – Shogun Nassar Jun 10 '22 at 12:32
17
File->Setting->Build, Execution, Deploymennt->Gradle

Check Offline work
Click Apply and Ok

Note: In newer version of Android studio, View->Tool Windows->Gradle->Toggle button of online/offline

Uddhav P. Gautam
  • 7,362
  • 3
  • 47
  • 64
Athar Iqbal
  • 458
  • 3
  • 12
  • There is no reason for a well-configured gradle build to download anyt large files after the first run. – Vaiden Jul 26 '17 at 13:06
  • 1
    if you are use play services dependency like this `compile 'com.google.android.gms:play-services:9.2.1'` don't use this one because play services has thousand of the classes which will be loaded into a project and Gradle will be slow...use well-defined dependency like this if you use GCM notification `compile 'com.google.android.gms:play-services-gcm:9.2.1'` – Athar Iqbal Jul 26 '17 at 13:30
5

You need to post some more information for us to help.

Post your build.gradle files & how many modules does your app have & gradle.properties file & settings.gradle file.

You could also take a look at Activiy Monitor on your Mac, and see what else is running in the background that might take up some resources.
8 GB of RAM is not much, so you should try and shut down other elements which may be taking up some of that RAM.

Try this in the app's build.gradle file, in the 'android' block:

dexOptions {
        preDexLibraries true
        javaMaxHeapSize "4G"
    }

Along with this in your gradle.properties file:

org.gradle.jvmargs=-Xmx4608M

Since you 'only' have 8 GB of RAM, you should not try and dedicate 8 GB to Gradle, like you tried to earlier.
I have 16 GB of RAM, and i use 4-5 GB for Gradle, and builds are quite fast. Even our most massive project, which is still being developed after 7 years, takes 2-3min to build from scratch.

But please, post the info i requested in the start of this post, and we can help more.

Moonbloom
  • 7,738
  • 3
  • 26
  • 38
4

If you have a large codebase it's quite normal that it takes time. Or better, it's expected to take some time. I've just noticed that you're using Kotlin on your project, which means that you have 2 stages of compilation for java code instead of just 1.

Kotlin is compiled (and java references) in first stage. After that you have a 2nd stage of compilation when plain Java is compiled. Which means that average compilation time two is times longer.

Compilation: Java code + Kotlin -> Kotlin compiler Java code + parsed Kotlin code -> Java copiler dexing and everything else

My suggestion would be, split your code in modules. Even better if you can separate modules by language. This way the Kotlin modules will be slow but java modules faster.

In case you have totally separate Java and Kotlin modules, it should take roughly the same time as just Java.

Conclusion:

Modularizing your code will speed up build. If you want Kotlin, reduce Java code, it will speed up builds. Separate Kotlin and Java modules, it will require less compilation, and less stages on some modules, so it will speed up builds.

shadox
  • 3,238
  • 4
  • 24
  • 38
4

My case was completely different. I dont know why but it was caused by this line

maven { url "https://dl.bintray.com/crysis21/Android" }

It instantly improved build speed by about 99%... So look for such links in your project and if there is any you dont need then remove them.

These links can be useful

George Shalvashvili
  • 1,263
  • 13
  • 21
1

I noticed that build of my project increased by 3 times after Android Studio (or some of it's SDK tools) upgrade. Build time increased from 5-6 minutes to 17-19 minutes.

The problem was in Kotlin version 1.6.10.

The saving was in updating of Kotlin version to 1.7.21.

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.21"

and

implementation "org.jetbrains.kotlin:kotlin-stdlib:1.7.21"

P.S. may be 1.7.xx will work too.

P.P.S. I had to update Room version to 2.4.3.

Eugene Babich
  • 1,271
  • 15
  • 25
0

A possibility is that there is an unreachable URL on your allProjects bracket of your project level build.gradle file. Since Gradle scans the URLs on that bracket in order from top to bottom and there are, let's say, 10 missing components that it needs to download, it starts to scan from the top and waits for the URL to return a response. And, there is a possibility that the URL will cause a timeout without returning a reply, and that'll cause Gradle to wait for the URL for each missing component, increasing the build time by a lot.

My suggestion would be inserting the maven roots to allProjects bracket from the most necessary to least necessary, from top to bottom and ensure all URLs are reachable. I've decreased the build time of some of my projects from 15 minutes to 30 seconds after fixing the endpoints.

It also relates to this answer where the link in question is unreachable: https://stackoverflow.com/a/67569873/6276596

Hope this helps to somebody.

Furkan Yurdakul
  • 2,801
  • 1
  • 15
  • 37
0

I have the same issue, the key point is here. You have to custom the gradle configuration. In your gradle.properties file add :

org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
#org.gradle.jvmargs=-XX:+UseParallelGC
org.gradle.unsafe.configuration-cache=true
# Use this flag sparingly, in case some of the plugins are not fully compatible
org.gradle.unsafe.configuration-cache-problems=warn

Source : https://developer.android.com/studio/build/optimize-your-build

Zhar
  • 3,330
  • 2
  • 24
  • 25