android多项目构建_如何提高Android项目的构建速度

android多项目构建

by Prateek Phoenix

通过Prateek Phoenix

Recently, I undertook the task of migrating the Android codebase at Kure to AndroidX. It seemed like the perfect opportunity to try and fix the build speeds of the project.

最近,我承担了将Kure的Android代码库迁移到AndroidX的任务。 似乎是尝试并修复项目构建速度的绝佳机会。

Gradle has always had a bad rep for being slow and resource intensive, but I was quite surprised at how minor changes to the project’s build configuration could massively improve the build speeds.

Gradle总是因为速度慢和占用大量资源而表现不好,但是我对项目的构建配置进行微小的更改会如何极大地提高构建速度感到非常惊讶。

To give you a sneak peek of the time I was able to shed from our clean builds, here’s a before and after metric from the build scan.

为了让您能一窥我能够从干净的构建中摆脱出来的时间,这是构建扫描之前和之后的指标。

Going down from 5.5 minutes to 17 seconds?? That’s bonkers.

从5.5分钟缩短至17秒?? 真是傻瓜。

It’s easy to go overboard with optimizations that you can perform to bring down your build time even further. But I am going to intentionally focus on the minor, painless measures I took to come close to this metric for the sake of keeping this post beginner friendly.

您可以轻松进行优化,而执行优化可以进一步缩短构建时间。 但是,我将有目的地集中精力于为使此职位初学者保持友好状态而采取的较小而无痛苦的措施,以接近该指标。

但首先! (But first!)

Before starting off with the optimization, it’s important to benchmark our project to see how long it currently takes to build. Gradle has a handy scan option that you can use to analyze the performance of your task. Fire up the terminal on Android Studio and execute the following command:

在开始优化之前,对我们的项目进行基准测试以了解当前需要花多长时间来构建它很重要。 Gradle有一个方便的扫描选项,可用于分析任务的性能。 在Android Studio上启动终端并执行以下命令:

./gradlew assembleDebug --scan

Once the build completes successfully, it will prompt you to accept the terms of service to upload your build scan results. Type yes to proceed. Once it’s done publishing, you will get a link on the terminal to check your build scan. Open the link.

构建成功完成后,它将提示您接受服务条款以上传构建扫描结果。 输入继续。 发布完成后,您将在终端上获得一个链接来检查构建扫描。 打开链接。

There are quite a few options on the site, but for the sake of brevity, we’re only gonna take a look at what’s most important.
该网站上有很多选项,但是为了简洁起见,我们只看一下最重要的内容。

The summary view shows you a summary of the tasks that were run and how long it took for them to complete. But what we’re interested in here is the Performance section. It gives you a more detailed breakdown of the total build time as shown below.

摘要 视图向您显示了已运行任务的摘要以及完成任务所需的时间。 但是我们在这里感兴趣的是“ 性能”部分。 它为您提供了总构建时间的更详细细分,如下所示。

Under the performance section, there’s a Settings and suggestions tab that gives you suggestions on how you can improve your build speeds. Let’s check that out.

在“性能”部分下,有一个“设置和建议”选项卡,可为您提供有关如何提高构建速度的建议。 让我们检查一下。

We can find some easy fixes for our build speed in this section. So let’s go ahead and apply these suggestions in our project.

我们可以在本节中找到一些针对我们构建速度的简单修复程序。 因此,让我们继续在项目中应用这些建议。

步骤1:更新您的工具 (Step 1: Update your tooling)

The Android team is constantly improving and evolving the Android build system. So most of the time you can receive significant improvements just by adopting the latest version of the tooling.

Android团队正在不断改进和发展Android构建系统。 因此,在大多数情况下,仅通过采用最新版本的工具即可获得重大改进。

At the time of this refactor, our project was on version 3.2.1 of the Gradle plugin for Android Studio (which is a few versions older than the latest release).

