AnActionListener not triggered when "Sync now" is pressed which is prompted by the IDE

已回答

I'm developing an Android Studio plugin and want to perform some action when the Gradle Sync happens. I added the following code:

public class CustomActionListener implements AnActionListener {
    @Override
    public void afterActionPerformed(@NotNull AnAction action, @NotNull AnActionEvent event,
                                     @NotNull AnActionResult result) {
        if (event.getProject() != null && action instanceof SyncProjectAction)
            // Do something
    }
}

This works when the user presses the Sync Project with Gradle files button manually

However when we make changes to the gradle files, the IDE prompts to sync the changes made

Pressing the Sync Now button here does not trigger the Action Listener at all. I tried putting a breakpoint as well and it does not get triggered at all.

 

I'm testing on Android Studio Koala 2024.1.1 Patch 1 (IntelliJ 2024.1). Not sure if this replicates on a plugin for IntelliJ 2024.1.

0

Hi,

The Sync Now in the top panel is handled here:
https://github.com/JetBrains/android/blob/master/project-system-gradle/src/com/android/tools/idea/gradle/notification/ProjectSyncStatusNotificationProvider.java#L223

It is not AnAction. It invokes GradleSyncInvoker.requestProjectSync(), which takes a listener, but null is passed here. I can see listeners can be registered with GradleSyncState.subscribe(Project, GradleSyncListener), so I would try this approach.

0

This worked! Thanks a lot. For future reference, here's the code

GradleSyncState.subscribe(project, new GradleSyncListener() {
    @Override
    public void syncSucceeded(@NotNull Project project) {
        // Do something
    }
});
0

请先登录再写评论。