Building and running app via Gradle and Android Studio is slower than via Eclipse

前端 未结 28 1928
太阳男子
太阳男子 2020-11-22 08:21

I have a multi-project (~10 modules) of which building takes about 20-30 seconds each time. When I press Run in Android Studio, I have to wait every time to rebuild the app,

相关标签:
28条回答
  • 2020-11-22 08:52

    After change this settings my compile duration 10 minutes changed to ~10 secs.

    Step 1:

    Settings(ctrl+Alt+S) ->

    Build,Execution,Deployment ->

    Compiler ->

    type "--offline" in command-line Options box.

    Step 2:

    check the “Compile independent modules in parallel” checkbox.

    & click Apply -> OK

    Reference - https://www.sundoginteractive.com/blog/speed-up-gradle-in-android-studio

    Disadvantage:

    You will not be able to pull down the latest versions of the dependencies identified in your build.gradle file. It runs faster because it uses a cached snapshot of those imported libraries.

    Important Note: When you deploy the application remove this settings & build with latest versions of dependencies.

    0 讨论(0)
  • 2020-11-22 08:55

    I recently bought a new SSD and went from Windows to Linux.My build times are now an order of magnitude faster, and no longer annoying.

    Though it does not directly answer your question as to why it's slower than eclipse, it shows that the process is disk-bounded and an upgrade to an SSD might be a (somewhat expensive) solution. I'm guessing there will be people googling the issue and ending up here, who might appreciate my experience.

    0 讨论(0)
  • 2020-11-22 08:57

    Solved mine with

    File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle -> Offline work

    Gradle builds went from 8 minutes to 3 seconds.

    0 讨论(0)
  • 2020-11-22 08:57

    USE this sudo dpkg --add-architecture i386 sudo apt-get update sudo apt-get install libncurses5:i386 libstdc++6:i386 zlib1g:i386

    Android Studio fails to build new project, timed out while wating for slave aapt process

    0 讨论(0)
  • 2020-11-22 08:58

    Hardware

    I'm sorry, but upgrading development station to SSD and tons of ram has probably a bigger influence than points below combined.

    Tools versions

    Increasing build performance has major priority for the development teams, so make sure you are using latest Gradle and Android Gradle Plugin.

    Configuration File

    Create a file named gradle.properties in whatever directory applies:

    • /home/<username>/.gradle/ (Linux)
    • /Users/<username>/.gradle/ (Mac)
    • C:\Users\<username>\.gradle (Windows)

    Append:

    # IDE (e.g. Android Studio) users:
    # Settings specified in this file will override any Gradle settings
    # configured through the IDE.
    
    # For more details on how to configure your build environment visit
    # http://www.gradle.org/docs/current/userguide/build_environment.html
    
    # The Gradle daemon aims to improve the startup and execution time of Gradle.
    # When set to true the Gradle daemon is to run the build.
    # TODO: disable daemon on CI, since builds should be clean and reliable on servers
    org.gradle.daemon=true
    
    # Specifies the JVM arguments used for the daemon process.
    # The setting is particularly useful for tweaking memory settings.
    # https://medium.com/google-developers/faster-android-studio-builds-with-dex-in-process-5988ed8aa37e#.krd1mm27v
    org.gradle.jvmargs=-Xmx5120m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
    
    # When configured, Gradle will run in incubating parallel mode.
    # This option should only be used with decoupled projects. More details, visit
    # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
    org.gradle.parallel=true
    
    # Enables new incubating mode that makes Gradle selective when configuring projects. 
    # Only relevant projects are configured which results in faster builds for large multi-projects.
    # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:configuration_on_demand
    org.gradle.configureondemand=true
    
    # Set to true or false to enable or disable the build cache. 
    # If this parameter is not set, the build cache is disabled by default.
    # http://tools.android.com/tech-docs/build-cache
    android.enableBuildCache=true
    

    Gradle properties works local if you place them at projectRoot\gradle.properties and globally if you place them at user_home\.gradle\gradle.properties. Properties applied if you run gradle tasks from console or directly from idea:

    IDE Settings

    It is possible to tweak Gradle-IntelliJ integration from the IDE settings GUI. Enabling "offline work" (check answer from yava below) will disable real network requests on every "sync gradle file".

    IDE settings

    Native multi-dex

    One of the slowest steps of the apk build is converting java bytecode into single dex file. Enabling native multidex (minSdk 21 for debug builds only) will help the tooling to reduce an amount of work (check answer from Aksel Willgert below).

    Dependencies

    Prefer @aar dependencies over library sub-projects.

    Search aar package on mavenCentral, jCenter or use jitpack.io to build any library from github. If you are not editing sources of the dependency library you should not build it every time with your project sources.

    Antivirus

    Consider to exclude project and cache files from antivirus scanning. This is obviously a trade off with security (don't try this at home!). But if you switch between branches a lot, then antivirus will rescan files before allowing gradle process to use it, which slows build time (in particular AndroidStudio sync project with gradle files and indexing tasks). Measure build time and process CPU with and without antivirus enabled to see if it is related.

    Profiling a build

    Gradle has built-in support for profiling projects. Different projects are using a different combination of plugins and custom scripts. Using --profile will help to find bottlenecks.

    0 讨论(0)
  • 2020-11-22 08:59

    You can ignore gradle update-to-date checks.

    enter image description here

    For Windows running Android Studio 1.5: Go to File -> Settings -> Build, Execution, Deployment -> Build tools -> Gradle -> Check Offline work (as shown in image)

    down from ~30+ sec to ~3 sec

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