Home
Public
Tags
Users
Jobs
How to Speed Up Android Studio

Offline Gradle, increasing memory in gradle.properties file, disabling Instant Run

First, click on File -> Settings -> Plugins to open dialog like this.

 

The Plugins section in Settings in Android

Now, disable or Check Off all the plugins which not usable for you. I have disabled following:

  • Android APK Support
  • Android Games
  • Android NDK
  • App Links Assistant
  • Copyright
  • Coverage
  • CVS Integeration
  • Editor Config
  • Fabric for Android Studio
  • Firebase (App Indexing, Services, Testing)
  • Github
  • Google (Cloud Tools Core, Cloud Tools for Android, Developer Samples, Login, Services)
  • Markdown Support
  • Mercurial integration
  • hg4idea
  • Settings repository
  • Subversion integration
  • Task management
  • Test recorder
  • TestNG-J
  • YAML

Now, you dialog will be something like this:

The Plugins section after disabling some plugins in Settings in Android

Now, click Apply -> OK and restart Android Studio. You’ll feel the difference instantly I bet.


  • Linux performs better for Android Studio than Windows.
  • 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.
  • Change and configure Highlighting Level to Syntax or None from bottom right corner of status-bar. Remember this will disable syntax highlighting of error code.
  • If you are using any antivirus , make sure to add Exception of all android studio directories (android, java, .android, .gradle, .androidstudio(version number) all those folders ). So it won’t try and Scan those while you are using Android Studio. It makes it even faster.

------------------------------------------------------------------------------------------------------------------------------

Step 1: Update Gradle version

An easier way to accomplish this is to go to: Open Module Settings (your project) > Project Structure

In Gradle version and enter 2.10

Changing the Gradle version in Project Structure dialog

Download Gradle Release distributive from https://services.gradle.org/distributions/gradle-2.10-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.

  1. Go to Gradle from android studio Setting and click in Offline work box.

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

Gradle options: Compiling in parallel and Offline mode

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.

----------------------------------------------------------------------------------------------------------------

1) in AndroidStudio's settings > compile enable checkbox named Compile independent modules in parallel.

2) Under Help> Edit Custom VM Options I have:

-Xms1024m 
-Xmx4096m # <------ increase this to most of your RAM 
-XX:MaxPermSize=1024m 
-XX:ReservedCodeCacheSize=440m 
-XX:+UseCompressedOops 
-XX:-HeapDumpOnOutOfMemoryError 
-Dfile.encoding=UTF-8

P.S. - Some people say Note, instead of VM options, it's better to combine can be overriden by combining those lines into one line single command in gradle.properties, like this :

org.gradle.jvmargs=-Xms1024m -Xmx4096m ......

3) I have an old dual core with 4GB ram, running ubuntu. Qs command line option I have only --offline (which specifies that the build should operate without accessing network resources). I also enabled the remaining checkboxes and now it's running ok:

  • Make project automatically

  • Use in-process building Configure on demand

  • Check the AndroidStudio's settings, under compile that the checkbox Compile independent modules in parallel is enabled.

Under Vmoptions I have

-Xmx2048m -XX:MaxPermSize=1024

I have an old dual core with 4GB ram, running ubuntu. Qs commandline option I have only --offline , which specifies that the build should operate without accessing network resources. I enabled also the remaining checkboxes:

  1. Make project automatically
  2. Use in-process building
  3. Configure on demand

    and it is running ok

Edit

It is possible to provide additional options through studio.vmoptions located at (just replace X.X with version):

  • Windows: go to %USERPROFILE%\.AndroidStudioX.X\studio.exe.vmoptions (or studio64.exe.vmoptions)

  • Mac~/Library/Preferences/.AndroidStudioX.X/studio.vmoptions

  • Linux~/.AndroidStudioX.X/studio.vmoptions (and/or studio64.vmoptions)

Increasing the value of -Xmx should help a lot. E.g

-Xms1024m
-Xmx4096m
-XX:MaxPermSize=1024m
-XX:ReservedCodeCacheSize=256m
-XX:+UseCompressedOops

will assign 4G as max heap, with initial value of 1G

Edit:

On windows the defaults are stored into C:\Program Files\Android\Android Studio\bin\*.vmoptions. The IDE allows you to tweak those values through Help->Edit Custom VM options (thanks to @Code-Read for pointing it out).

EDIT 2:

Android studio 3.5 makes easier to change same of those values. Just go to:

Preferences > Appearance & Behavior > System Settings > Memory Settings