【发布时间】:2015-12-18 01:39:23
【问题描述】:
我有一个运行 shell 脚本的任务。它仅驻留在我的顶级 gradle 文件中。所以我所说的gradle文件可以在这里看到:
现在有趣的部分是,我在名为 copyFiles 的文件中有一个任务,它只运行一个简单的 shell 脚本来复制文件。我现在将向您展示我的整个 build.gradle 顶级文件:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
task copyFiles(type:Exec) {
exec {
commandLine 'sh', './copyFiles.sh'
}
}
现在,每当我构建项目时,都会执行此任务。只要我构建项目(也就是编译它),这个任务就会运行。这正常吗?我在那里放了一条打印线以确保每次我构建“应用程序”模块时它都会运行。这正常吗。如果我不希望它那样做呢?
更新:现在经过研究,我发现如果定义如下,一些 gradle 任务可以立即执行而不会被调用:
//option 1, this runs right away on build
task foo(type: Exec) {
// this is configuration
// i.e. it runs regardless
}
//option 2 this runs only when called on
task bar(type: Exec) << {
// this is execution
// i.e. it only runs if you call the task
}
然后我想我会尝试同样的事情:
task copyFiles(type: Copy) {
from '/Users/myuser/Documents/androidstudio/MyApplication/Common/'
into '/Users/myuser/Documents/androidstudio/MyApplication/app/src/main/assets'
}
但令我惊讶的是,它不会自行运行。我居然要打电话?这与 type:Exec 有何不同?为什么没有一致性?
更新:
我写了一篇关于 the gradle lifecycle 的博客,供在我分析后需要帮助的人使用。
【问题讨论】:
标签: android gradle android-gradle-plugin gradlew