Gradle:构建Android库后如何运行自定义任务?

编程入门 行业动态 更新时间:2024-10-24 05:23:13
本文介绍了Gradle:构建Android库后如何运行自定义任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个Android库,它正在生成debug.aar和release.aar,我需要将release.aar复制到另一个文件夹中,以引用项目的其他部分。

I have an Android Library, it's generating a debug.aar and a release.aar, I need to copy the release.aar to another folder as a reference to other part of the project.

我现在要做的是在这个Android库build.gradle中定义一个任务:

What I've done now is in this Android Library build.gradle I defined a task:

task copyAARToCommonLibs(type: Copy) { from('../build/outputs/aar') { include '*-release.arr' } into '../SomeSampleApps/libs' }

我正在尝试在生成了arr,我假设它是assembleRelease阶段,因此我在此build.gradle中尝试了此操作。

I'm trying to run this task after the arr is generated, which I assume is assembleRelease stage, so I tried do this in this build.gradle

assembleRelease.doLast{ copyAARToCommonLibs }

我使用

gradle build

但是

我也尝试过这样做:

applicationVariants.all { variant -> variant.assemble.doLast { copyAARToCommonLibs } }

内部android {}属性(我想这就是它的名字吗?)正在运行gradle构建,出现此错误:找不到属性'applicationVariants'

inside android{} property(I guess that's what it's called?) Running gradle build, got this error: Could not find property 'applicationVariants'

然后我遇到了以下代码段:

I then came across this snippet:

tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn copyAARToCommonLibs }

但这似乎使任务在编译后即可运行,我不

But it seems this makes the task to run after compiling, I don't know exactly how to modify this to run after assemble.

有人可以请我纠正我做错的地方,以及如何在.arr文件保存后使此复制任务正常工作。

Could someone please correct me where I did wrong and how can I get this copy task work after the .arr file is generated?

推荐答案

似乎 finalizedBy 可能会有所帮助。

assembleRelease.finalizedBy(copyAARToCommonLibs)

请注意以下事实,您将不会定义依赖项:

Mind the fact that in the following way you won't define a dependency:

assembleRelease.doLast { copyAARToCommonLibs }

实际上..它完全不执行任何操作。您需要执行任务:

actually.. it does exactly nothing. You need to execute the task:

assembleRelease.doLast { copyAARToCommonLibs.execute() }

但不鼓励以以下方式运行任务,这是非常糟糕的做法。

but running task in the following way is discouraged and very bad practice.

您也可以尝试:

assembleRelease.doLast { copy { from('../build/outputs/aar') { include '*-release.aar' } into '../AscendonSDKSamples/libs' } }

更多推荐

Gradle:构建Android库后如何运行自定义任务?

本文发布于:2023-10-04 12:22:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1467161.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   Gradle   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!