VERY VERY Slow Gradle Based on Android Studio

here is my configuration:

MBP 2015, 8 GB RAM, SSD

Android Studio 2.2.3

On average, assembly takes 7/8 minutes, sometimes 10/12 minutes

I tried EVERYTHING, I could find online, but nobody works:

--- VM custom option:

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

--- Compiler / customization on demand

--- Compiler / compilation of 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 know what is happening because it is just ridiculous.

UPDATE:

Since then I switched to 16 GB RAM, obviously, there are some improvements, I got the impression that it will be less than a minute or so, but it takes 3-4 minutes, which is still unacceptable: here is a part from Gradle magazine I found takes too much time:

 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 good here, but :app:compileDebugKotlin will take about 60 seconds ...

and then

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

This last line stays so long.

+12
source share
4 answers

For kotlin users Incremental builds

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

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

Read more: https://medium.com/keepsafe-engineering/kotlin-vs-java-compilation-speed-e6c174b39b5d

It may also be useful to add this line of code to gradle.properties

 // 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 your dependencies are downloaded from the repo center, you can make gradle a standalone form

File->Setting->Build, Execution, Deploymennt->Gradle

+12
source
 File->Setting->Build, Execution, Deploymennt->Gradle Check Offline work Click Apply and Ok 
+7
source

If you have a large code base, it is quite normal that it takes time. Or better, he expected it to take some time. I just noticed that you are using Kotlin in your project, which means that you have 2 compilation steps for Java code, not just 1.

Kotlin compiled (and Java links) in the first step. After that, you have the second stage of compilation when simple Java is compiled. This means that the average compilation time is twice as long.

Compilation: Java code + Kotlin β†’ compiler Kotlin Java code + parsed Kotlin code β†’ Java copier dexing and everything else

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

If you have completely separate Java and Kotlin modules, it will take about the same time as Java.

Output:

Modulating your code will speed up the build. If you want Kotlin, reduce the Java code, this will speed up the assembly. Separate Kotlin and Java modules, this will require less compilation and fewer steps for some modules, so it will speed up the assembly.

+4
source

You need to post additional information for us.

Publish your build.gradle files and the number of modules of your application and the gradle.properties file and settings.gradle file.

You can also take a look at Activiy Monitor on your Mac and see what else is running in the background, which may take some resources.
8 GB of RAM is not so much, so you should try and turn off other elements that may occupy part of this RAM.

Try this in the build.gradle file of the application in the "android" block:

 dexOptions { preDexLibraries true javaMaxHeapSize "4G" } 

Along with this, in the gradle.properties file:

 org.gradle.jvmargs=-Xmx4608M 

Since you only have 8 GB of RAM, you should not try to allocate 8 GB on Gradle, as you tried before.
I have 16 GB of RAM, and I use 4-5 GB for Gradle, and the builds are pretty fast. Even our largest project, which is still being developed after 7 years, takes 2-3 minutes to build from scratch.

But please post the information I requested at the beginning of this post and we can help more.

+3
source

Source: https://habr.com/ru/post/1270218/


All Articles