如何在Android Studio的build.gradle中控制任务执行顺序?

Stackbiz

我正在使用Android Studio 4.1和Gradle 6.7.1

(请注意,“ mergeReleaseAssets”不是我的自定义任务,它是系统任务的一部分,并且我不知道在哪里可以找到“ mergeReleaseAssets”的定义)

我想将任务“ mergeReleaseAssets”设置为在“ myTask”之后执行,如下所示:

...
> Task :app:myTask
...
(no matter how many tasks in the middle should be ok)
...
> Task :app:mergeReleaseAssets
...

build.gradle中的任务定义:

...
task myTask {
    doLast {
        println String.format("put something in the assets")
    }
}
tasks.withType(JavaCompile) { t -> t.finalizedBy myTask }
...

通过单击菜单栏->“构建”->“重建项目”来构建项目时,任务执行顺序并不总是相同,似乎任务是并行执行的。

首次生成输出:

Executing tasks: [clean, :app:assembleRelease] in project /home/testproject

> Task :clean UP-TO-DATE
> Task :app:clean UP-TO-DATE
> Task :app:preBuild UP-TO-DATE
> Task :app:preReleaseBuild UP-TO-DATE
> Task :app:compileReleaseAidl NO-SOURCE
> Task :app:compileReleaseRenderscript NO-SOURCE
> Task :app:generateReleaseBuildConfig
> Task :app:checkReleaseAarMetadata
> Task :app:generateReleaseResValues
> Task :app:generateReleaseResources
> Task :app:createReleaseCompatibleScreenManifests
> Task :app:extractDeepLinksRelease
> Task :app:processReleaseMainManifest
> Task :app:processReleaseManifest
> Task :app:processReleaseManifestForPackage
> Task :app:mergeReleaseResources
> Task :app:processReleaseResources
> Task :app:mergeReleaseNativeDebugMetadata NO-SOURCE
> Task :app:mergeReleaseShaders
> Task :app:javaPreCompileRelease

> Task :app:compileReleaseJavaWithJavac
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

> Task :app:myTask
> Task :app:compileReleaseSources
> Task :app:lintVitalRelease
> Task :app:compileReleaseShaders NO-SOURCE
> Task :app:generateReleaseAssets UP-TO-DATE
> Task :app:mergeReleaseAssets
> Task :app:compressReleaseAssets
> Task :app:dexBuilderRelease
> Task :app:checkReleaseDuplicateClasses
> Task :app:processReleaseJavaRes NO-SOURCE
> Task :app:collectReleaseDependencies
> Task :app:sdkReleaseDependencyData
> Task :app:mergeReleaseJniLibFolders
> Task :app:mergeReleaseNativeLibs
> Task :app:stripReleaseDebugSymbols NO-SOURCE
> Task :app:validateSigningRelease
> Task :app:mergeReleaseJavaResource
> Task :app:desugarReleaseFileDependencies
> Task :app:mergeExtDexRelease
> Task :app:mergeDexRelease
> Task :app:packageRelease
> Task :app:assembleRelease

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.7.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 2m 0s
32 actionable tasks: 30 executed, 2 up-to-date

Build Analyzer results available

第二次的构建输出:

Executing tasks: [clean, :app:assembleRelease] in project /home/testproject

> Task :clean UP-TO-DATE
> Task :app:clean UP-TO-DATE
> Task :app:preBuild UP-TO-DATE
> Task :app:preReleaseBuild UP-TO-DATE
> Task :app:compileReleaseAidl NO-SOURCE
> Task :app:compileReleaseRenderscript NO-SOURCE
> Task :app:generateReleaseBuildConfig
> Task :app:checkReleaseAarMetadata
> Task :app:generateReleaseResValues
> Task :app:generateReleaseResources
> Task :app:javaPreCompileRelease
> Task :app:createReleaseCompatibleScreenManifests
> Task :app:extractDeepLinksRelease
> Task :app:processReleaseMainManifest
> Task :app:processReleaseManifest
> Task :app:processReleaseManifestForPackage
> Task :app:mergeReleaseNativeDebugMetadata NO-SOURCE
> Task :app:mergeReleaseShaders
> Task :app:compileReleaseShaders NO-SOURCE
> Task :app:generateReleaseAssets UP-TO-DATE
> Task :app:mergeReleaseAssets
> Task :app:compressReleaseAssets
> Task :app:checkReleaseDuplicateClasses
> Task :app:processReleaseJavaRes NO-SOURCE
> Task :app:mergeReleaseResources
> Task :app:processReleaseResources
> Task :app:mergeReleaseJavaResource

> Task :app:compileReleaseJavaWithJavac
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

> Task :app:desugarReleaseFileDependencies
> Task :app:myTask
> Task :app:compileReleaseSources
> Task :app:lintVitalRelease
> Task :app:dexBuilderRelease
> Task :app:collectReleaseDependencies
> Task :app:sdkReleaseDependencyData
> Task :app:mergeReleaseJniLibFolders
> Task :app:validateSigningRelease
> Task :app:mergeReleaseNativeLibs
> Task :app:stripReleaseDebugSymbols NO-SOURCE
> Task :app:mergeExtDexRelease
> Task :app:mergeDexRelease
> Task :app:packageRelease
> Task :app:assembleRelease

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.7.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 1m 54s
32 actionable tasks: 30 executed, 2 up-to-date

Build Analyzer results available