在进行此重构时,我们的项目使用的是Android Studio的Gradle插件3.2.1 版本(比最新版本要早一些版本)。

You can visit this link to get the version for the latest release of the Gradle Plugin.

您可以访问此链接以获取最新版本的Gradle插件的版本。

At the time of writing this post, the latest version happens to be version 3.4.0.

在撰写本文时,最新版本恰巧是3.4.0版。

But it comes with a gotcha that we need to keep in mind for later:

但是它带有一个陷阱,稍后我们需要记住:

When using Gradle 5.0 and above we will need to explicitly increase the heap size to ensure our build speed doesn’t worsen. We will come back to this in just a minute.

使用Gradle 5.0及更高版本时,我们将需要显式增加堆大小,以确保构建速度不会恶化。 我们将在短短一分钟后再讨论这个问题。

Open the top level build.gradle file which you will find in the root of your project and add the following line in the dependencies section:

打开将在项目根目录中找到的顶级build.gradle文件,并在“ dependencies”部分中添加以下行

classpath 'com.android.tools.build:gradle:3.4.0'

You will also need to update the distribution URL in the gradle wrapper properties file located at gradle/wrapper/gradle-wrapper.properties. Update the URL to the following.

您还需要在gradle / wrapper / gradle-wrapper.properties中的gradle包装器属性文件中更新分发URL 将URL更新为以下内容。

(This link will be available on the Android Gradle plugin release page.)

(此链接将在Android Gradle插件发布页面上提供 。)

distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip

If you are using Kotlin in your project, you will run into an error if your Kotlin Gradle plugin’s version is less than 1.3.0. If that’s the case, use the IDE’s prompt to update your Kotlin Gradle plugin to the latest version (which at the time of writing this post happens to be version 1.3.31).

如果您在项目中使用Kotlin,则如果Kotlin Gradle插件的版本低于1.3.0 ,则会遇到错误。 如果是这种情况,请使用IDE的提示将您的Kotlin Gradle插件更新为最新版本(在撰写本文时,该插件刚好是1.3.31版 )。

Alright, let’s run the build again from the terminal to see if we achieved any improvements.

好了,让我们从终端再次运行该构建,以查看是否取得了任何改进。

步骤2:更新您的配置 (Step 2: Update your configurations)

So we were able to shed around 2.5 minutes from the build time but it’s still not good enough. Upon investigating the build logs in the terminal, I came across one line which is of interest to us:

因此,我们能够从构建时间开始减少大约2.5分钟的时间,但这仍然不够好。 在终端中调查构建日志后,我遇到了一条我们感兴趣的行:

Incremental compilation basically prevents wasteful compilation of the entire set of source files and instead compiles only the files that have changed. Looking at the logs, it’s clear that we’re not taking advantage of this feature. It suggests us to use android.enableSeparateAnnotationProcessing=true but since we’re using Kotlin in our projects, we should not be using the ‘annotationProcessor’ configuration anyways.

增量编译基本上可以防止浪费地编译整个源文​​件集,而是仅编译已更改的文件。 查看日志,很明显我们没有利用此功能。 它建议我们使用android.enableSeparateAnnotationProcessing = true,但是由于我们在项目中使用的是Kotlin,因此无论如何我们都不应该使用“ annotationProcessor”配置。

Luckily, Kotlin version 1.3.30 added the support for incremental annotation processing.

幸运的是,Kotlin 版本1.3.30添加了对增量注释处理的支持。

So let’s

那么我们

  1. Change the annotationProcessor configuration to kapt

    注解 处理器配置更改为kapt

  2. Enable the incremental annotation processing experimental flag

    启用增量注释处理实验标记

Open your module level build.gradle file and add the following line to the top of the file:

打开模块级别的build.gradle文件,并将以下行添加到文件顶部:

apply plugin: 'kotlin-kapt'

Next, change all annotationProcessor configurations in the dependencies section to use kapt. Here’s an example:

