我有一个多项目(大约10个模块),每次建造大约需要20-30秒。当我在Android Studio中按下Run键时,我每次都要等待重新构建应用程序,这非常缓慢。

是否有可能在Android Studio中自动化构建过程?或者你有什么建议可以加快这个过程吗?

在Eclipse中,由于自动构建,在模拟器上运行相同的项目大约需要3-5秒。

这是我的身材。Gradle文件(app模块):

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':libraries:SharedLibs')
    compile project(':libraries:actionbarsherlock')
    compile project(':libraries:FacebookSDK')
    compile project(':libraries:GooglePlayServices')
    compile project(':libraries:HorizontalGridView')
    compile project(':libraries:ImageViewTouch')
    compile project(':libraries:SlidingMenu')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 16
    }
}

当前回答

Android Studio 2.3后更新

所有的答案都很棒,我鼓励使用这些方法来提高构建速度。

在2016年9月android 2.2发布后,android发布了实验性的构建缓存功能,以加速gradle构建性能,现在正式从android Studio 2.3 Canary发布。(正式发布说明)

它引入了一个新的构建缓存功能,默认情况下是启用的,可以通过存储和重用在相同或不同Android项目的以前构建中创建的文件/目录来加快构建时间(包括完整构建,增量构建和即时运行)。

使用方法:

在gradle中添加以下线条。属性文件

android.enableBuildCache = true
# Set to true or false to enable or disable the build cache. If this parameter is not set, the build cache is enable by default.

清理缓存:

有一个新的Gradle任务叫做cleanBuildCache,你可以更容易地清理构建缓存。您可以在终端中输入以下命令来使用它: 。/ gradlew cleanBuildCache 或者你可以通过删除所有存储在该位置的文件来清除Android studio 2.2的缓存 C:\Users\ <用户名> \ .android \构建缓存

其他回答

如果使用谷歌播放服务,只依赖于你需要的库而不是整个blob可以使事情更快。

如果你只需要地图,使用:

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

而不是:

compile 'com.google.android.gms:play-services:6.5.+'

后者将20k个方法(参见博客)引入类路径,这可能使总方法计数超过64k。

这将强制使用proguard或multidex,即使是调试版本。对于我的一个项目,我有以下的构建时间

Multidex构建(带支持库)~40秒 保护建造~20秒 当方法限制< 64k ~5sec时构建

如果在sdk 21+上开发,就有可能优化multidex构建,如android文档中所述

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
        }
    }
    ...
}

Android Studio 2.3后更新

所有的答案都很棒,我鼓励使用这些方法来提高构建速度。

在2016年9月android 2.2发布后,android发布了实验性的构建缓存功能,以加速gradle构建性能,现在正式从android Studio 2.3 Canary发布。(正式发布说明)

它引入了一个新的构建缓存功能,默认情况下是启用的,可以通过存储和重用在相同或不同Android项目的以前构建中创建的文件/目录来加快构建时间(包括完整构建,增量构建和即时运行)。

使用方法:

在gradle中添加以下线条。属性文件

android.enableBuildCache = true
# Set to true or false to enable or disable the build cache. If this parameter is not set, the build cache is enable by default.

清理缓存:

有一个新的Gradle任务叫做cleanBuildCache,你可以更容易地清理构建缓存。您可以在终端中输入以下命令来使用它: 。/ gradlew cleanBuildCache 或者你可以通过删除所有存储在该位置的文件来清除Android studio 2.2的缓存 C:\Users\ <用户名> \ .android \构建缓存

只需创建一个名为gradle的文件。属性:

/home/<username>/.gradle/ (Linux)
/Users/<username>/.gradle/ (Mac)
C:\Users\<username>\.gradle (Windows)

将这一行添加到文件中:

org.gradle.daemon=true

对我来说,现在的速度等于Eclipse。

来源:https://www.timroes.de/2013/09/12/speed-up-gradle/

你可以忽略gradle update-to-date检查。

对于运行Android Studio 1.5的Windows:转到文件->设置->构建,执行,部署->构建工具-> Gradle ->检查离线工作(如图所示)

从~30+秒降低到~3秒

如果你从命令行使用gradle,你可以让这个过程更快。IDE开发人员有很多优化工作要做。但这只是一个早期版本。

欲了解更多信息,请阅读与一些开发人员在g+上的讨论。