如您所见,第一次,顺序是“ myTask”-> ...->“ mergeReleaseAssets”。 但是第二次更改为“ mergeReleaseAssets”-> ...->“ myTask”。

然后,我尝试在build.gradle中设置任务依赖项:

project.getTasks().getByName("mergeReleaseAssets").dependsOn(myTask)
//This also not work
//project.getTasks().getByName(":app:mergeReleaseAssets").dependsOn(myTask)

但是Android Studio显示错误:

Task with name "mergeReleaseAssets" not found in project ':app'

//Or this error if using ":app:mergeReleaseAssets":
Task with name ":app:mergeReleaseAssets" not found in project ':app'

请帮忙,谢谢。

里丘蒂

问题在于mergeReleaseAssets任务是动态创建的,因此您不能直接在build.gradle脚本中引用它

一种解决方案是使用Project.afterEvaluate生命周期钩所描述这里

project.afterEvaluate{ 
    // at this stage, task 'mergeReleaseAssets' has been declare, you can reference it to perform additional configuration
    project.getTasks().getByName("mergeReleaseAssets").dependsOn(myTask) 
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected].com 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类常见问题

Android Studio:Gradle-构建失败-任务':dexDebug'的执行失败

来自分类Dev

如何在Android Studio Gradle中降级Proguard版本?

来自分类Dev

Android Studio Gradle风格未在Build Variants中显示

来自分类Dev

如何在Android Studio 0.3.6中运行Gradle 1.9?

来自分类Dev

如何在Android Studio中实现搜索?

来自分类Dev

如何解决Android Studio卡住执行Gradle任务的问题?

来自分类Dev

Gradle任务无法在Android Studio中执行

来自分类Dev

Android Studio-Gradle任务在SYNC之后执行

来自分类Dev

如何在Android Studio中投影干净?

来自分类Dev

在Android Studio中,如何运行特定的Build Variant?

来自分类Dev

从build.gradle在Android Studio中运行应用程序之前,请运行gradle任务“ uninstallAll”

来自分类Dev

如何在Android Studio中创建SDK

来自分类Dev

如何在Android Studio中获取gradle --debug的输出?

来自分类Dev

如何在android studio中更新插件?

来自分类Dev

如何在Android Studio中解决ClassNotFoundException

来自分类Dev

如何在Android Studio 2.2预览版1中正确使用NDK-Build

来自分类Dev

Android Studio-如何在build.gradle中编辑此字体颜色

来自分类Dev

Android Studio中的Gradle Build问题

来自分类Dev

Android Studio中的Gradle任务排序问题

来自分类Dev

Android Studio-如何在Kotlin中执行doubleClick?

来自分类Dev

如何控制不相关的Alteryx IO任务的执行顺序?

来自分类Dev

Android Studio / Gradle Build问题

来自分类Dev

模块依赖性导致错误:Gradle:Android Studio中的任务“:app:preDexDebug”的执行失败

来自分类Dev

Android Studio中的build.gradle文件错误

来自分类Dev

Android Studio-如何在build.gradle中编辑此字体颜色

来自分类Dev

如何在 Android Studio 中固定 gradle?

来自分类Dev

在 Android Studio 中的 :app:processDebugManifest gradle 任务期间执行失败

来自分类Dev

Android Studio 中的任务执行异常

来自分类Dev

如何在 Android Studio 中清理 Gradle?

Related 相关文章

  1. 1

    Android Studio:Gradle-构建失败-任务':dexDebug'的执行失败

  2. 2

    如何在Android Studio Gradle中降级Proguard版本?

  3. 3

    Android Studio Gradle风格未在Build Variants中显示

  4. 4

    如何在Android Studio 0.3.6中运行Gradle 1.9?

  5. 5

    如何在Android Studio中实现搜索?

  6. 6

    如何解决Android Studio卡住执行Gradle任务的问题?

  7. 7

    Gradle任务无法在Android Studio中执行

  8. 8

    Android Studio-Gradle任务在SYNC之后执行

  9. 9

    如何在Android Studio中投影干净?

  10. 10

    在Android Studio中,如何运行特定的Build Variant?

  11. 11

    从build.gradle在Android Studio中运行应用程序之前,请运行gradle任务“ uninstallAll”

  12. 12

    如何在Android Studio中创建SDK

  13. 13

    如何在Android Studio中获取gradle --debug的输出?

  14. 14

    如何在android studio中更新插件?

  15. 15

    如何在Android Studio中解决ClassNotFoundException

  16. 16

    如何在Android Studio 2.2预览版1中正确使用NDK-Build

  17. 17

    Android Studio-如何在build.gradle中编辑此字体颜色

  18. 18

    Android Studio中的Gradle Build问题

  19. 19

    Android Studio中的Gradle任务排序问题

  20. 20

    Android Studio-如何在Kotlin中执行doubleClick?

  21. 21

    如何控制不相关的Alteryx IO任务的执行顺序?

  22. 22

    Android Studio / Gradle Build问题

  23. 23

    模块依赖性导致错误:Gradle:Android Studio中的任务“:app:preDexDebug”的执行失败

  24. 24

    Android Studio中的build.gradle文件错误

  25. 25

    Android Studio-如何在build.gradle中编辑此字体颜色

  26. 26

    如何在 Android Studio 中固定 gradle?

  27. 27

    在 Android Studio 中的 :app:processDebugManifest gradle 任务期间执行失败

  28. 28

    Android Studio 中的任务执行异常

  29. 29

    如何在 Android Studio 中清理 Gradle?

热门标签

归档