Android: Speed up Gradle build process in Android Studio


These techniques will help you to make the Gradle build process faster.

Technique #1

  1. Open up gradle.properties file
  2. add the following line

org.gradle.daemon=true

Side effects

  1. Gradle daemon is a background process. If you use this, it might take (300-400) MB memory or more from your system.
  2. After adding the above line, try to Rebuild the project. If you get error, something like, Unable to locate a Java Runtime to invoke, very obvious solution, remove the line.

Technique #2

  1. Open up Android Studio Settings (PC/Linux) or Preference (Mac)
  2. check Compile independent modules in parallel

prefs_as

Alternatively,

  1. Open up gradle.properties file
  2. add the following line

org.gradle.parallel=true

Technique #3

  1. Open up Android Studio Settings (PC/Linux) or Preference (Mac)
  2. check Configure on demand

prefs_as2

Alternatively,

  1. Open up gradle.properties file
  2. add the following line

org.gradle.configureondemand=true

Technique #4

  1. Open up gradle.properties file
  2. add the following line

org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

Side effects

The above line means, you are allowing Java compilers to have available memory up to 2 GB (2048 MB). If you do not have available memory to give out 2 GB to Java then you should not add this line.

Another way to say this, If you have enough memory then you can try to tweak the settings.

Technique #5

All the above changes to gradle.properties file can be configures globally instead of changing the settings for every individual project.

Modify or create a file named gradle.properties in the following directory.

For Mac,
/Users/USER_NAME/.gradle/gradle.properties

For Linux,
/home/USER_NAME/.gradle/gradle.properties

For Windows,
C:\Users\USER_NAME\.gradle\gradle.properties

and apply the above changes. Finally it should look like,


org.gradle.daemon=true

# 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

org.gradle.configureondemand=true

# 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

Technique #6

  1. Open up Android Studio Settings (PC/Linux) or Preference (Mac)
  2. check Compile independent modules in parallel

prefs_as3

Side effects

Apply this option after all of your libraries are downloaded and configured correctly. Gradle try to resolve the dependencies from internet, every time we build our project. This option will force the gradle to resolve the dependencies from cache. You may need to uncheck the option if you add/modify a library.

Technique #7

If you are using google play services, then try to use only the library you need. For example, if you need maps, then use

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

instead of:

compile 'com.google.android.gms:play-services:7.5.0'

Technique #8

Consider using separate flavors for development and production.

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
        }
    }
          ...
    buildTypes {
        release {
            runProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                                                 'proguard-rules.pro'
        }
    }
}
dependencies {
  compile 'com.android.support:multidex:1.0.0'
}

for further details,

https://developer.android.com/tools/building/multidex.html#dev-build

Technique #9

Try to use Gradle 2.4, it has a huge performance boost over previous versions.

  1. Open up Android Studio Project Structure option. File -> Project Structure
  2. Write value 2.4 as Gradle Version

prefs_as4

Technique #10

  1. Open up app level build.gradle file. (Inside app directory)
  2. add the following line

dexOptions {
incremental true
}

android {
    compileSdkVersion 22
    buildToolsVersion 1.2.3

    defaultConfig {
        applicationId "com.wordpress.tausiq.sampleapp"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    dexOptions {
        incremental true
    }
}

Side effects

Enabling this could cause your builds to fail (especially on consecutive runs). If error occurs you can try to build again. Or, if it doesn’t work for you then skip it.

Technique #11

Try not to use Build -> Rebuild Project every time to check for compile errors or after a small change. Rather use, Build -> Make Project, it is much faster than previous option.

References:

Advertisements

One thought on “Android: Speed up Gradle build process in Android Studio

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s