接下来,更改“依赖关系”部分中的所有注释处理器配置,以使用kapt。 这是一个例子:

//Before
annotationProcessor 'com.google.dagger:dagger-compiler:2.9'

//After
kapt 'com.google.dagger:dagger-compiler:2.9'

Now open up your gradle.properties file located at the root of your project and add the following line:

现在打开位于项目根目录的gradle.properties文件,并添加以下行:

kapt.incremental.apt=true

Let’s run the build again. ??????

让我们再次运行构建。 ??????

Alright, looks like we’re getting there.

好吧,看来我们要到达那里。

步骤3:Gradle属性 (Step 3: Gradle Properties)

We’re in the last stage now. Remember the gotcha we came across while updating our Gradle plugin version? Turns out the newer versions of Gradle reduce the heap size to 512 MB. This is to make sure lower end machines don’t choke up. I am on a 16 gig machine so I can afford to give around 2–3gigs to the Gradle daemon, but your mileage may vary.

我们现在处于最后阶段。 还记得我们在更新Gradle插件版本时遇到的陷阱吗? 事实证明,较新版本的Gradle会将堆大小减小到512 MB。 这是为了确保低端机器不会阻塞。 我使用的是16演出机,因此我可以负担大约2-3演出给Gradle守护程序,但是您的里程可能会有所不同。

Open the gradle.properties file located at the root of your project and add the following line. Remember to select the size according to your requirements and machine specification.

打开位于项目根目录的gradle.properties文件,并添加以下行。 切记根据您的要求和机器规格选择尺寸。

org.gradle.jvmargs=-Xmx3072m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

While we’re at it, let’s also enable parallel builds and configure on demand in the properties.

在讨论的同时,我们还启用并行构建并在属性中按需配置。

Here’s what my final gradle.properties file looks like:

这是我最终的gradle.properties文件如下所示:

org.gradle.jvmargs=-Xmx3072m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

org.gradle.parallel=true

org.gradle.configureondemand=true

kapt.incremental.apt=true
  • org.gradle.parallel - This flag allows Gradle to build modules within a project in parallel instead of sequentially. This is only beneficial in a multi-module project.

    org.gradle.parallel此标志允许Gradle在项目内并行而不是顺序地构建模块。 这仅在多模块项目中是有益的。

  • org.gradle.configureondemand - This flag configures only the modules needed by the project, instead of building all of them.

    org.gradle.configureondemand此标志仅配置项目所需的模块,而不是构建所有模块。

With these, let’s see where we are on our build speed metric:

有了这些,让我们看看构建速度指标的位置:

And there we go. ???

然后我们走了。 ???

结束语 (Closing remarks)

This is by no means an extensive coverage of all the ways one can optimize the build speed of their project. There are tons of other things which I did not go over in this post like using minSdk 21 when using MultiDex, pre-dexing your libraries, disabling PNG crunching, and so on — to name a few.

这绝不是涵盖可以优化其项目构建速度的所有方式的广泛内容。 在这篇文章中,我还有很多其他内容,例如在使用MultiDex时使用minSdk 21,对您的库进行预删除,禁用PNG压缩等等,等等。

But most of these configurations require a deeper understanding of Android’s build system and experience working on large multi-module projects (which is where the benefits are most apparent). The steps I mentioned above are easy to incorporate in a project even by junior devs and have considerable payoffs. I hope this helps you trim down your build speeds!

但是,这些配置中的大多数都需要对Android的构建系统有更深入的了解,并且需要具有处理大型多模块项目的经验(这是最明显的好处) 。 我上面提到的步骤即使对于初级开发人员也很容易纳入项目中,并且具有可观的收益。 我希望这可以帮助您降低构建速度!

Alright until next time, peace! ✌?

好吧,直到下一次,和平! ✌?

翻译自: https://www.freecodecamp.org/news/how-to-improve-the-build-speed-of-your-android-projects-bd49029d8602/

android多项目构建

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值