Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android studio build slowed down after adding new libraries?

My application uses old architecture components. I want to move to new android architecture components.

For this purpose I added room related dependencies in the beginning, after that build was normal.

But when I tried adding dependencies for Lyfecycles, LiveData and ViewModel, as mentioned here.

Application build process slowed down considerably, 5 mins and more time is required to build apk.

Following dependecies added in app's build.gradle :

    compile "android.arch.lifecycle:runtime:1.0.0-alpha5"
    compile "android.arch.lifecycle:extensions:1.0.0-alpha5"
    annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha5"

Also I have to enable jack for Java 8 compatibility, as follows :

defaultConfig {
........
        jackOptions {
            enabled true
        }
    }

After adding all these componets build process has slowed down considerably. I tried making some custom VM option changes for some parameters by going to Help -> Edit custom VM options

-Xmx5120m

I set it to almost 5 GBs but nothing worked for me. My machine has enough hardware I believe. ( 8 GB RAM, Windows 10, 1TB HDD, AMD A8)

My application makes use of many google services, like Gmail API, Firebase APIs, some other libraries did I exhausted 64K reference limit ? But I have already enabled the multidexing as mentioned here.

Did this happened because of new architecture components or something else ? How do I make build process faster ?

Update :

One of the answer below by Budius suggested a script which will show timings taken by each build process, I executed it in my application here are the findings :

BUILD SUCCESSFUL

Total time: 18 mins 28.44 secs
Task timings:
    480ms  :app:mergeDebugResources
   2516ms  :app:processDebugResources
 487725ms  :app:transformClassesWithPreJackPackagedLibrariesForDebug
  29213ms  :app:transformClassesWithPreJackRuntimeLibrariesForDebug
    752ms  :app:transformResourcesWithMergeJavaResForDebug
 556894ms  :app:transformJackWithJackForDebug
   5184ms  :app:transformNativeLibsWithMergeJniLibsForDebug
  17524ms  :app:packageDebug

Most of the timings are taken by Jack.

I tried the canary version suggested in below answer by Bryan following is output of the timing taken for build process :

BUILD SUCCESSFUL in 6m 11s
42 actionable tasks: 33 executed, 9 up-to-date
Task timings:
    608ms  :app:preDebugBuild
    350ms  :app:mergeDebugResources
    394ms  :app:processDebugManifest
   2543ms  :app:processDebugResources
   9410ms  :app:javaPreCompileDebug
  46585ms  :app:compileDebugJavaWithJavac
    262ms  :app:compileDebugShaders
    395ms  :app:mergeDebugAssets
   5835ms  :app:packageInstantRunResourcesDebug
  98922ms  :app:transformClassesWithDesugarForDebug
    334ms  :app:transformClassesWithExtractJarsForDebug
   7765ms  :app:transformClassesWithInstantRunVerifierForDebug
  23117ms  :app:transformNativeLibsWithMergeJniLibsForDebug
  10128ms  :app:transformResourcesWithMergeJavaResForDebug
  16565ms  :app:transformClassesWithInstantRunForDebug
  11825ms  :app:transformClassesWithInstantRunSlicerForDebug
  84703ms  :app:transformClassesWithDexBuilderForDebug
  17061ms  :app:transformDexArchiveWithDexMergerForDebug
   1706ms  :app:transformDexWithInstantRunDependenciesApkForDebug
   9770ms  :app:transformDexWithInstantRunSlicesApkForDebug
  10571ms  :app:packageDebug
   1387ms  :app:buildInfoGeneratorDebug

So I removed jack & switched to this canary version, build is faster than previous for sure but still slow for use.

like image 587
Prashant Avatar asked Jul 31 '17 12:07

Prashant


People also ask

Why is my Android Studio lagging?

Android Studio needs at least 8 GB RAM to run better. Change your Hard Disk to SSD. Loading/Compiling/Designing/Writing time will be reduced even in 4GB RAM. Use Power Save mode from File Menu that will reduce lots of background work.

Why Android build takes so long?

android:android-maps-utils:0.4+' . Dynamic Dependencies slow down your build since they keep searching for the latest builds every time. To improve the performance we need to fix the version in place. Use only those dependencies that you need.

Is Android Studio heavy?

In the previous versions, the android studio was more heavy software Google is updating it and making it more scalable for developers. But it is even also heavy software which will suck your computer's ram.

How long does it take to sync Gradle in Android Studio?

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


4 Answers

A lot on your question is based of assumptions that this or that might be, that there are "lots". But time is something very easy to measure, and gradle divides the build process into several smaller tasks. So I guess your best action is to measure each task and than you can compare what's taking so long.

Here is a script I made to measure build times, just add it do the root folder of your project and on your top-level gradle file add apply from: 'time.gradle'

timer.gradle

import java.util.concurrent.TimeUnit

class TimingsListener implements TaskExecutionListener, BuildListener {
    private long startTime
    private timings = []

    @Override
    void beforeExecute(Task task) {
        startTime = System.nanoTime()
    }

    @Override
    void afterExecute(Task task, TaskState taskState) {
        def ms = TimeUnit.MILLISECONDS.convert(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
        timings.add([ms, task.path])
    }

    @Override
    void buildFinished(BuildResult result) {
        println "Task timings:"
        for (timing in timings) {
            if (timing[0] >= 250) {
                printf "%7sms  %s\n", timing
            }
        }
    }

    @Override
    void buildStarted(Gradle gradle) {}

    @Override
    void projectsEvaluated(Gradle gradle) {}

    @Override
    void projectsLoaded(Gradle gradle) {}

    @Override
    void settingsEvaluated(Settings settings) {}
}

gradle.addListener new TimingsListener()
like image 62
Budius Avatar answered Oct 16 '22 01:10

Budius


The Jack Toolchain is deprecated, and was still in an experimental phase before its deprecation. Although the process of generating code can be slow (as @FlorescuGeorgeCătălin mentioned) it doesn't usually cause such excessively slow build times. I suspect the cause of your slow build times is the Jack Toolchain; as it is notoriously slow.

If you want Java 8 language features I suggest you move to the canary version of Android Studio 3.0 which has them built-in.

If that isn't an option you could use Retrolambda instead, which includes most of the same Java 8 language features.

like image 20
Bryan Avatar answered Oct 16 '22 00:10

Bryan


Since the library has an annotation processor, he must generate new code into generated classes. Dagger is also a library that generate code. Butterknife the same.

You can found them into app/build/generated project folder.

You can check more about annotation processor here.

Also can be from your HDD. An SSD could give you much more processing power.

like image 34
Florescu Cătălin Avatar answered Oct 16 '22 01:10

Florescu Cătălin


Did you set Gradle to Offline Work and try to Edit custom VM options to bigger heap size?

Another solution is upgrade Android Studio to Android Studio 3.0 which also increase the build speed

like image 27
nhoxbypass Avatar answered Oct 15 '22 23:10

nhoxbypass