Android Studio插件开发入门教程

android studio 相关说明 | 2019-03-21 23:06

插件只能使用IDEA开发,IDEA下载地址:打开IDEA,选择 File → Project Structure ,在弹出窗口中选择 New → IntelliJ Platform Plugin SDK ,然后点击 OK即可

点击 File → New → Project → IntelliJ Platform Plugin → Next → 输入项目名 → Finish建好之后会生成一个默认的 plugin.xml 文件,默认代码如下:

<idea-plugin>

<id>com.your.company.unique.plugin.id</id>

<name>Plugin display name here</name>

<version>1.0</version>

<vendor email="" url="">YourCompany</vendor>

<description><![CDATA[

Enter short description for your plugin here.<br>

<em>most HTML tags may be used</em>

]]></description>

<change-notes><![CDATA[

Add change notes here.<br>

<em>most HTML tags may be used</em>

</change-notes>

<!-- please see for description -->

<idea-version since-build="173.0"/>

on how to target different products -->

<!-- uncomment to enable plugin in all products

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

<extensions defaultExtensionNs="com.intellij">

<!-- Add your extensions here -->

</extensions>

<actions>

<!-- Add your actions here -->

</actions>

</idea-plugin>

修改配置示例如下,各字段的含义见注释:

<idea-plugin>

<id>com.your.company.unique.plugin.id</id>

<!--插件名-->

<name>MyFirstDemo</name>

<version>1.0</version>

<!--开发者信息:邮箱、网站、名称-->

<vendor email="" url="">Cassin</vendor>

<!--插件描述,可以使用html语法-->

<description><![CDATA[

Study IDEA Plugin<br>

It's just for fun. But it has to ask for <em>40</em> characters.

]]></description>

<!--版本变更日志-->

<change-notes><![CDATA[

First Version<br>

It's just for fun. But it has to ask for <em>40</em> characters.

</change-notes>

<!-- please see for description -->

<idea-version since-build="173.0"/>

on how to target different products -->

<!-- uncomment to enable plugin in all products

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

<extensions defaultExtensionNs="com.intellij">

<!-- Add your extensions here -->

</extensions>

<actions>

<!-- Add your actions here -->

</actions>

</idea-plugin>

注意:必须将类全部放在文件夹下,不能直接放在src目录中,否则打包后运行会报这个错:

java.lang.NullPointerException

at com.android.tools.idea.diagnostics.AndroidStudioSystemHealthMonitor.getActionName(AndroidStudioSystemHealthMonitor.java:744)

at com.android.tools.idea.diagnostics.AndroidStudioSystemHealthMonitor.countActionInvocation(AndroidStudioSystemHealthMonitor.java:630)

at com.android.tools.idea.diagnostics.AndroidStudioSystemHealthMonitor$3.countActionInvocation(AndroidStudioSystemHealthMonitor.java:353)

at com.intellij.ide.AndroidStudioSystemHealthMonitorAdapter.countActionInvocation(AndroidStudioSystemHealthMonitorAdapter.java:29)

at com.intellij.openapi.actionSystem.impl.ActionManagerImpl.fireBeforeActionPerformed(ActionManagerImpl.java:1196)

at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter.lambda$actionPerformed$0(ActionMenuItem.java:302)

在src下新建一个文件夹,例如com.study.demo。在文件夹下新建一个Action:将光标定位到文件夹上 → File → New → Plugin DevKit → Action输入Action信息,例如:需要注意的是,Groups代表该插件安装后显示在哪里。我们选择CodeMenu,代表Code菜单栏下,Anchor表示在Code菜单栏下的第一个位置还是最后一个位置,我们选择Last。 点击 OK 后会自动生成这个我们自定义的MainAction类,同时在plugin.xml中会自动生成以下\标签:

<actions>

<action id="MainAction.ID" class="com.study.demo.MainAction" text="MyPlugin"

description="This is a demo for study IDEA Plugin.">

<add-to-group group-id="CodeMenu" anchor="last" />

</action>

</actions>

当然,我们也可以不使用 Plugin DevKit 快捷方式,改为自己手动创建Action类,并添加类似的\标签,效果一样。

修改MainAction代码如下:

public class MainAction extends AnAction {

public void actionPerformed(AnActionEvent e) {

Project project = e.getData(PlatformDataKeys.PROJECT);

String text = Messages.showInputDialog(project,

"What do you want to listen?",

"I will say it to you",

Messages.getQuestionIcon());

Messages.showMessageDialog(project,

"Greeting",

Messages.getInformationIcon());

一个简单的输入弹窗,和一个消息提示弹窗。

点击 Build → Prepare Plugin Module 'IDEAPluginDemo' For Deployment打包完成后生成的jar文件在这里:同时可以看到Log控制台输出相应的日志。

打开Android Studio,点击Android Studio → Preferences → Plugins → Install plugin from disk... → 选择刚刚生成的jar包 → Apply → OK → 重启Android Studio

这样就完成了文章开头的效果图。

官方文档: Studio Plugin 插件开发教程(一) —— 开发你的第一个插件:https://juejin.im/post/599f7f546fb9a0248c2de496