fro*_*mcs 456 android gradle android-studio build.gradle
我有一个多项目(~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
}
}
Run Code Online (Sandbox Code Playgroud)
Ser*_*kyi 438
对不起,但是将开发站升级到SSD和大量的ram可能比以下几点的影响更大.
提高构建性能是开发团队的首要任务,因此请确保您使用的是最新的Gradle和Android Gradle插件.
创建一个gradle.properties在任何目录中命名的文件:
/home/<username>/.gradle/ (Linux)的/Users/<username>/.gradle/ (苹果电脑)C:\Users\<username>\.gradle (视窗)附加:
# 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
Run Code Online (Sandbox Code Playgroud)
如果放置它们,Gradle属性在本地工作,如果放置它们,则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进程使用它之前重新扫描文件,这会减慢构建时间(特别是带有gradle文件和索引任务的AndroidStudio同步项目).测量构建时间并在启用和未启用防病毒的情况下处理CPU,以查看它是否相关.
Gradle内置了对概要分析项目的支持.不同的项目使用插件和自定义脚本的不同组合.使用--profile将有助于找到瓶颈.
yav*_*ava 148
您可以忽略gradle update-to-date检查.

对于运行Android Studio 1.5的Windows:转到 File -> Settings -> Build, Execution, Deployment -> Build tools -> Gradle -> Check Offline work (as shown in image)
从~30 +秒下降到~3秒
odi*_*ity 34
到处搜索,最终找到了适合我们的解决方案.启用并行构建(在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/
And*_*vik 25
我最近买了一台新的SSD,从Windows转到Linux.我的构建时间现在快了一个数量级,不再烦人.
虽然它没有直接回答你的问题,为什么它比eclipse慢,但它表明这个过程是磁盘限制的,升级到SSD可能是一个(有点贵)的解决方案.我猜测会有人在谷歌上搜索问题并在此结束,他们可能会欣赏我的经历.
Aks*_*ert 19
如果使用谷歌播放服务,根据您需要的库而不是整个blob可以使事情更快.
如果您只需要地图,请使用:
compile 'com.google.android.gms:play-services-maps:6.5.+'
Run Code Online (Sandbox Code Playgroud)
代替:
compile 'com.google.android.gms:play-services:6.5.+'
Run Code Online (Sandbox Code Playgroud)
后者将20k方法(参见博客)引入类路径,这可能会使总方法计数超过64k.
即使对于调试版本,这也会强制使用proguard或multidex.对于我的一个项目,我有以下构建时间
如果在sdk 21+上开发,可以优化android文档中所述的 multidex构建
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
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
Ahm*_*deh 19
在Android Studio 3.2.1中加速Gradle构建
您是否觉得等待在Android Studio中完成构建几分钟?我也是.而且这很烦人.幸运的是,有几种方法可以用来改善这一点.Android使用Gradle进行构建.最新版本4.6与以前的版本相比具有巨大的性能提升(有关详细信息,请参阅发行说明).
步骤1:更新Gradle版本更简单的方法是转到:打开模块设置(您的项目)>项目结构
UPDATE
更改为Gradle版本:4.6 并更改为Android插件版本:3.2.1
从https://services.gradle.org/distributions/gradle-4.6-all.zip下载Gradle Release分发版 并将其复制到Gradle文件夹:
最后一步是在设置> Gradle中添加您的描述
不要忘记单击"应用"以保存更改.
步骤2:为项目启用脱机模式,Gradle守护程序和并行构建脱机模式告知Gradle忽略更新到日期检查.Gradle每次都要求依赖,并且使用此选项使其只使用计算机上已有的依赖项.从android studio设置转到Gradle,然后单击"脱机工作"框.
下一步是为项目启用Gradle守护程序和并行构建.并行构建将使您的项目具有多个模块(Gradle中的多项目构建)并行构建,这将使大型或模块化项目的构建更快.
这些选项,这些设置可以通过modifiing一个文件中启用的摇篮scripts目录名为gradle.properties(即〜/ .gradle/gradle.properties).有些(如Complie并联模块)可在Android Studio和还没有启用默认情况下,但将它们放在gradle.properties文件中将在从终端构建时启用它们,并确保您的同事将使用相同的设置.但是如果你在一个团队工作,有时你不能提交这些东西.
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit org.gradle.parallel=true
# When set to true the Gradle daemon is used to run the build. For local developer builds this is our favorite property.
# The developer environment is optimized for speed and feedback so we nearly always run Gradle jobs with the daemon.
org.gradle.daemon=true
Run Code Online (Sandbox Code Playgroud)
使用守护进程将使您的构建启动更快,因为它不必每次都启动整个Gradle应用程序.默认情况下,Gradle守护程序未启用,但建议始终为开发人员的计算机启用它(但为连续集成服务器禁用它).有关此模式的常见问题解答,请访问https://docs.gradle.org/current/userguide/gradle_daemon.html.对于某些项目,并行构建设置可能不安全.该要求是,所有的模块都必须脱钩或者您的构建可能会失败(见http://gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects了解详细信息).
步骤3:启用增量设计和调整内存设置您可以通过启用增量删除来加速构建.在模块的构建文件中:
将此选项添加到您的android块:
dexOptions {
incremental true
}
Run Code Online (Sandbox Code Playgroud)
在该dexOptions块中,您还可以指定dex进程的堆大小,例如:
dexOptions {
incremental true
javaMaxHeapSize "12g"
}
Run Code Online (Sandbox Code Playgroud)
其中"12g"是12GB的内存.有关此内容的更多信息,请访问google.github.io/android-gradle-dsl/current/您还可以在设置文件中配置Gradle参数,例如,如果您有大型项目,请增加最大堆大小:
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅此处的所有参数列表:https://docs.gradle.org/current/userguide/userguide_single.html#sec:grad_configuration_properties.
步骤4:禁用防病毒考虑从防病毒扫描中排除项目和缓存文件.这显然是与安全的权衡.但是如果你在分支机构之间切换很多,那么防病毒软件会在允许gradle进程使用它之前重新扫描文件,这会减慢构建时间(特别是带有gradle文件和索引任务的Android Studio同步项目).测量构建时间并在启用和未启用防病毒的情况下处理CPU,以查看它是否相关.我希望这有帮助.如果您有任何问题或其他提示可以提高构建性能,请发表评论.
Rea*_*hed 16
接受的答案是旧版本的android studio,其中大部分仍在使用.更新android studio让它变得更快一点.不要指定堆大小,因为它会随着Xms和Xmx的增加而自动增加.这是VMoptions的一些修改
在bin文件夹中有一个studio.vmoptions文件来设置环境配置.在我的情况下,这是studio64.vmoptions如果它们尚未添加,请添加以下行并保存文件.在我的情况下,我有8GB的RAM.
-Xms4096m
-Xmx4096m
-XX:MaxPermSize=2048m
-XX:+CMSClassUnloadingEnabled
-XX:+CMSPermGenSweepingEnabled
-XX:+HeapDumpOnOutOfMemoryError
-Dfile.encoding=utf-8`
Run Code Online (Sandbox Code Playgroud)启动android studio.转到文件 - >设置 - >构建,执行,部署 - >编译器
在使用mac的情况下,起初我找不到vmoptions.无论如何,这是一篇很好的文章,关于我们如何改变MAC OSX中的vmoptions.在这里引用这篇文章.
打开终端并输入此命令以打开MAC OSX中的vmoptions:
open -e /Applications/Android\ Studio.app/Contents/bin/studio.vmoptions
Run Code Online (Sandbox Code Playgroud)
Yst*_*ter 15
只需在以下目录中创建名为gradle.properties的文件:
/home/<username>/.gradle/ (Linux)
/Users/<username>/.gradle/ (Mac)
C:\Users\<username>\.gradle (Windows)
Run Code Online (Sandbox Code Playgroud)
将此行添加到文件中:
org.gradle.daemon=true
Run Code Online (Sandbox Code Playgroud)
对我来说,速度现在等于Eclipse.
资料来源:https://www.timroes.de/2013/09/12/speed-up-gradle/
Gen*_*ani 11
如果有人正在使用通过Subversion同步的项目并且这仍在发生,我认为这会减慢Android Studio中的工作流程.例如,如果它在以下情况下工作非常慢:在类,xml等中滚动,而我的应用程序仍然在我的设备上运行.
所有答案都很棒,我鼓励使用这些方法来提高构建速度.
在2016年9月发布Android 2.2后,Android发布了实验性构建缓存功能,以加快gradle构建性能,现在Android Studio 2.3 Canary正式推出.(官方发行说明)
它引入了一个新的构建缓存功能,默认情况下启用,可以通过存储和重用在相同或不同Android的先前版本中创建的文件/目录来加快构建时间(包括完整构建,增量构建和即时运行)项目.
如何使用:
在gradle.properties文件中添加以下行
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.
Run Code Online (Sandbox Code Playgroud)
清理缓存:
有一个新的Gradle任务要求cleanBuildCache您更轻松地清理构建缓存.您可以在终端中键入以下内容来使用它:
./gradlew cleanBuildCache
或者您可以通过删除位置存储的所有文件来清除Android studio 2.2的缓存
C:\Users\<username>\.android\build-cache
更改此设置后,我的编译持续时间10分钟更改为~10秒.
步骤1:
设置(ctrl + Alt + S) - >
构建,执行,部署 - >
编译器 - >
--offline在命令行选项框中键入" ".
第2步:
选中"并行编译独立模块"复选框.
然后单击应用 - >确定
参考 - https://www.sundoginteractive.com/blog/speed-up-gradle-in-android-studio
坏处:
您将无法下载build.gradle文件中标识的最新版本的依赖项.它运行得更快,因为它使用这些导入库的缓存快照.
重要说明:部署应用程序时,请删除此设置并使用最新版本的依赖项进行构建.
小智 6
解决了我的问题
File -> Settings -> Build, Execution, Deployment -> Build Tools ->Gradle -> Offline work
Gradle构建从8分钟到3秒.
| 归档时间: |
|
| 查看次数: |
219155 次 |
| 最近记录: |