Listen for Compilation event in Android Studio

已回答

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?

1

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

0

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

0

It is available since 2019.1 only

0

Would this work being that Android Studio uses 2018.3.4?

0

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

0

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

0

There is an Android Studio specific topic:

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

0

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

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

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

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

0

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

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

0

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

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

I only receive the Android Build events.

0

Hello, sorry to resurrect an old thread but none of the above approaches work for me to detect Android builds.

I have tried subscribing to CompilerTopics.COMPILATION_STATUS, ExecutionManager.EXECUTION_TOPIC, and ProjectTaskListener.TOPIC - but none of these seem to fire in Android Studio, although they work fine in IntelliJ.

I have also tried adding "android" to my plugins configuration in gradle then in my plugin.xml adding

<depends optional="true" config-file="android_config.xml">org.jetbrains.android</depends>

where that in turn invokes a class with AndroidProjectBuildNotifications.subscribe(...) but that immediately throws 

java.lang.ClassNotFoundException: com.android.tools.idea.project.AndroidProjectBuildNotifications$BuildContext

when trying to load the plugin in Android Studio 2021.3.1 (Dolphin)

It seems this class has been stable for some time so I'm unsure why this approach is no longer working https://github.com/JetBrains/android/blob/master/android/src/com/android/tools/idea/project/AndroidProjectBuildNotifications.java

Any suggestions here?

Thanks!

1

Harold Martin This class seems to have been removed AFAIU com.android.tools.idea.projectsystem.ProjectSystemBuildManager.BuildListener could be used as replacement in newer IDEs

0

请先登录再写评论。