IntelliJ Platform Plugin 开发概要
本文操作基于 IntelliJ IDEA 2019 版本
下载 IntelliJ
官网下载 IntelliJ 后直接安装启动。
![](https://img.haomeiwen.com/i1234624/405a214ee3ac42ff.png)
启动完成后点击 Create New Project
创建新的项目,在新的界面中点击左侧 IntelliJ Platform Plugin
,右侧选择 Project SDK,若第一次创建插件项目下拉列表没有选项,则需点击右侧 New
指定 SDK 目录。
![](https://img.haomeiwen.com/i1234624/bfe8483834b65078.png)
![](https://img.haomeiwen.com/i1234624/caa123e14be2ff51.png)
完成后点击 Next
进入下一步,在弹出框中选择 JDK,最后点击 Finish
。
![](https://img.haomeiwen.com/i1234624/199676042e3db952.png)
创建 Action
plugin.xml 文件是插件项目的配置文件,和 Android 中的 AndroidManifest.xml 大同小异。
![](https://img.haomeiwen.com/i1234624/b874ce70c4a87884.png)
右键单击左侧 src 建立 Action 。
![](https://img.haomeiwen.com/i1234624/861775c01544d7cd.png)
配置 Action ID、Class Name、Desc 等,下面的 Group 的配置用于显示该 Action 的位置,比如工具栏、侧边栏、右键单击列表等位置。
![](https://img.haomeiwen.com/i1234624/a3209daa931dd26b.png)
配置完成后点击 OK
。
此时,plugin.xml 文件中多了如下配置:
<actions>¸Z
<!-- Add your actions here -->
<action id="Action.Demo" class="DemoAction" text="ActionDemoTools"
description="A Demo for IntelliJ Platform Plugin ">
<add-to-group group-id="MainToolBar" anchor="first"/>
</action>
</actions>
可以给 Action 添加 logo,用于在标题栏、工具栏等位置时显示。
<actions>¸Z
<!-- Add your actions here -->
<action id="Action.Demo" class="DemoAction" text="ActionDemoTools"
description="A Demo for IntelliJ Platform Plugin " icon="/logo.png">
<add-to-group group-id="MainToolBar" anchor="first"/>
</action>
</actions>
编写 Action 代码
Action 类的 actionPerformed 方法便是插件行为的入口方法,通过该方法编写插件所需要的功能即可,编写完成后点击工具栏“运行”按钮,查看效果。
public class DemoAction extends AnAction {
@Override
public void actionPerformed(AnActionEvent e) {
// TODO: insert action logic here
}
}
运行后查看自己编写的 Action 是否显示在了具体位置,比如工具栏显示了 ActionDemo 的图标,点击之后就是 actionPerformed() 方法中的具体功能了。
![](https://img.haomeiwen.com/i1234624/0df009fc0012b466.png)
发布 Plugin
鼠标依次点击 Build
-> Prepare Plugin Model...
将编写好的插件导出以供使用和发布,导出后项目目录中会多出一个插件文件,可以将该插件安装到 Android Studio 或 IntelliJ 等平台。
![](https://img.haomeiwen.com/i1234624/1813f9e93b8d14a1.png)
若要发布到插件仓库,需要注册 jetbrains 账号后点击个人头像 Upload 即可。