Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VERY VERY slow Gradle build on Android Studio

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.

like image 227
TootsieRockNRoll Avatar asked Jul 26 '17 12:07

TootsieRockNRoll


People also ask

Why my Gradle build is slow?

This happens due to the fact that the module needs to be built from the scratch every time. 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.

How long does Gradle build take?

gradle, if you have minifyEnabled for debug, remove it. I had it in my project and it took ~2-3 minutes to build.

Why is Android Studio running so slow?

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.

How do I speed up Gradle task assembleDebug?

Enable build caching For example, ./gradlew assembleDebug --build-cache . You can also configure build caching on your gradle. properties file. Estimates that this will improve your full clean build by up to 3 times faster, and incremental builds by up to 10 times faster!


2 Answers

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 
like image 155
Ali mohammadi Avatar answered Oct 09 '22 21:10

Ali mohammadi


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

like image 25
Athar Iqbal Avatar answered Oct 09 '22 21:10

Athar Iqbal