Speeding up Gradle builds
2016-01-27 15:37
519 查看
转:http://jimulabs.com/2014/10/speeding-gradle-builds/
Gradle is a nice build system and there are many benefits to using it for Android builds. However, a common complaint about Gradle is that build times are much longer than they were with the old ant-based built system. Specifically, building and installing
apps from Android Studio or IntelliJ involves a lot more waiting than developers are used to compared to Eclipse and ADT. Whether you’re using Android Studio or Gradle on the command line, there are a few things you can do to speed your builds up.
Note that these suggestions apply to Gradle in general, not just to Android builds. At the time of writing the latest version of Android Studio is 0.8.11 and the latest version of Gradle is 2.1. Both of these tools are evolving and improving rapidly; we’ll
try to keep this post up to date as things change.
Update April 2, 2015: The content below still applies to Studio 1.1, 1.2 preview 4, and Gradle 2.2.1.
This one is a must if you’re building frequently. The Gradle daemon is a process that runs in the background on your machine. When you run a Gradle task using the daemon, the already-running process handles it. This eliminates Gradle startup costs that are
otherwise incurred every time you run Gradle. The only downside to using the daemon is that the process takes up some memory: on my machine a couple hundred MB. After 3 hours of idle time the daemon will stop itself; you can stop it earlier by running
daemon chapter in the Gradle user guide for more information.
Nothing to do! Android Studio uses a daemon by default, so you don’t need to configure it.
You can configure your project to always use a daemon by adding the following line to the
in your project directory:
If you want to use the daemon only for certain tasks, use the
when starting the task. This will connect a running daemon, or start a new one if necessary. For example:
If your Gradle build contains multiple decoupled projects, you can take advantage of Gradle’s parallel mode. In parallel mode, Gradle will run multiple executor threads, which can execute tasks from different projects in parallel. By default Gradle will create
one executor thread for each CPU core on your machine, but this is configurable.
For more information about multi-project builds, decoupled projects, and parallel execution, have a look at the Multi-project
Builds section in the Gradle user guide.
You can turn on parallel execution mode in Android Studio’s settings. Under the Compiler (Gradle-based Android Projects) section in Studio 1.1 (or Build,
Execution, Deployment > Compiler > Compiler section in Studio 1.2), check the “Compile independent modules in parallel” box. If you want to change the number of threads Gradle uses, add
the “Command-line options” field on the same screen, where
the number of threads to use.
Like the daemon, you an configure this per-project and per-task. To always build in parallel, add the following line to your
To use parallel mode for only a particular task, use the
optionally with the
to change the number of threads used:
In its default mode, Gradle configures every project before executing tasks, regardless of whether the project is actually needed for the particular build. “Configuration on demand” mode changes this behaviour, only configuring required projects. Like parallel
mode, configuration on demand mode will have the greatest effect on multi-project builds with decoupled projects. Note that configuration on demand mode is currently an incubating feature, so isn’t fully supported. You can learn more about it in the Multi-project
Builds section of the Gradle user guide.
You can turn on configure on demand mode on the same screen that has the parallel build settings. In theCompiler (Gradle-based Android Projects) section in Studio
1.1 (or Build, Execution, Deployment > Compiler > Compiler section in Studio 1.2) of Android Studio’s settings. Toggle the “Configure on demand” checkbox to turn
the mode on and off.
This works the same way as parallel mode. To turn on configuration on demand mode for all tasks, add the following line to your
And to use configure on demand mode for a single task, use the
Gradle is a nice build system and there are many benefits to using it for Android builds. However, a common complaint about Gradle is that build times are much longer than they were with the old ant-based built system. Specifically, building and installing
apps from Android Studio or IntelliJ involves a lot more waiting than developers are used to compared to Eclipse and ADT. Whether you’re using Android Studio or Gradle on the command line, there are a few things you can do to speed your builds up.
Note that these suggestions apply to Gradle in general, not just to Android builds. At the time of writing the latest version of Android Studio is 0.8.11 and the latest version of Gradle is 2.1. Both of these tools are evolving and improving rapidly; we’ll
try to keep this post up to date as things change.
Update April 2, 2015: The content below still applies to Studio 1.1, 1.2 preview 4, and Gradle 2.2.1.
Use the Gradle daemon
This one is a must if you’re building frequently. The Gradle daemon is a process that runs in the background on your machine. When you run a Gradle task using the daemon, the already-running process handles it. This eliminates Gradle startup costs that areotherwise incurred every time you run Gradle. The only downside to using the daemon is that the process takes up some memory: on my machine a couple hundred MB. After 3 hours of idle time the daemon will stop itself; you can stop it earlier by running
gradle --stopon the command line (this is useful when sometimes your gradle builds give an error
Unable to locate a Java Runtime to invoke). See the Gradle
daemon chapter in the Gradle user guide for more information.
In Android Studio
Nothing to do! Android Studio uses a daemon by default, so you don’t need to configure it.
On the command line
You can configure your project to always use a daemon by adding the following line to the gradle.propertiesfile
in your project directory:
org.gradle.daemon=true
If you want to use the daemon only for certain tasks, use the
--daemonflag
when starting the task. This will connect a running daemon, or start a new one if necessary. For example:
gradle build --daemon
Compile projects in parallel
If your Gradle build contains multiple decoupled projects, you can take advantage of Gradle’s parallel mode. In parallel mode, Gradle will run multiple executor threads, which can execute tasks from different projects in parallel. By default Gradle will createone executor thread for each CPU core on your machine, but this is configurable.
For more information about multi-project builds, decoupled projects, and parallel execution, have a look at the Multi-project
Builds section in the Gradle user guide.
In Android Studio
You can turn on parallel execution mode in Android Studio’s settings. Under the Compiler (Gradle-based Android Projects) section in Studio 1.1 (or Build,Execution, Deployment > Compiler > Compiler section in Studio 1.2), check the “Compile independent modules in parallel” box. If you want to change the number of threads Gradle uses, add
--parallel-threads=Nto
the “Command-line options” field on the same screen, where
Nis
the number of threads to use.
On the command line
Like the daemon, you an configure this per-project and per-task. To always build in parallel, add the following line to your gradle.propertiesfile:
org.gradle.parallel=true
To use parallel mode for only a particular task, use the
--parallelflag,
optionally with the
--parallel-threadsflag
to change the number of threads used:
gradle build --parallel --parallel-threads=N
Configure projects on demand
In its default mode, Gradle configures every project before executing tasks, regardless of whether the project is actually needed for the particular build. “Configuration on demand” mode changes this behaviour, only configuring required projects. Like parallelmode, configuration on demand mode will have the greatest effect on multi-project builds with decoupled projects. Note that configuration on demand mode is currently an incubating feature, so isn’t fully supported. You can learn more about it in the Multi-project
Builds section of the Gradle user guide.
In Android Studio
You can turn on configure on demand mode on the same screen that has the parallel build settings. In theCompiler (Gradle-based Android Projects) section in Studio1.1 (or Build, Execution, Deployment > Compiler > Compiler section in Studio 1.2) of Android Studio’s settings. Toggle the “Configure on demand” checkbox to turn
the mode on and off.
On the command line
This works the same way as parallel mode. To turn on configuration on demand mode for all tasks, add the following line to your gradle.propertiesfile:
org.gradle.configureondemand=true
And to use configure on demand mode for a single task, use the
--configure-on-demandflag:
gradle build --configure-on-demand
相关文章推荐
- 交互设计的用户行为模式十二项 【转载】
- UITableViewCell中的bug
- Please specify exact device preset UUID
- build and install everything into local directory
- 给GPUImage录制的视频添加水印
- Customizing Your Build With Gradle
- ViewPager强制刷新UI
- UISearchDisplayController 底部留白
- require文件失败的解决
- 动态获取UITextView 高度
- Java - String, StringBuffer and StringBuilder
- 两个UIViewController之间的翻转可以用动画效果翻转(上下,左右)--跨界面之间的视图切换
- HDU-4908-BestCoder Sequence【思维题】
- RESTEasy数据自动装配之@QueryParam
- ClassNotFound: edu.emory.mathcs.backport.java.util.concurrent.BlockingQueue
- UIScrollView 实践经验
- 解决VirtualBox分辨率太小及VBoxGuestAdditions_4.3.12.iso下载地址
- druid数据源例子
- Value Dispose() cannot be called while doing CreateHandle().
- 实现ueditor + 图片空间 实现图片上传