简体   繁体   English

使用android gradle插件时,在packageApplication之后运行任务

[英]Run task after packageApplication when using the android gradle plugin

Gradle has the ability to run tasks after other tasks. Gradle具有在其他任务之后运行任务的能力。 The syntax is taskY.mustRunAfter taskX . 语法是taskY.mustRunAfter taskX The android gradle plugin says one of the ApplicationVariant tasks it defines is packageApplication . android gradle插件说它定义的ApplicationVariant任务之一是packageApplication

In my build.gradle I have taskX.mustRunAfter packageApplication 在我的build.gradle中,我有taskX.mustRunAfter packageApplication

The error I get is "Could not find property 'packageApplication' on project ':someproject'." 我得到的错误是“无法在项目中找到属性'packageApplication':someproject'。”

Is it even possible to get access to the packageApplication task? 甚至可以访问packageApplication任务吗? If so is it considered bad practice? 如果这样被认为是不好的做法?

I found my the answer. 我找到了答案。 rciovati cleared up the confusion around mustRunAfter and that what I was doing was the wrong approach. rciovati清除了围绕mustRunAfter的混乱,我正在做的是错误的做法。 I ended up converted taskY to a groovy function. 我最终将taskY转换为groovy函数。 Then used the following code: 然后使用以下代码:

android.applicationVariants.all { variant ->
    // rename apk after we assemble the application
    variant.assemble.doLast {
        taskY(variant)
}

Please note this sentence from the documentation: 请注意文档中的这句话:

By using 'must run after" ordering rule you can specify that taskB must always run after taskA, whenever both taskA and taskB are scheduled for execution . 通过使用“必须运行后”排序规则,您可以指定taskB必须始终在taskA之后运行, 只要taskA和taskB都被安排执行

This means that 这意味着

taskX.mustRunAfter packageApplication

doesn't make the taskX run always after packageApplication but just if you type: packageApplication之后不会使taskX始终运行,但只要键入:

./gradlew taskX packageApplication

On the other hand, it doesn't define a dependency, that is it doesn't automatically run taskX task. 另一方面,它没有定义依赖关系,也就是说它不会自动运行taskX任务。

To do something, after a task is executed you can use the doLast closure: 要执行某些操作,执行任务后您可以使用doLast闭包:

taskX.doLast{ println 'Hello' }

The packageApplication is a property of the ApplicationVariant class but not a task. packageApplicationApplicationVariant类的属性,但不是任务。 The tasks are package<VariantName> . 任务是package<VariantName>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Android Gradle 插件在编译前运行任务 - Run task before compilation using Android Gradle plugin Android:如何设置build.gradle文件以在同步后自动运行插件任务 - Android: how to set build.gradle file to run a plugin task automatically after sync 使用android gradle插件运行aapt - using android gradle plugin to run aapt 使用 android gradle 插件 3.6+ 执行 crashlyticsGenerateSymbols 任务时出错 - Error when executing crashlyticsGenerateSymbols task with android gradle plugin 3.6+ 运行单元测试时将 Android Gradle 插件从 3.5.3 更新到 3.6.1 后出现 OutOfMemoryError - Got OutOfMemoryError after update Android Gradle Plugin to 3.6.1 from 3.5.3 when run unit test Android Gradle - 如何在应用程序安装后运行任务 - Android Gradle - how to run task after app installation Gradle:构建Android库后如何运行自定义任务? - Gradle: How to run custom task after an Android Library is built? 在Android Gradle Plagin中运行gradle任务JavaExec吗? - Run gradle task JavaExec in Android Gradle Plagin? 如何在 Android 项目配置阶段在 Gradle 插件中运行自定义 Gradle 任务? - How to Run Custom Gradle Task In Gradle Plugin During Android Project Configuration Phase? 运行Gradle构建时运行Gradle插件任务(不是从命令行手动运行) - Run a Gradle plugin task when Gradle build is running (not manually from command line)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM