Listen for Compilation event in Android Studio

Answered

I've created a plugin that performs an analysis after the project compiles using CompilerTopics.COMPILATION_STATUS/ CompilationStatusListener. The plugin works fine with IntelliJ but not with Android Studio. It seems Android Studio is using a different topic.

Is there a topic I can subscribe to be notified when am APK is generated or the project is built/rebuilt in Android Studio?

16 comments
Comment actions Permalink

See com.intellij.task.ProjectTaskListener/com.intellij.task.ProjectTaskListener#TOPIC if you need to work with external build system.

0
Comment actions Permalink

I'm using version 2018.3.4 and I cannot find the ProjectTaskListener class.

0
Comment actions Permalink

It is available since 2019.1 only

0
Comment actions Permalink

Would this work being that Android Studio uses 2018.3.4?

0
Comment actions Permalink

No, since it's part of the underlying platform.

0
Comment actions Permalink

Okay and there aren't any other ways to do this?

0
Comment actions Permalink

There is an Android Studio specific topic:

com.android.tools.idea.project.AndroidProjectBuildNotifications#TOPIC
com.android.tools.idea.project.AndroidProjectBuildNotifications.AndroidProjectBuildListener

0
Comment actions Permalink

Do I need to use a dependency in the plugin.xml or build.gradle file in order to use the AndroidProjectBuildListener? It doesn't seem to be available in my current setup.

0
Comment actions Permalink

Yes, in both places actually. This API is Android Studio specific, so you'll need to depend on Android plugin (optionally) http://www.jetbrains.org/intellij/sdk/docs/basics/plugin_structure/plugin_dependencies.html and use corresponding API in case your plugin runs in AS instead of IJ.

0
Comment actions Permalink

The solution suggested works but there is one problem. I keep getting two notifications from AndroidProjectBuildNotifications. I've tried applying different filters but I still receive two notifications. Which means that the code in buildComplete is being executed twice. Below is my implementation. 

Plugin.xml

<project-components>
<component>
<implementation-class>com.analysis.CompilationListener</implementation-class>
</component>
</project-components>

CompilationListener.java Implementation

public class CompilationListener implements ProjectComponent {

public CompilationListener(Project project) {
this.project = project;
}

@Override
public void initComponent() {

AndroidProjectBuildNotifications.subscribe(project, context -> {

if (context instanceof GradleBuildContext) {

GradleBuildContext gradleBuildContext = (GradleBuildContext) context;
GradleInvocationResult build = gradleBuildContext.getBuildResult();

if (build.isBuildSuccessful()) {

///Execute code
}
}
});
}
0
Comment actions Permalink

Do you have more than one project open? Please see usage pattern in com.android.tools.idea.res.ResourceNotificationManager

0
Comment actions Permalink

Only one project is open.

Do you have an example of the usage pattern? I've searched online but I don't see any good examples.

0
Comment actions Permalink

See com.android.tools.idea.res.ResourceNotificationManager in Android plugin

0
Comment actions Permalink

Are you suggesting that I should follow the implementation of com.android.tools.idea.res.ResourceNotificationManager to implement the notifications for AndroidProjectBuildNotifications? I've taken a look at the implementation for com.android.tools.idea.res.ResourceNotificationManager but I really don't see how I can use that to fix the problem I am having.

0
Comment actions Permalink

Yes, that was my suggestion. Please note that there are 2 events triggering this callback: JPS build and Android build (see AndroidGradleProjectComponent). Check whether you receive different implementations passed in BuildContext parameter.

0
Comment actions Permalink

I only receive the Android Build events.

0

Please sign in to leave a comment.