本文翻译自:Building and running app via Gradle and Android Studio is slower than via Eclipse
I have a multi-project (~10 modules) of which building takes about 20-30 seconds each time. 我有一个多项目(~10个模块),每次建筑大约需要20-30秒。 When I press Run in Android Studio, I have to wait every time to rebuild the app, which is extremely slow. 当我在Android Studio中按Run时,我必须每次都等待重建应用程序,这非常慢。
Is it possible to automate building process in Android Studio? 是否可以在Android Studio中自动化构建过程? Or do you have any advice on how to make this process faster? 或者您对如何更快地完成此过程有任何建议?
In Eclipse, thanks to automatic building, running the same project on an emulator takes about 3-5 seconds. 在Eclipse中,由于自动构建,在模拟器上运行相同的项目大约需要3-5秒。
This is my build.gradle file (app module): 这是我的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
}
}
#1楼
参考:https://stackoom.com/question/18Nzh/通过Gradle和Android-Studio构建和运行应用程序比通过Eclipse慢
#2楼
You could make the process faster, if you use gradle from command line. 如果从命令行使用gradle,则可以使进程更快。 There is a lot of optimization to do for the IDE developers. IDE开发人员需要做很多优化。 But it is just an early version. 但它只是一个早期版本。
For more information read this discussion on g+ with some of the devs. 欲了解更多信息,请阅读本讨论与一些开发者对G +。
#3楼
Searched everywhere for this and finally found a solution that works for us. 到处搜索,最终找到了适合我们的解决方案。 Enabling parallel builds (On OSX: preferences -> compiler -> gradle -> "Compile independent modules in parallel"
) and enabling 'make project automatically' brought it down from ~1 min to ~20 sec. 启用并行构建(在OSX上: preferences -> compiler -> gradle -> "Compile independent modules in parallel"
)并启用“自动创建项目”将其从约1分钟缩短到约20秒。 Thanks to /u/Covalence. 感谢/ u / Covalence。
http://www.reddit.com/r/androiddev/comments/1k3nb3/gradle_and_android_studio_way_slower_to_build/ http://www.reddit.com/r/androiddev/comments/1k3nb3/gradle_and_android_studio_way_slower_to_build/
#4楼
Hardware 硬件
I'm sorry, but upgrading development station to SSD and tons of ram has probably a bigger influence than points below combined. 对不起,但是将开发站升级到SSD和大量的ram可能比以下几点的影响更大。
Tools versions 工具版本
Increasing build performance has major priority for the development teams, so make sure you are using latest Gradle and Android Gradle Plugin . 提高构建性能是开发团队的首要任务,因此请确保您使用的是最新的Gradle和Android Gradle插件 。
Configuration File 配置文件
Create a file named gradle.properties
in whatever directory applies: 在任何适用的目录中创建名为gradle.properties
的文件:
-
/home/<username>/.gradle/
(Linux)/home/<username>/.gradle/
) -
/Users/<username>/.gradle/
(Mac)/Users/<username>/.gradle/
) -
C:\\Users\\<username>\\.gradle
(Windows)C:\\Users\\<username>\\.gradle
(Windows)
Append: 附加:
# 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 properties works local if you place them at projectRoot\\gradle.properties
and globally if you place them at user_home\\.gradle\\gradle.properties
. 如果将projectRoot\\gradle.properties
属性放在projectRoot\\gradle.properties
,则Gradle属性在本地工作;如果将它们放在user_home\\.gradle\\gradle.properties
,则全局user_home\\.gradle\\gradle.properties
。 Properties applied if you run gradle tasks from console or directly from idea: 如果从控制台或直接从构思运行gradle任务,则应用属性:
IDE Settings IDE设置
It is possible to tweak Gradle-IntelliJ integration from the IDE settings GUI. 可以从IDE设置GUI调整Gradle-IntelliJ集成。 Enabling "offline work" (check answer from yava below) will disable real network requests on every "sync gradle file". 启用“离线工作”(检查从答案Yava公司下文)将禁止在每一个“的gradle同步文件”真正的网络请求。
Native multi-dex 原生多语言
One of the slowest steps of the apk build is converting java bytecode into single dex file. apk构建的最慢步骤之一是将java字节码转换为单个dex文件。 Enabling native multidex (minSdk 21 for debug builds only) will help the tooling to reduce an amount of work (check answer from Aksel Willgert below). 启用本机multidex(仅适用于调试版本的minSdk 21)将有助于工具减少工作量(请参阅下面的Aksel Willgert的答案)。
Dependencies 依赖
Prefer @aar
dependencies over library sub-projects. 首选@aar
依赖于库子项目。
Search aar package on mavenCentral , jCenter or use jitpack.io to build any library from github. 在mavenCentral , jCenter上搜索aar包或使用jitpack.io从github构建任何库。 If you are not editing sources of the dependency library you should not build it every time with your project sources. 如果您不编辑依赖项库的源代码,则不应每次使用项目源构建它。
Antivirus 杀毒软件
Consider to exclude project and cache files from antivirus scanning. 考虑从防病毒扫描中排除项目和缓存文件。 This is obviously a trade off with security (don't try this at home!). 这显然是与安全的权衡(不要在家里试试!)。 But if you switch between branches a lot, then antivirus will rescan files before allowing gradle process to use it, which slows build time (in particular AndroidStudio sync project with gradle files and indexing tasks). 但是如果你在分支机构之间切换很多,那么防病毒软件会在允许gradle进程使用它之前重新扫描文件,这会减慢构建时间(特别是带有gradle文件和索引任务的AndroidStudio同步项目)。 Measure build time and process CPU with and without antivirus enabled to see if it is related. 测量构建时间并在启用和未启用防病毒的情况下处理CPU,以查看它是否相关。
Profiling a build 分析构建
Gradle has built-in support for profiling projects . Gradle内置了对概要分析项目的支持。 Different projects are using a different combination of plugins and custom scripts. 不同的项目使用插件和自定义脚本的不同组合。 Using --profile
will help to find bottlenecks. 使用--profile
将有助于找到瓶颈。
#5楼
I'm far from being an expert on Gradle but my environment had the following line in .gradle/init.gradle 我远不是Gradle的专家,但我的环境在.gradle / init.gradle中有以下几行
gradle.projectsLoaded {
rootProject.allprojects {
repositories {
mavenRepo name: 'libs-repo', url: 'http://guest-vm/artifactory/repo'
}
}
}
Yet I have no idea why that line was there, but I try changing to 然而我不知道为什么那条线在那里,但我尝试改变
gradle.projectsLoaded {
rootProject.allprojects {
repositories {
mavenCentral()
}
}
}
and now I finally can work without swearing to Android Studio & Gradle buildind scheme. 现在我终于可以在不咒骂Android Studio和Gradle buildind方案的情况下工作了。
#6楼
Just create a file named gradle.properties in the following directory: 只需在以下目录中创建名为gradle.properties的文件:
/home/<username>/.gradle/ (Linux)
/Users/<username>/.gradle/ (Mac)
C:\Users\<username>\.gradle (Windows)
Add this line to the file: 将此行添加到文件中:
org.gradle.daemon=true
For me the speed is now equal to Eclipse. 对我来说,速度现在等于Eclipse。
Source: https://www.timroes.de/2013/09/12/speed-up-gradle/ 资料来源: https : //www.timroes.de/2013/09/12/speed-up-gradle/