在gradle android项目中的所有可能的打包任务之前运行自定义任务 [英] Run custom task before ALL possible packaging tasks in gradle android project

查看:98
本文介绍了在gradle android项目中的所有可能的打包任务之前运行自定义任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 data-android 的gradle项目,该项目应用了 com.android.library 插件.在打包阶段之前,我要执行自己的自定义任务(名为 copySqlMigrations ).此任务将一堆资产复制到项目树中.

I have a gradle project named data-android that applies the com.android.library plugin. Before the packaging phase I want a custom task of my own (named copySqlMigrations) to be executed. This task copies a bunch of assets into the project tree.

我做了很多尝试,但是没有一个能普遍发挥作用.我最接近的是在gradle文件的末尾添加以下内容.

I have made many attempts, but none of them work universally. The closest I got was adding the following to the end of my gradle file.

afterEvaluate {
    assemble.dependsOn copySqlMigrations
    assembleDebug.dependsOn copySqlMigrations
    assembleRelease.dependsOn copySqlMigrations
}

这仅在我致电例如 gradle assembleDebug 直接,但是当我要启动我的APK(我的库项目是它的依赖项)时,这失败了.

This only works if I call e.g. gradle assembleDebug directly, but when I want to launch my APK (for which my library project is a dependency), this fails spectacularly.

Error:(56, 1) A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:_debugApk'.
   > A problem occurred configuring project ':data-android'.
      > Could not get unknown property 'assembleDebug' for project ':data-android' of type org.gradle.api.Project.

在打包此项目之前,我如何总是调用:data-android:copySqlMigrations ?我正在寻找一种适用于所有构建风格和所有潜在调用(构建/运行/调试)的解决方案.

How can I always call :data-android:copySqlMigrations before this project is being packaged up? I'm looking for a solution that works with all build flavors and all potential invocations (build/run/debug).

推荐答案

Gradle具有 TaskCollection 包含所有任务.可以通过 matching 方法查询 TaskCollection 的子集,并且借助一些魔术,新的 TaskCollection 可以运行.因此,每当新任务添加到父 TaskCollection (或 TaskContainer )并且它与 matching 方法的闭包匹配时,子集 TaskCollection 将包含它.只要与 TaskCollection all 方法一起使用,就可以按照创建的模式处理每个任务.

Gradle has a TaskContainer accessible via (project.)tasks. This container is also a TaskCollection containing all tasks. One can query a subset TaskCollection via the matching method and, thanks to some magic, the new TaskCollection is live. So, whenever a new task gets added to the parent TaskCollection (or the TaskContainer) and it matches the closure of the matching method, the subset TaskCollection will contain it. Together with the TaskCollection all method one can handle each task following a pattern whenever it is created.

对于您的问题中所述的问题,我编写并测试了以下构建文件:

For the problem stated in your question, I wrote and tested the following build file:

task assembleX { }

task copySqlMigrations { }

task assembleY { }

tasks.matching { task ->
    task.name.startsWith('assemble')
}.all { task ->
    task.dependsOn copySqlMigrations
}

task assembleZ { }

您可以调用每个 assemble * 任务,它将作为依赖项调用 copySqlMigrations .当然,您可以根据需要修改匹配的闭包.

You can call each assemble* task and it will call copySqlMigrations as a dependency. Of course, you can modify the matching closure to fit your needs.

这篇关于在gradle android项目中的所有可能的打包任务之前运行自定义任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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