Android Studio using 100% CPU on an i7 processor for project Rebuild

后端 未结 4 1909
故里飘歌
故里飘歌 2021-02-05 12:19

My Windows 7 machine has a quad core i7 processor. When I Rebuild my project, it takes on average 25 seconds. And when I launch the app, it takes on average 36 seconds (before

相关标签:
4条回答
  • 2021-02-05 12:32

    To be honest, Android Studio is hands down better than Eclipse because of the UI designer. The downside is that it uses gradle instead of Ant. Gradle is also better but slower - especially on Windows. It runs much better on Linux. If you haven't used Linux before, fear not. Linux Mint is a stable OS that has a UI that is similar to Windows. You'll be right at home in no time. It consumes fewer resources so that leaves more processing power for the gradle build. Make the switch. You'll never go back.

    0 讨论(0)
  • 2021-02-05 12:34

    In addition to optimizations specific to Gradle (see below), I recommend that you try disabling anti-virus protection for your Gradle caches directory and your Android Studio project directory. For me, this reduces my build times by roughly 50%. Excluding those same directories from Windows Search indexing can also help.

    Gradle optimizations I use, in ~/.gradle/gradle.properties.

    org.gradle.daemon=true
    org.gradle.jvmargs=-Xmx6144m <-- Tweak this based on available RAM
    org.gradle.caching=true
    org.gradle.parallel=true
    kotlin.incremental=true
    

    Note that enabling caching means you sometimes have to explicitly clear your caches when switching branches. I run this script when I run into puzzling build issues.

    #!/bin/bash
    
    # Clean Android cache
    ./gradlew cleanBuildCache
    
    # Clean Gradle cache, prompting for each directory
    find ~/.gradle/caches -maxdepth 1 -name build-cache* -print -exec rm -rfI {} \;
    
    # Clean Project
    ./gradlew clean
    
    # Stop Gradle Daemon
    ./gradlew --stop
    
    0 讨论(0)
  • 2021-02-05 12:38

    Here are the three improvements I was able to make:

    I was preDexing my JARs every time I built the project, so I found this solution:

    dexOptions {
        preDexLibraries = false
    }
    

    I was using the entire Google Play Services library:

    compile('com.google.android.gms:play-services:+') {
        exclude module: 'support-v4'
    }
    

    When all I needed was Google Cloud Messenger:

    compile('com.google.android.gms:play-services-gcm:+') {
        exclude module: 'support-v4'
    }
    

    In Eclipse, I would always do a Rebuild and then launch app with the play button. In Android Studio, now I am just doing a Clean and then launch app with the play button. Also the Run button in Android Studio does NOT work every time right after the Clean. This was causing what seemed to be delays because nothing was happening. So now I leave the Gradle Console open to make sure that the run button is working, and when it doesn't I just hit it a second time.

    What I used to have:

    Rebuild: 26 seconds
    Launch:  36 seconds
    Install: 15 seconds
    

    and now:

    Clean:    8 seconds
    Launch:  22 seconds
    Install: 15 seconds
    

    which is a major improvement! Hopefully this helps someone else.

    0 讨论(0)
  • 2021-02-05 12:44

    As stated on the tracker page for this issue, the team has identified this as the problem:

    --parallel-threads only applies to project parallelization.

    For android tasks that are running in parallel, we always create as many threads as possible

    From the page, it seems that they target release 1.3 to address this (see comment #13 there).

    In the meantime, what has helped me to cope on Windows 7 is to set the CPU affinity for the Android Studio process (and its child processes) to spare at least one of the cores (as suggested by comment #9 on the page).

    There are many ways to do this, but you might want to try the top-voted answer on this superuser question (which suggested to use Process Lasso) that appears to work well enough for me.

    0 讨论(0)
提交回复
热议问题