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

前端 未结 28 1926
太阳男子
太阳男子 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:40

    You could try opening the gradle menu on the right side of studio, and assemble only the modules you have changed, then run the install command. When you press run it assembles everything regardless of any changes you you may have made to the code it is assembling

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

    Speed Up Gradle Build In Android Studio 3.2.1

    Ever feel like you are waiting for the builds to complete in Android Studio for minutes? Me too. And it’s a pretty annoying. Fortunately, there are a few ways that you can use to improve this. Android uses Gradle for building. The latest version is 4.6 has a huge performance boost over previous versions (see Release notes for details).

    Step 1: Update Gradle version An easier way to accomplish this is to go to: Open Module Settings (your project) > Project Structure

    UPDATE

    Change to Gradle version: 4.6 and Change to Android Plugin Version: 3.2.1

    Download Gradle Release distributive from https://services.gradle.org/distributions/gradle-4.6-all.zip And copy it to the Gradle folder:

    Last step is to add your discribution in Settings > Gradle

    Don’t forget to click Apply to save changes.

    Step 2: Enable Offline mode, Gradle daemon and parallel build for the project Offline mode tells Gradle to ignore update-to-date checks. Gradle asks for dependencies everytime and having this option makes it just uses what is already on the machine for dependencies. Go to Gradle from android studio Setting and click in Offline work box.

    1. Go to Compiler from android studio Setting and add “— offline” in command-line box and click Compile independent modules in parallel.

    The next step is to enable the Gradle daemon and parallel build for your project. Parallel builds will cause your projects with multiple modules (multi-project builds in Gradle) to be built in parallel, which should make large or modular projects build faster.

    These settings could enabled by modifiing a file named gradle.properties in Gradle scripts directory(i.e., ~/.gradle/gradle.properties).Some of these options (e.g. Complie modules in parallel) are available from Android Studio and also enabled there by default, but putting them in the gradle.properties file will enabled them when building from the terminal and also making sure that your colleagues will use the same settings. But if you’re working on a team, sometimes you can’t commit this stuff.

    # When configured, Gradle will run in incubating parallel mode.
    # This option should only be used with decoupled projects. More details, visit org.gradle.parallel=true
    # When set to true the Gradle daemon is used to run the build. For local developer builds this is our favorite property.
    # The developer environment is optimized for speed and feedback so we nearly always run Gradle jobs with the daemon.
     org.gradle.daemon=true
    

    Using the daemon will make your builds startup faster as it won’t have to start up the entire Gradle application every time. The Gradle Daemon is not enabled by default, but it’s recommend always enabling it for developers’ machines (but leaving it disabled for continuous integration servers). FAQ about this mode could be found here https://docs.gradle.org/current/userguide/gradle_daemon.html. The parallel builds setting could be unsafe for some projects. The requirement is that all your modules must be decoupled or your build could fail (see http://gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects for details).

    Step 3: Enable incremental dexign and tweak memory settings You can speed up your builds by turning on incremental dexing. In your module’s build file:

    Add this option to your android block:

    dexOptions {
        incremental true
    }
    

    In that dexOptions block you can also specify the heap size for the dex process, for example:

    dexOptions {
        incremental true
        javaMaxHeapSize "12g"
    }
    

    Where “12g” is 12GB of memory. Additional information about this could be found here google.github.io/android-gradle-dsl/current/ You can also configure Gradle parameters in the settings file, e.g. increase the max heap size in case you have a large project:

    # Specifies the JVM arguments used for the daemon process.
    # The setting is particularly useful for tweaking memory settings.
    # Default value: -Xmx10248m -XX:MaxPermSize=256m
    org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
    

    See all list of parameters here: https://docs.gradle.org/current/userguide/userguide_single.html#sec:gradle_configuration_properties for details.

    Step 4: Disable Antivirus Consider to exclude project and cache files from antivirus scanning. This is obviously a trade off with security. 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 Android Studio 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. I hope this helps. Leave a comment if you have any question or some other tips for improving the build performance.

    helpful link

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

    I'm far from being an expert on Gradle but my environment had the following line in .gradle/init.gradle

    gradle.projectsLoaded {
        rootProject.allprojects {
            repositories {
                mavenRepo name: 'libs-repo', url: 'http://guest-vm/artifactory/repo'
            }
        }
    }
    

    Yet I have no idea why that line was there, but I try changing to

    gradle.projectsLoaded {
        rootProject.allprojects {
            repositories {
                mavenCentral()
            }
        }
    } 
    

    and now I finally can work without swearing to Android Studio & Gradle buildind scheme.

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

    If using google play services, depending on just the libraries you need instead of the whole blob can make things faster.

    If your only need is maps, use:

    compile 'com.google.android.gms:play-services-maps:6.5.+'
    

    instead of:

    compile 'com.google.android.gms:play-services:6.5.+'
    

    The latter brings 20k methods (see blog) into the classpath, which might tip the total method count over 64k.

    That would force the use of proguard or multidex even for debug builds. For one of my projects i had the following build times

    • multidex build (with supportlibrary) ~40sec
    • proguard build ~20sec
    • build when method limit < 64k ~5sec

    If developing on sdk 21+, it would possible to optimize multidex builds as stated in the android documentation

    android {
        productFlavors {
            // Define separate dev and prod product flavors.
            dev {
                // dev utilizes minSDKVersion = 21 to allow the Android gradle plugin
                // to pre-dex each module and produce an APK that can be tested on
                // Android Lollipop without time consuming dex merging processes.
                minSdkVersion 21
            }
            prod {
                // The actual minSdkVersion for the application.
                minSdkVersion 14
            }
        }
        ...
    }
    
    0 讨论(0)
  • 2020-11-22 08:45

    Just another performance imporvement tip:

    Android Studio 3.0 includes new DEX compiler called D8.

    "The dex compiler mostly works under the hood in your day-to-day app development, but it directly impacts your app's build time, .dex file size, and runtime performance."

    "And when comparing the new D8 compiler with the current DX compiler, D8 compiles faster and outputs smaller .dex files, while having the same or better app runtime performance."

    D8 is optional - do use it we have to put to project's gradle.properties

    android.enableD8=true
    

    More informations: https://android-developers.googleblog.com/2017/08/next-generation-dex-compiler-now-in.html

    PS. It impove my build time by about 30%.

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

    The accepted answer is for older versions of android studio and most of them works still now. Updating android studio made it a little bit faster. Don't bother to specify heap size as it'll increase automatically with the increase of Xms and Xmx. Here's some modification with the VMoptions

    1. In bin folder there's a studio.vmoptions file to set the environment configuration. In my case this is studio64.vmoptions Add the following lines if they're not added already and save the file. In my case I've 8GB RAM.

      -Xms4096m
      -Xmx4096m
      -XX:MaxPermSize=2048m
      -XX:+CMSClassUnloadingEnabled
      -XX:+CMSPermGenSweepingEnabled 
      -XX:+HeapDumpOnOutOfMemoryError
      -Dfile.encoding=utf-8`
      
    2. Start android studio. Go to File-> Settings-> Build, Execution, Deployment-> Compiler

      • Check compile independent modules in parallel
      • In command-line Options write: --offline
      • Check Make project automatically
      • Check configure on demand

    In case of using mac, at first I couldn't find the vmoptions. Anyway, here's a nice article about how we can change the vmoptions in MAC OSX. Quoting from this article here.

    Open your terminal and put this command to open the vmoptions in MAC OSX:

    open -e /Applications/Android\ Studio.app/Contents/bin/studio.vmoptions
    
    0 讨论(0)
提交回复
热议问题