我有一个多项目(大约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
    }
}

当前回答

解决我的问题

文件->设置->构建,执行,部署->构建工具-> Gradle ->离线工作

Gradle的构建从8分钟缩短到3秒。

其他回答

我受够了本地机器上缓慢的android构建。我解决这个问题的方法是在AWS上启动一台高端机器,将我本地的代码重新同步到机器上,然后在那里编译。

我看到了性能的立即提升,并且我的本地系统不再占用CPU。看看我创建的这个工具,帮助开发人员加快他们的终端https://stormyapp.com

我最近买了一个新的固态硬盘,从Windows换成了Linux。我的构建时间现在快了一个数量级,不再烦人了。

虽然它没有直接回答为什么它比eclipse慢的问题,但它表明该进程受磁盘限制,升级到SSD可能是一个(有点昂贵的)解决方案。我猜会有人在谷歌上搜索这个问题,最后来到这里,他们可能会欣赏我的经验。

如果使用谷歌播放服务,只依赖于你需要的库而不是整个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
        }
    }
    ...
}

硬件

我很抱歉,但是升级开发站到SSD和大量内存可能比下面的点加起来影响更大。

工具版本

提高构建性能是开发团队的首要任务,所以请确保您使用的是最新的Gradle和Android Gradle Plugin。

配置文件

创建一个名为gradle的文件。应用于任何目录的属性:

-用户home / < >。gradle - (Linux) - \用户/用户< >。gradle - (Mac) C: \ \用户用户< > \。gradle (Windows)

附加:

# IDE (e.g. Android Studio) users:
# Settings specified in this file will override any Gradle settings
# configured through the IDE.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# The Gradle daemon aims to improve the startup and execution time of Gradle.
# When set to true the Gradle daemon is to run the build.
# TODO: disable daemon on CI, since builds should be clean and reliable on servers
org.gradle.daemon=true

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# https://medium.com/google-developers/faster-android-studio-builds-with-dex-in-process-5988ed8aa37e#.krd1mm27v
org.gradle.jvmargs=-Xmx5120m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# 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

# Enables new incubating mode that makes Gradle selective when configuring projects. 
# Only relevant projects are configured which results in faster builds for large multi-projects.
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:configuration_on_demand
org.gradle.configureondemand=true

# Set to true or false to enable or disable the build cache. 
# If this parameter is not set, the build cache is disabled by default.
# http://tools.android.com/tech-docs/build-cache
android.enableBuildCache=true

如果你把Gradle属性放在projectRoot\ Gradle,它们就可以在本地工作。属性和全局,如果你把它们放在user_home\.gradle\gradle.properties。属性适用于从控制台或直接从想法运行gradle任务:

IDE设置

可以从IDE设置GUI中调整Gradle-IntelliJ集成。启用“离线工作”(从yava下面检查答案)将禁用每个“同步gradle文件”上的真实网络请求。

本机multi-dex

apk构建中最慢的步骤之一是将java字节码转换为单个dex文件。启用本地multidex(仅用于调试版本的minSdk 21)将有助于减少工具的工作量(请看下面Aksel Willgert的答案)。

依赖关系

优先选择@aar依赖而不是库子项目。

在mavenCentral, jCenter或使用jitpack搜索aar包。IO从github构建任何库。如果你没有编辑依赖库的源代码,你不应该每次都用你的项目源代码来构建它。

杀毒

考虑从防病毒扫描中排除项目和缓存文件。这显然是一个与安全的权衡(不要在家里尝试!)。但是如果你在分支之间切换很多,那么反病毒会在允许gradle进程使用它之前重新扫描文件,这会减慢构建时间(特别是AndroidStudio同步项目与gradle文件和索引任务)。测量构建时间和进程CPU是否启用防病毒,以查看两者是否相关。

分析构建

Gradle内置了对项目分析的支持。不同的项目使用不同的插件和自定义脚本组合。使用——profile将有助于发现瓶颈。

根据android文档,将此添加到app模块的gradle文件中。

android {
    ...
    dexOptions {
    preDexLibraries true
    maxProcessCount 8
    }
}