Call Gradle task synchronously

Answered

Hi,

In my plugin I need to invoke specific Gradle tasks and wait for them to be finished before continuing. Furthermore, this is in Android Studio.

I tried using `com.android.tools.idea.gradle.invoker.GradleInvoker` but it looks like it's always running tasks asynchronously. Is there some other API that I am missing?

Thanks.

0
4 comments

Hi, you can try com.intellij.openapi.externalSystem.util.ExternalSystemUtil#runTask method, e.g.

ExternalSystemTaskExecutionSettings settings = new ExternalSystemTaskExecutionSettings();
settings.setExternalProjectPath(projectPath);
settings.setTaskNames(Collections.singletonList("build"));
settings.setVmOptions(gradleVmOptions);
settings.setExternalSystemIdString(GradleConstants.SYSTEM_ID.getId());
ExternalSystemUtil.runTask(settings, DefaultRunExecutor.EXECUTOR_ID, project, GradleConstants.SYSTEM_ID,
taskCallback, ProgressExecutionMode.MODAL_SYNC, false);

 

1

Thanks, your solution works fine! I had to use an async ProgressExecutionMode though, I'm not sure why. A popup was showing an indeterminate progress bar, but it looked like the task was doing nothing at all, so the Gradle invocation never finished. The only difference I had with your code is that the version of `ExternalSystemUtil.runTask` I have doesn't contain the last boolean parameter, probably because Android Studio is using an older version of the API.

Anyway, it works fine with IN_BACKGROUND_ASYNC + a callback.

0

ah, yes, sorry, I've found an issue with MODAL_SYNC mode, wrong modality was used, I've fixed it in the dev branch: https://github.com/JetBrains/intellij-community/commit/aea091674fd071c396cbf01e38bb30a6fe203127

Usually, it's better to call gradle tasks asynchronously, with progress information in modal window or in status bar to let user know what's going on. However, I've added additional execution modes for usecases where no progress information needed or it is implemented in other way: NO_PROGRESS_SYNC, NO_PROGRESS_ASYNC, see details at https://github.com/JetBrains/intellij-community/commit/07a47c9f9ef5a80d7fe34e29fb461ae4f95d7bef

Thanks for the feedback,
Vlad

0

I am also trying to run a gradle task on a selected resource, in my intelliJ plugin. In original snippet Vladislav Soroka posted (shown below) how are the projectPath, gradleVmOptions, GradleConstants and taskCallback objects retrieved?

ExternalSystemTaskExecutionSettings settings = new ExternalSystemTaskExecutionSettings();
settings.setExternalProjectPath(projectPath);
settings.setTaskNames(Collections.singletonList("build"));
settings.setVmOptions(gradleVmOptions);
settings.setExternalSystemIdString(GradleConstants.SYSTEM_ID.getId());
ExternalSystemUtil.runTask(settings, DefaultRunExecutor.EXECUTOR_ID, project, GradleConstants.SYSTEM_ID,
taskCallback, ProgressExecutionMode.MODAL_SYNC, false);

I am quite new to plugin development and would appreciate any explanations!

Many thanks,

Ashreen

0

Please sign in to leave a comment.