在与 Android Studio 相关的 build.gradle 文件中无法访问的几个任务

Several tasks not accessable in build.gradle file in connection with Android Studio

我在 Android Studio 中发现了一个奇怪的行为。

我想访问我的模块 build.gradle 文件中的 dexDebug 任务。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.testprj.test"
        minSdkVersion 10
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dexDebug.doFirst {
    // do some stuff here
}

但我收到以下错误:Error:(rowNumber, 0) Could not find property 'dexDebug' on project ':app'.

这很奇怪,因为我可以通过 Android Studio 终端通过 gradle dexDebug 调用此任务。

这是什么原因?

创建你自己的任务:

task myTask() {

}

dexDebug 之前完成任务 "process stuff":

gradle.taskGraph.whenReady { taskGraph ->

    if (taskGraph.hasTask(dexDebug)) { // <-- check if your task is ready
        dexDebug.dependsOn myTask // <-- make it depend on your task
    }
}