我有一个多项目(约10个模块),每次构建需要20-30秒。当我在Android Studio中按下 "运行 "键时,我不得不每次都等待重建应用程序,这非常慢。
有没有可能在Android Studio中实现自动构建过程?或者你有什么建议可以让这个过程更快?
在Eclipse中,由于有自动构建功能,在模拟器上运行同一个项目大约需要3-5秒。
这是我的build.gradle文件(应用程序模块)。
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和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
,它们就能在本地工作;如果你把它们放在user_home\.gradle\gradle.properties
,它们就能在全球工作。如果你从控制台或直接从想法中运行gradle任务,属性就会应用。
可以通过IDE设置GUI来调整Gradle-IntelliJ的集成。启用"离线工作"(查看下面yava的答案)将禁用每个"同步gradle文件"的真实网络请求。
apk构建中最慢的步骤之一是将java字节码转换为单个dex文件。启用本地multidex(minSdk 21仅用于调试构建)将帮助工具减少大量的工作(查看下面Aksel Willgert的答案)。
比起库的子项目,更喜欢`@aar'的依赖关系。
在mavenCentral、jCenter上搜索aar包,或使用jitpack.io构建来自github的任何库。如果你没有编辑依赖库的源,就不应该每次都用项目源构建它。
考虑将项目和缓存文件排除在防病毒扫描之外。这显然是对安全的一种交换(不要在家里尝试这个!)。但是如果你经常在不同的分支之间切换,那么防病毒软件会在允许gradle进程使用文件之前重新扫描文件,这就会减慢构建时间(特别是AndroidStudio同步项目与gradle文件和索引任务)。在启用和未启用防病毒软件的情况下,测量构建时间和进程CPU,看看是否与此有关。
Gradle内置了对剖析项目的支持。不同的项目使用不同的插件和自定义脚本的组合。使用--profile
将有助于找到瓶颈。
为此到处寻找,终于找到了一个适合我们的解决方案。启用并行构建(在OSX上:preferences -> compiler -> gradle -> "Compile independent modules in parallel"
)并启用'make project automatically',使其从~1分钟下降到~20秒。感谢/u/Covalence。
http://www.reddit.com/r/androiddev/comments/1k3nb3/gradle_and_android_studio_way_slower_to_build/