AS 插件开发

博文地址

我的GitHub 我的博客 我的微信 我的邮箱
baiqiantao baiqiantao bqt20094 baiqiantao@sina.com

目录

IDEA 插件开发

使用Gradle构建插件
managing-plugins
gradle-intellij-plugin

第一个 IDEA 插件

新建项目

File -> New -> Project -> 选择 Gradle -> 选择需要的库和框架 -> 填写项目信息 -> 确定

新建完工程之后,IDEA 会自动开始解析项目依赖,因为它要下载一个几百兆的 SDK 依赖包,所以可能会比较久。

配置 gradle

gradle 插件可以这样配置

File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle

为了方便,我将此目录拷到了根目录中

手动下载 ideaIC

因为 ideaIC 这个组件非常大,大约 500MB 左右,通过 IDEA 很难下载成功,可以采取如下方式下载:

  • 找到你上述配置的 gradle 的如下子目录,例如D:\_devgradle\_GRADLE_USER_HOMEcachesmodules-2files-2.1com.jetbrains.intellij.ideaideaIC
  • 这个就是 ideaIC 组件配置信息目录,我们不需要关心里面具体什么内容,只需要看文件夹名字即可,例如为:2020.2.4(这其实就是你所安装的 IDEA 的版本)
  • 根据你的版本号,直接用迅雷下载以下文件,我这边瞬间就下载完成了:
  • 取消 IDEA 中的下载进程,将上面通过迅雷下载的ideaIC-2020.2.4.zip拷到2020.2.4目录中
  • 同步一下项目,就会跳过下载ideaIC-2020.2.4.zip这个步骤,后面很快就会提示:BUILD SUCCESSFUL
  • 然后就可以把上述ideaIC-2020.2.4.zip直接删掉了(因为这个文件会被复制到其他目录中),注意复制的ideaIC-2020.2.4.zip文件不能删掉(虽然他已经被解压了,zip文件也不能删掉)

项目结构

依赖解析完成之后,项目结构如下图:

初始工程可能需要手动在 main 下创建 java 目录,再创建 package 目录

项目下只有3个有意义的文件:

  • plugin.xml:插件的配置
  • build.gradle:和 Android 项目下的同名文件类似,配置项目构建的脚本
  • settings.gradle:和 Android 项目下的同名文件类似,只有一行代码 rootProject.name = 'Bqtplugin'

plugin.xml中的初始内容大概如下

<idea-plugin>
    <id>com.bqt.test.plugin.BqtPlugin</id>
    <name>白乾涛的插件</name>
    <vendor email="0909082401@163.com" url="https://www.cnblogs.com/baiqiantao/">白乾涛</vendor>
    <description>插件说明,支持大部分 HTML 标签</description>

    <!-- please see https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html on how to target different products -->
    <depends>com.intellij.modules.platform</depends>

    <extensions defaultExtensionNs="com.intellij">
        <!--依赖的其他插件能力,Add your extensions here-->
    </extensions>

    <actions>
        <!--在这里定义插件动作-->
    </actions>
</idea-plugin>

创建 Action

Action 是 IDEA 中对事件响应的处理器,创建方式:在 package 上右键 -> New -> Plugin Devkit -> Action

需要填写的属性如下:

  • ActionID:代表该Action的唯一的ID
  • ClassName:类名
  • Name:就是最终插件在菜单上的名称
  • Description:对这个Action的描述信息
  • Groups:这个Action所存在的组
  • Anchor:这个Action在Groups中的相对位置
  • Keyboard Shortcut:调起此Action的快捷键

这些信息都会注册在plugin.xml中,后续可以手动修改。

public class MainMenuAction extends AnAction {

	@Override
	public void actionPerformed(AnActionEvent e) {
		Messages.showMessageDialog("Hello World !", "Information", Messages.getInformationIcon());
	}
}

调试插件

代码写完之后,选择 Plugin 后点击 Run 按钮就会启动一个安装了插件的 IDEA,然后就可以进行测试。你还可以右键启动 Debug 模式,这样还能进行断点。

以上等价于通过运行runIde任务

生成插件

点击buildPlugin任务即可生成插件,插件生成后被存放在uilddistributions目录中,是一个zip文件。

文件名由settings.gradle中的rootProject.name的值 + build.gradle中的version的值构成,例如:BqtPlugin-0.1.zip

常见 Action 效果

主页菜单

<actions>
	<group id="om.bqt.test.plugin.menu1"
		   text="我的插件"
		   description="这是一个主菜单插件">
		<add-to-group group-id="MainMenu" anchor="last"/>
		<action id="com.bqt.test.plugin.action1"
				class="com.bqt.test.plugin.BqtPlugin.MainMenuAction"
				text="测试主菜单"
				description="测试主菜单--这是描述">
			<keyboard-shortcut keymap="$default" first-keystroke="shift ctrl alt L"/>
		</action>
	</group>
</actions>

子菜单

工具栏

右键菜单

面板

一些细节

乱码问题

除了以下位置均设置为 UTF-8 外,还需要一个特殊的设置:

  • 双击 Shift 搜索 vmoptions,打开搜索到的文件(或通过菜单:Help--Edit Custom VM Options打开)

  • 如果没有该文件,请按照提示自动创建即可
  • 在文件末尾添加-Dfile.encoding=UTF-8
  • 重启 AndroidStudio,问题解决

如何使插件支持 AS

Android Studio Plugin Development

Android Studio plugins extend 扩展 or add functionality to the Android Studio IDE. Plugins can be written in Kotlin or Java, or a mix of both, and are created using IntelliJ IDEA and the IntelliJ Platform. It’s also helpful to be familiar 熟悉 with Java Swing. Once completed, plugins can be packaged and distributed at JetBrains Plugin Repository.

build.gradle

Configuring the Plugin build.gradle File

intellij {
    version '201.8743.12'
    type 'IC'
    plugins 'android'
}

For API compatibility 兼容, it is essential 必须 to match the version of the IntelliJ Platform APIs used for plugin development with the target version of Android Studio. The version number of Android Studio contains the version of the underlying 基础 IntelliJ Platform APIs that were used to build it.

To find the version of the IntelliJ Platform used to build Android Studio, use the Android Studio About dialog screen. An example is shown below. In this case, the version of the IntelliJ Platform is 201.8743.12.

plugin.xml

Configuring the Plugin plugin.xml File

When using APIs from the android plugin, declare a dependency:

<idea-plugin>
	<depends>com.intellij.modules.platform</depends>
	<depends>org.jetbrains.android</depends>
</idea-plugin>

As discussed in the Plugin Dependencies section of this guide, a plugin’s dependency on Modules Specific to Functionality must be declared in plugin.xml. When using Android Studio-specific features (APIs), a dependency on com.intellij.modules.androidstudio must be declared as shown in the code snippet below. Otherwise, if only general IntelliJ Platform features (APIs) are used, then a dependency on com.intellij.modules.platform must be declared as discussed in Plugin Compatibility with IntelliJ Platform Products.

<depends>com.intellij.modules.androidstudio</depends>

2020-11-30

原文地址:https://www.cnblogs.com/baiqiantao/p/14059062.html