如何组织通用gradle任务并提取通用代码块 [英] How to organize common gradle tasks and extract common code blocks

查看:81
本文介绍了如何组织通用gradle任务并提取通用代码块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含10多个库模块的android项目.这些模块中的每个模块都包含许多所有模块共有的gradle代码.例如,我在所有build.gradle文件中都使用此答案中的Javadoc生成任务.

I have an android project consisting of 10+ library modules. Each of these modules contains a lot of gradle code common to all modules. For example, I'm using the Javadoc generation task from this answer in all of my build.gradle files.

我该如何将任务创建逻辑提取到放置在单独的gradle文件中的函数中,该文件可以由每个模块包含"?我所有模块的代码都是相同的,但是显然取决于变量和项目.是否可以提取将项目作为参数并返回该项目的任务的函数?

How would I go about to extract that task creation logic to a function put in a separate gradle file which can be "included" by each module? The code is identical for all my modules, but obviously depends on variant and project. Is it possible to extract a function that takes a project as parameter and returns a task for that project?

由于我确实很喜欢Gradle,所以我可能会倒退,但任何指向我的指针都避免了在10个不同文件(lib1/build.gradle,lib2/build.gradle 、.中的相同)的gradle代码的60行...)会有所帮助!

I'm probably going around this backwards since I really suck at Gradle but any pointers leading to me avoiding having the same 60 lines of gradle code in 10 different files (lib1/build.gradle, lib2/build.gradle, ...) would be helpful!

实际上,甚至更进一步,除依赖项部分外,基本上所有项目的整个build.gradle都是相同的-有一个 android 块,其中包含 buildTypes compileOptions 等,其中应用了一些插件( apply plugin:'com.android.library'等),并且设置了一些工件参数.实际上,仅 我的依赖项完全不同.所以我想知道是否有可能像这样(显然是伪代码)完全提取一个公共文件:

In fact, taking it even further, basically my entire build.gradle is identical for all projects except the dependencies section - there's an android block with buildTypes, compileOptions etc, there are some plugins applied (apply plugin: 'com.android.library' etc), and there are some artifact parameters set up. In fact, only my dependencies differ at all. So I'm wondering if it would be possible to extract a common file completely, like this (pseudo code obviously):

<include common.gradle> // includes android block, common tasks, artifact setup etc. 

dependencies {
  api 'androidx.appcompat:appcompat:1.1.0'
  api 'com.google.code.gson:gson:2.8.5'
  ...
}

推荐答案

提取常见构建逻辑的推荐方法是

The recommended way of extracting common build logic is to leverage buildSrc and create plugins that are then applied to your projects.

如所记录的,这具有应用脚本的许多好处.

This has a number of benefits of applying a script, as documented.

此外,我建议通过插件应用程序进行显式显示.虽然您可以开发一个用于配置所有项目的插件,例如,基于项目名称的某些逻辑,但通常最好让不同的插件与不同的项目类型匹配.这样,打开构建文件时,您会立即看到它是一个什么样的项目.

In addition, I would recommend being explicit through plugin application. While you could develop a single plugin that configures all your projects, with some logic based on project name for example, it is usually better to have different plugins matching the different project types. That way, when opening a build file you immediately see what kind of project it is.

当然,您可以在 buildSrc 中获得通用代码,以了解通用部分.

And of course you can have common code in buildSrc for the common parts.

另一个好处是,从 buildSrc 插件迁移到已发布的插件要容易得多,以防万一您的配置模式开始出现在不同的项目中.

One more benefit is that moving from a buildSrc plugin to a published plugin is much easier ... just in case your configuration patterns start to appear in different projects.

这篇关于如何组织通用gradle任务并提取通用代码块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