简体   繁体   中英

How to stop Android Studio from running my gradle task when syncing project with Gradle files?

From the docs ,

When you make changes to the build configuration files in your project, Android Studio requires that you sync your project files so that it can import your build configuration changes and run some checks to make sure your configuration won't create build errors.

my build.gradle for app:

apply plugin: 'com.android.application'

android {
    //...  <--modified here
}

task myTask() {
    println 'doing something...'
    //time-consuming operations which may takes 16s or more
    println 'done'
}

I just found that it runs myTask every time I modified the build.gradle for app, even if I didn't change it.

Is that a normal operation when running some checks to make sure your configuration won't create build errors ? If that is, how can I make the task not run when syncing?

Environment :
Android Studio 3.1.3 with gradle-4.9-bin.zip

Your task does not run at all, it is just configured.

Gradle distinguishes between the configuration phase and the execution phase . On each invocation, every task is configured, but only the tasks passed to Gradle and their task dependencies are executed. As IntelliJ / AndroidStudio invokes Gradle on sync, every task is configured on sync. When you define a task in Gradle, the closure you pass is used for configuration. Only task actions (internal functionality), doFirst and doLast closures are executed during execution phase . For your custom task, you should put your functionality into such a closure:

task example {
    println 'Configuration' 
    doLast {
        println 'Execution' 
    } 
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM