Reprint please indicate the source: blog.csdn.net/zhaoyanjun6… This article is from [Zhao Yanjun’s blog]

A: an overview

I believe that when you use Android Studio, you will more or less use some plug-ins, the appropriate combination of plug-ins can help us improve the development efficiency, more happy. Such as:

Github.com/zzz40500/Gs… Can help us transform from Gson to entity class github.com/avast/andro… Butterknife github.com/Skykai521/E… Can help complete English -> Chinese translation in IDE

See Android Studio’s Common Plugin collection for more useful plugins

There is a saying that it is better to teach people to fish than to teach people to fish. It is necessary to learn to write plug-ins instead of following the footsteps of others. When you have a good idea, you can realize it by yourself. Today’s lesson: Write your own Android Studio plug-in.

Two: preparation

We need to do some preparatory work before we can actually start construction, otherwise the rest of the content will be difficult to start.

1. Install JDK and configure environment variables. Installation process here is not detailed, direct search can be.

2, You need to understand the relevant knowledge of Groovy, including groovy environment construction, Groovy development tools to understand, Groovy syntax to understand, these are not detailed here, specific knowledge points can refer to my blog groovy using full parsing

3: Android Studio Plugin development tools

The development tool we choose to use IntelliJ IDEA

Download it at www.jetbrains.com/idea/

IntelliJ IDEA has two versions: Ultimate and Community. Ultimate is the paid version and Community is the free version. For those who can afford the paid version, we can consider it, but for our plug-in development, we can use the free Community version.

The installation process of IntelliJ IDEA is not detailed here.

4. Create a Plugin Project

In IntelliJ IDEA of open select File | New | Project

Groovy let’s leave it unchecked for now, click Next

Finally, enter the project name and click Finish

Configure the plug-in project

In the created Plugin project, a plugin.xml configuration file will be generated by default, in which relevant configuration information of the plug-in project can be modified, such as plug-in name and plug-in version number, as shown in the following figure

The meanings of each label are as follows:

  • [id] Plug-in ID, similar to the package name of the Android project, cannot be the same as other plug-in projects, so it is recommended to use the com.xxx.xxx format

  • [name] The name of the plugin that others use to search for your plugin in the official plugin library

  • [version] Plug-in version number

  • [vendor] Information about the publisher of the plugin. Mailbox links can be added

  • [description] This is the description of your plugin. It supports HTML tags

  • [change-Notes] Plug-in version change log, support HTML tags

  • [idea-version] Support for IntelliJ IDEA Software. The plugin version is divided into since-build lowest version and until-build highest version. Can choose one or two attributes used at the same time Website has a detailed introduction to www.jetbrains.org/intellij/sd… The general rule is since-build <= support version < until-build

  • [Extensions] User-defined extensions that are not needed at the moment

  • [Actions] The specific plug-in actions are described later

, configuration information filled in this questionnaire will be show in other people search for your plug-in is introduced, in the File | Settings | Plugins view existing plug-ins, or click Browse respositories Browse released by others in the center the plugin. When a plug-in is selected, you can see the configuration information in plugin.xml on the right side, and you can also delete and install plug-ins. Each operation takes effect only after the software is restarted.

6. Start writing plug-ins

Project creation good have a SRC folder, in which the File | New… To create a new file, there are several special file types for plug-ins

  • GUI Form: Form interface
  • Dialog: a Dialog box
  • XXXComponent: scope class, which is divided into Application, Project and Module for different scopes
  • Action: Class that handles plug-in interactions

In the project of the SRC directory, New | plugins Devkit | Action

The Action creation dialog looks like this:

  • Action ID: indicates the Action ID. The recommended value is plug-in name. XXAction “format
  • Class Name: The Class Name of the Action to write
  • Name: The Name of the action displayed in the menu options
  • Description: Indicates the action Description
  • Groups: Defines the group to which the action option belongs. For example, EditMenu corresponds to Edit on the IDE menu bar and CodeMenu corresponds to Code on the menu bar
  • Actions: Actions that are currently selected under Groups. For example, if we select CodeMenu, this will show several options that are already in the Code
  • Anchor: Used to specify the position of action options in Groups, Frist being the top, Last being the bottom, or above/below an option
  • Keyboard Shortcuts: Call Action Shortcuts for plugins. Leave them blank and note hotkey conflicts

After an Action is created, a corresponding Action is automatically added to the actions TAB of the plugin.xml configuration file.

  <actions>
    <! -- Add your actions here -->
    <action id="TestID" class="com.Test" text="Test" description="Test">
      <add-to-group group-id="EditMenu" anchor="first"/>
      <keyboard-shortcut keymap="$default" first-keystroke="ctrl Q"/>
    </action>
  </actions>
Copy the code

Pay attention to

  • All of this information can be modified again, but the class name must be modified to match the class name
  • You can add more than one, that is, you can choose to use them in different menus
  • The ids of multiple actions in the same group must be unique.

The Action class created under SRC inherits AnAction by default and implements the actionPerformed method. Add the following code to the method so that an input box pops up when the Action is clicked.

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;

public class Test extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        // TODO: insert action logic here
        Project project = e.getData(PlatformDataKeys.PROJECT);
        String txt = Messages.showInputDialog(project,
                "What is your name?"."Input your name",
                Messages.getQuestionIcon());
        Messages.showMessageDialog(project,
                "Hello, " + txt + ! "" \n I am glad to see you."."Information",
                Messages.getInformationIcon());}}Copy the code

After writing the code, run runs the project, which automatically launches a new IntelliJ IDEA project that contains the plug-in options we just wrote.

Seven, packaging,

Is simpler, directly in the bar at the top of the main menu select Build | Prepare the Plugin Module XXX For Deployment.

Before packaging, fill in the plugin.xml configuration file with relevant information.

After the package is completed, the.jar package will be generated

Similar to android’s packaged APK, this is when you can throw it away

Android Studio test plugin

In the previous chapter, we packaged the plugin.jar plug-in, now let’s test it on Android Studion.

8.1 Installing Plug-ins

File – > Settings – > Plugins – > Install plugin from disk

8.2 Testing Plug-ins

Or press the shortcut key Ctrl + Q to launch the plug-in.

9. Upload plug-ins

There is also a market for IntelliJ plugins, which are officially provided by plugins.jetbrains.com/

Open the Plugin center, register your account, and select Upload Plugin to Upload the jar package you generated, then select the category, and finally confirm that the Upload is complete.

X. Reference materials

[Android Studio Plugin Plugin] juejin.cn/post/684490…

Android Studio plugin Development detail 2: Utility classes


Personal wechat account: ZHAOyanjun125, welcome to follow