简体   繁体   English

将项目与Gradle文件同步时,如何停止Android Studio运行gradle任务?

[英]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. 当您在项目中更改构建配置文件时,Android Studio要求您同步项目文件,以便它可以导入构建配置更改并运行一些检查以确保您的配置不会产生构建错误。

my build.gradle for app: 我的app.buildle.gradle

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. 我刚刚发现,即使我没有更改,每次我修改app的build.gradle时,它都会运行myTask

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 带有gradle-4.9-bin.zip的Android Studio 3.1.3

Your task does not run at all, it is just configured. 您的任务根本不会运行,而只是配置了。

Gradle distinguishes between the configuration phase and the execution phase . Gradle区分配置阶段执行阶段 On each invocation, every task is configured, but only the tasks passed to Gradle and their task dependencies are executed. 在每次调用时,都会配置每个任务,但是仅执行传递给Gradle的任务及其任务相关性。 As IntelliJ / AndroidStudio invokes Gradle on sync, every task is configured on sync. 当IntelliJ / AndroidStudio在同步时调用Gradle时,每个任务都将在同步时进行配置。 When you define a task in Gradle, the closure you pass is used for configuration. 在Gradle中定义任务时,您通过的闭包将用于配置。 Only task actions (internal functionality), doFirst and doLast closures are executed during execution phase . 执行阶段仅执行任务动作(内部功能), doFirstdoLast关闭。 For your custom task, you should put your functionality into such a closure: 对于您的自定义任务,您应该将功能放入这样的闭包中:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 Android Studio 项目与 Gradle 文件同步 - Syncing Android Studio project with Gradle files Gradle无法在新的Android Studio 2.0中同步我的项目 - Gradle is failing on syncing my project in the new Android Studio 2.0 Android Studio:从 G​​ithub 导入项目并使用 gradle 文件重建/清理/同步后“无法解析符号 R” - Android Studio: "cannot resolve symbol R" after importing Project from Github and rebuilding/cleaning/syncing with gradle files 同步Gradle时遇到Android Studio问题 - Running into issues in Android Studio while syncing Gradle Android Studio Gradle无法同步 - Android Studio Gradle not Syncing 从Android Studio项目中删除Android(应用程序或lib)模块需要在每次项目启动时将项目与gradle文件同步 - Removing Android (app or lib) module from Android Studio project requires syncing project with gradle files at every project startup 将 firebase 添加到 android 工作室时 gradle 不同步 - gradle not syncing when adding firebase to android studio 我在Android Studio中启动了新应用程序,同时同步文件在Gradle同步文件中出错 - I started new application in my Android Studio, while syncing the files got error on Gradle syncing file Android Studio中的Gradle同步错误 - Gradle Syncing error in android studio 在Android Studio中更新android项目后,我的gradle构建未运行 - My gradle build is not running after updating the android project in android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM