Some Suggestions on Improving the Compilation Speed of Android studio in Google I/O

Keywords: Android Gradle Google

There is one in Google I/O How to speed up your slow Gradle builds In the speech, some suggestions to speed up Android studio compilation are put forward, which are as follows:

1. Use the latest Android gradle plug-in

Google tools team has been working to speed up the compilation of Android studio, so it's best to use the latest Android Gradle Plugin:

buildscript {
  repositories {
    google()
  }

 dependencies {
    classpath 'com.android.tools.build.gradle:3.0.0-alpha3'
 }
}

2. Avoid multidex

We know that when the method book exceeds 64k, we need to configure multidex, but if our minSdkVersion is set to 20 or lower, the build time will increase greatly, because the build system must make complex decisions about which classes must be included in the main DEX file and which classes can be included in the auxiliary DEX file.

In this case, it can be used productFlavors Create two build variants (a development customization and a release customization with different minSdkVersion values).

android {
    defaultConfig {
        ...
        multiDexEnabled true
    }
    productFlavors {
        dev {
            // Enable pre-dexing to produce an APK that can be tested on
            // Android 5.0+ without the time-consuming DEX build processes.
            minSdkVersion 21
        }
        prod {
            // The actual minSdkVersion for the production version.
            minSdkVersion 14
        }
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                                                 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile 'com.android.support:multidex:1.0.1'
}

3. Reduce packaged resource files

In the development mode, only the required resources can be packaged without adapting all the resource files:

productFlavors {
  dev {
    minSdkVersion 21
    //only package english translations, and xxhdpi resources   
    resConfigs ("en", "xxhdpi")
  }
}

4. Disabling PNG processing

PNG optimization is turned on by default and can be disabled in development mode:

android {
  if (project.hasProperty('devBuild')){
    aaptOptions.cruncherEnabled = false
  }
}

5. Use Instant run

Instant Run has many improvements in android studio 3.0 and can be tried out.

6. Don't change configuration casually

Gradle is very flexible to use, but incorrect use can slow down compilation. For example:

//this is BAD! This practice will cause every compilation manifest file to be modified, resulting in an unnecessary increase in compilation time.
def buildDateTime = new Date().format('yyMMddHHmm').toInteger()
android {
  defaultConfig {
    versionCode buildDateTime
 }
}

The correct approach is:

def buildDateTime = project.hasProperty('devBuild') ? 100 : new Date().format('yyMMddHHmm').toInteger()
android {
  defaultConfig {
    versionCode buildDateTime
 }
}

7. Avoid using dynamic version dependencies

Fixed version dependencies are usually used.

8. Pay attention to memory usage

Note the memory allocated to Gradle:
Current configuration

org.gradle.jvmargs=-Xmx1536m

No need to configure:

dexOptions {
 javaMaxHeapSize = '4g'
}

9. Use Gradle caching

In Gradle 3.5, files generated by previous builds can be cached and reused using cache.

# Set this in gradle.properties
org.gradle.caching=true

Relevant address: https://www.youtube.com/watch?v=7ll-rkLCtyk



Author: wutongke
Link: http://www.jianshu.com/p/6301f454de5c
Source: Brief Book
Copyright belongs to the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

Posted by sajy2k on Sun, 16 Dec 2018 05:48:03 -0800