我正在尝试启动一项任务,将aar文件(android libs)复制到一个单独的文件夹,但任务会在构建开始之前一直触发。
我正在使用这个答案,但它对我不起作用:
Is there a method in Gradle to execute some task after the build?
这是我的傻瓜:
def buildLibrary = true;
if (buildLibrary) {
apply plugin: 'com.android.library'
} else {
apply plugin: 'com.android.application'
}
apply plugin: 'io.fabric'
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
flatDir {
dirs 'libs' //this way we can find the .aar file in libs folder
}
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
repositories {
maven { url 'https://maven.fabric.io/public' }
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
if (!buildLibrary) {
applicationId "myapp.com.mysdk"
}
minSdkVersion 17
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
// Support libraries and widgets
compile 'com.android.support:support-v13:23.1.0'
compile 'com.android.support:support-v4:23.1.0'
compile 'com.android.support:gridlayout-v7:23.1.1'
}
// Why this keeps getting called before the build starts????
task copyAARToCommonLibs(type: Copy) {
println 'calling copyAARToCommonLibs before libs are built!!!'
from('../build/outputs/aar') {
include '*-release.arr'
}
into '../MyOutput/libs'
println 'end of calling!!!'
}
build.finalizedBy(copyAARToCommonLibs)
// This did not work either
tasks.build.doLast(){
println 'This is never called!!!'
}
和干净构建的输出:
Executing tasks: [clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:compileDebugSources, :app:compileDebugAndroidTestSources]
Configuration on demand is an incubating feature.
Crashlytics was applied to an android-library project.
Android-library support is currently an incubating feature.
Contact support@fabric.io with any issues.
calling copyAARToCommonLibs before libs are built!!!
end of calling!!!
:clean UP-TO-DATE
:app:clean
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preDebugAndroidTestBuild UP-TO-DATE
:app:preDebugUnitTestBuild UP-TO-DATE
:app:preReleaseBuild UP-TO-DATE
:app:preReleaseUnitTestBuild UP-TO-DATE
... a bunch of stuff
:app:generateDebugAndroidTestBuildConfig
:app:generateDebugAndroidTestAssets UP-TO-DATE
:app:mergeDebugAndroidTestAssets
:app:generateDebugAndroidTestResValues UP-TO-DATE
:app:generateDebugAndroidTestResources
:app:mergeDebugAndroidTestResources
:app:processDebugAndroidTestResources
:app:generateDebugAndroidTestSources
:app:compileDebugAndroidTestJavaWithJavac
:app:compileDebugAndroidTestNdk UP-TO-DATE
:app:compileDebugAndroidTestSources
BUILD SUCCESSFUL
Total time: 13.157 secs
答案 0 :(得分:1)
您添加到copyAARToCommonLibs
的打印语句会误导您。根据它们在任务定义中的位置,它们在配置时执行,与任何任务间依赖性顺序无关。这就是打印在构建过程发生之前出现的原因(因为配置不依赖于任务依赖性)。
这是一个简单的例子:
task blahTask() {
println "Configuring finalizer"
doLast {
println "Actually running finalizer"
}
}
task toFinalize() {
println "Configuring to finalize"
doLast {
println "Actually running thing to finalize"
}
}
toFinalize.finalizedBy(blahTask)
当我执行toFinalize
时,输出为:
Configuring finalizer
Configuring to finalize
:app:toFinalize
Actually running thing to finalize
:app:blahTask
Actually running finalizer
BUILD SUCCESSFUL
正如您所看到的那样,终结器的配置代码实际上是在之前执行要完成的任务的配置代码(即独立于任务依赖性),而doLast
代码正在运行期望的顺序。
因此,以您拥有的方式使用print语句实际上并未指示复制任务何时运行。
要实现的另一件事是,在您的输出中(从您显示的内容)中没有:app:copyAARToCommonLibs
语句,表明该任务实际上并未运行(正在配置,因此打印)。根据{{3}},如果最终确定的任务(在您的情况下为build
)是最新的,则终结器任务将不会运行。引用:
另一方面,如果最终任务没有做任何工作,则不执行终结器任务,例如,如果它被认为是最新的或者依赖任务失败。