Android gradle项目构建非常缓慢

Mor*_*ard 14 android intellij-idea gradle android-studio

我们已将Android应用项目更改为使用gradle,但已注意到它的构建速度明显变慢.

之前使用ANT:
6 sek/50 sec(干净)

使用gradle后:
30 sek/80 sec(干净)

我用以下方法描述了解决方案:

gradle assembleDebug --profile
Run Code Online (Sandbox Code Playgroud)

结果报告中的主要任务是:任务:(在构建中没有干净)

:packageDebug   10.690s     
:processDebugResources  8.795s  
:compileDebugJava   7.644s  
Run Code Online (Sandbox Code Playgroud)

我对获取有关这些任务的更多详细信息没有任何想法.

这是正常的吗?怎么可以改善?
我知道新的构建系统仍处于测试阶段,但似乎其他人的构建速度更快.


我已经环顾四周而没有找到解决方案我尝试过几个方面,包括确保使用包含以下内容的gradle.properties文件启用gradle deamon:

org.gradle.daemon=true
org.gradle.jvmargs=-Xms128m -Xmx256m
org.gradle.parallel=true
Run Code Online (Sandbox Code Playgroud)

的build.gradle:

buildscript {
    repositories {
        mavenCentral()
        maven { url 'http://download.crashlytics.com/maven' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.+'
        classpath 'com.google.guava:guava:14.0.1'
        classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
        classpath 'me.tatarka:gradle-retrolambda:1.1.1'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2'
    }
}

repositories {
    mavenCentral()
    maven { url 'http://download.crashlytics.com/maven' }
}

apply plugin: 'android'
apply plugin: 'crashlytics'
apply plugin: 'retrolambda'
apply plugin: 'android-apt'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile 'com.google.guava:guava:14.0.1'
    compile 'com.crashlytics.android:crashlytics:1.+'
    apt "org.androidannotations:androidannotations:3.0.1"
    compile "org.androidannotations:androidannotations-api:3.0.1"
}

apt {
    arguments {
        resourcePackageName "com.example"
        androidManifestFile variant.processResources.manifestFile
    }
}

android {
    packagingOptions { //Fix: http://stackoverflow.com/a/20675331/860488
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    compileSdkVersion 10
    buildToolsVersion "18.0.1"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 10
        buildConfigField "boolean", "useProductionServices", "true"
    }

    buildTypes {
        testflight.initWith(buildTypes.debug)
        debug {
            packageNameSuffix ".debug"
            buildConfigField "boolean", "useProductionServices", "false"
        }

        testflight {
            packageNameSuffix ".testflight"
            buildConfigField "boolean", "useProductionServices", "true"
        }

        release {
            buildConfigField "boolean", "useProductionServices", "true"
        }
    }
}

retrolambda {
    compile "net.orfjackal.retrolambda:retrolambda:1.1.2"
    jdk System.getenv("JAVA8_HOME")
}
Run Code Online (Sandbox Code Playgroud)

Mor*_*ard 3

添加gradle.properties包含此内容的文件会有所帮助:

org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.jvmargs=-Xmx256m
Run Code Online (Sandbox Code Playgroud)

来源:https://medium.com/@erikhellman/boosting-the-performance-for-gradle-in-your-android-projects-6d5f9e4580b6

编辑:
我刚刚读过这篇博文,其中有 6 个提高 gradle 构建速度的技巧:
6 Tips to speed up your Gradle build