我的Android Studio项目过去构建得更快,但现在需要很长时间来构建。知道是什么导致了延误吗?
我试过https://stackoverflow.com/a/27171878/391401但没有效果。我没有运行任何可能中断构建的反病毒程序。我的应用程序也没有那么大(大约5MB),它曾经在几秒钟内构建,但不确定发生了什么变化。
10:03:51 Gradle build finished in 4 min 0 sec
10:04:03 Session 'app': running
10:10:11 Gradle build finished in 3 min 29 sec
10:10:12 Session 'app': running
10:20:24 Gradle build finished in 3 min 42 sec
10:28:18 Gradle build finished in 3 min 40 sec
10:28:19 Session 'app': running
10:31:14 Gradle build finished in 2 min 56 sec
10:31:14 Session 'app': running
10:38:37 Gradle build finished in 3 min 30 sec
10:42:17 Gradle build finished in 3 min 40 sec
10:45:18 Gradle build finished in 3 min 1 sec
10:48:49 Gradle build finished in 3 min 30 sec
10:53:05 Gradle build finished in 3 min 22 sec
10:57:10 Gradle build finished in 3 min 19 sec
10:57:11 Session 'app': running
找到了一个简单和最好的方法来构建gradle
去你的应用>>打开gradle。属性,并更改一些行从它[remove #]
否则你可以复制粘贴我下面的片段以及
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx2048m -XX:MaxPermSize=512m
org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=1024m -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
以前对我来说很有效,大约需要2-3分钟,现在只需要5-6秒
在上面的场景中,我的mac机器有8GB内存,所以我为gradle分配了4gb内存
我很长一段时间都面临着同样的问题,但我通过调整gradle中的设置解决了它。
步骤1:在Module app中添加BuildScript中的依赖项
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-alpha9'
}
}
步骤2:添加dexOption并给出以下heapSize
dexOptions {
incremental = true;
preDexLibraries = false
javaMaxHeapSize "4g"
}
第三步:添加产品口味
productFlavors {
dev {
minSdkVersion 23
applicationId = "com.Reading.home"
}
prod {
minSdkVersion 15
applicationId = "com.Reading.home" // you don't need it, but can be useful
}
}
这将减少您的构建时间。
如果你使用谷歌播放服务,确保你没有在你的Gradle构建脚本中使用这个:
compile 'com.google.android.gms:play-services:8.3.0'
只使用你的应用程序真正使用的谷歌api。如果你使用的是谷歌地图,你会使用这个:
com.google.android.gms:play-services-maps:8.3.0
当我这样做时,我的编译时间从2分钟多缩短到25秒左右。有关您可以选择性编译的谷歌api的列表,请参阅:
https://developers.google.com/android/guides/setup