Gradle:构建Android库后如何运行自定义任务? [英] Gradle: How to run custom task after an Android Library is built?
问题描述
我有一个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库后如何运行自定义任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!