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?
请先登录再写评论。
See com.intellij.task.ProjectTaskListener/com.intellij.task.ProjectTaskListener#TOPIC if you need to work with external build system.
I'm using version 2018.3.4 and I cannot find the ProjectTaskListener class.
It is available since 2019.1 only
Would this work being that Android Studio uses 2018.3.4?
No, since it's part of the underlying platform.
Okay and there aren't any other ways to do this?
There is an Android Studio specific topic:
com.android.tools.idea.project.AndroidProjectBuildNotifications#TOPIC
com.android.tools.idea.project.AndroidProjectBuildNotifications.AndroidProjectBuildListener
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.
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.
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
CompilationListener.java Implementation
Do you have more than one project open? Please see usage pattern in com.android.tools.idea.res.ResourceNotificationManager
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.
See com.android.tools.idea.res.ResourceNotificationManager in Android plugin
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.
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.
I only receive the Android Build events.
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
where that in turn invokes a class with AndroidProjectBuildNotifications.subscribe(...) but that immediately throws
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!
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