我有一个多项目(~10个模块),其中构建大约需要20-30秒的时间。当我在Android Studio中按下Run时,我必须等待每次重建应用程序,这是非常慢的。
有可能在Android Studio中自动化构建过程吗?或者,你有什么建议可以让这个过程更快吗?
在Eclipse中,多亏了自动构建,在模拟器上运行相同的项目大约需要3-5秒。
这是我的build.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
}
}发布于 2013-10-22 01:02:54
硬件
我很抱歉,但是将开发站升级到SSD和大量ram的影响可能比下面几点加起来还要大。
工具版本
提高构建性能是开发团队的首要任务,因此请确保您使用的是最新的Gradle和Android Gradle Plugin。
配置文件
在任何适用的目录中创建一个名为gradle.properties的文件:
/home/<username>/.gradle/ (Linux)/Users/<username>/.gradle/ (Mac)C:\Users\<username>\.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.properties中,则Gradle属性在本地有效;如果您将它们放在user_home\.gradle\gradle.properties中,则Gradle属性在全局中有效。从控制台或直接从idea运行gradle任务时应用的属性:
IDE设置
可以从IDE设置GUI调整Gradle-IntelliJ集成。启用“离线工作”(查看下面yava的答案)将禁用每个“同步gradle文件”上的真实网络请求。

原生多址
apk构建过程中最慢的步骤之一是将java字节码转换为单个dex文件。启用本机multidex (仅限调试版本的minSdk 21 )将有助于工具减少工作量(请查看下面Aksel Willgert的答案)。
依赖关系
优先考虑@aar依赖关系而不是库子项目。
在mavenCentral、jCenter上搜索aar包,或者使用jitpack.io从github构建任意库。如果您不是在编辑依赖库的源代码,则不应该每次都使用项目源代码来构建它。
防病毒软件
考虑从防病毒扫描中排除项目和缓存文件。这显然是对安全性的权衡(不要在家里尝试!)。但是如果你经常在分支之间切换,那么antivirus将在允许gradle进程使用它之前重新扫描文件,这会减慢构建时间(特别是与gradle文件和索引任务的AndroidStudio同步项目)。测量构建时间并处理启用和不启用防病毒的CPU,以查看它们是否相关。
分析构建
Gradle内置了对profiling projects的支持。不同的项目使用插件和自定义脚本的不同组合。使用--profile将有助于发现瓶颈。
发布于 2014-05-14 15:35:30
您可以忽略gradle最新检查。

对于运行Android Studio1.5的Windows :转到File -> Settings -> Build, Execution, Deployment -> Build tools -> Gradle -> Check Offline work (as shown in image)
从~30+秒降至~3秒
发布于 2013-08-11 03:03:51
我到处寻找这个问题,最后找到了一个适合我们的解决方案。启用并行构建(在OSX:preferences -> compiler -> gradle -> "Compile independent modules in parallel"上)和启用“自动生成项目”将时间从~1分钟缩短到~20秒。感谢/u/Covalence。
http://www.reddit.com/r/androiddev/comments/1k3nb3/gradle_and_android_studio_way_slower_to_build/
https://stackoverflow.com/questions/16775197
复制相似问题