Gradle compilation acceleration

Keywords: Android Gradle Maven less

I. Analyzing the Reasons for the Slow Construction of Gradle

1. Configure Gradle to build reports

File --> Settings --> Build,Execution,Deployment --> Compiler
Modify Command-line Options: Content, append -- profile

2. View the generated gradle build report

Sync Project , build , run, Make project ...
The reports folder is then generated under Project build. Open profile.html with browser to view

2. Solutions to Slow Gradle Construction

Record the build time that has not been optimized before gradle starts optimizing. * Build > Clean Project

1. Open the gradle separate daemon

1. Create it under the following directory
The gradle.properties file:
C:\Users\<username>\.gradle(windows)
2. Add:

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

This configuration is for studio users of this computer, so it works for all projects

2. Setting Gradle of fline

File > Settings > Office line work check as follows:


3. Develop and use SDK=21
android {
    ...

    productFlavors {
        dev {
            minSdkVersion 21
        }

        release {
            minSdkVersion 14
        }
    }
}

When the API is no less than 21, using ART, only doing class to dex in Build, not merging dex, will save a lot of time.

4. Use aar when introducing dependencies

When connecting remote warehouse network is not good, you can introduce your own private maven warehouse, or some domestic maven warehouse.
When using the third-party dependency libraries on the Internet, try to use aar, and the library module can also be packaged into aar. Speed up compilation.

build.gradle file (Top-level of the main project)

apply from:"config.gradle"//You can introduce a configuration file to all module s

buildscript {
     repositories {
     jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:1.3.0'
    // NOTE: Do not place your application dependencies here; they belong
     // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()//Introducing remote warehouse
        maven { url MAVEN_URL }//Introduce your own private maven warehouse
    }
}

gradle.properties (global configuration file)

# This can really make a significant difference if you are building a very complex project with many sub-module dependencies:
#sub-module Parallel Construction
org.gradle.parallel=true
#Background process construction
org.gradle.daemon=true
#Private maven warehouse address
MAVEN_URL= http://xxx.xx.1.147:8081/nexus/content/repositories/thirdparty/

build.gradle(Module)

apply plugin: 'com.android.application'//Plug-in decisions are apk aar jar, etc.

android {
compileSdkVersion 23
buildToolsVersion "24.0.0"

// The annotation here remains open by default, and when it is closed, it allows the less stringent images to be compiled, but causes the apk package to grow larger.
//aaptOptions.cruncherEnabled = false
//aaptOptions.useNewCruncher = false

 packagingOptions {
     exclude 'META-INF/NOTICE.txt'// Here is the full path of the specific conflict file
     exclude 'META-INF/LICENSE.txt'
}
//Default configuration
defaultConfig {
    applicationId "com.locove.meet"
    minSdkVersion 16
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    multiDexEnabled=true//65536 problem
}
sourceSets {
    main {
        jniLibs.srcDirs = ['libs']//Reconfiguration path
    }
}
buildTypes {
    release {
    // zipAlign optimization
    zipAlignEnabled true
    // Remove useless resource files
    shrinkResources false
    // confusion
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    signingConfig signingConfigs.releaseConfig
    }
}
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.code.gson:gson:2.2.+'
    testCompile 'junit:junit:4.12'
}

Posted by PoOP on Sun, 03 Feb 2019 00:24:17 -0800