Front knowledge

Android Studio is based on Intellij IDEA, so you need to use IDEA to develop your plug-in, which will work with all Jetbrain Intellij products, unless the plug-in uses a platform specific dependency library or is limited to a platform

The functional requirements

Replace logo images of different sizes in each RES/MIPMap under Android project with one click

The warehouse address

GitHub

1. Initialize the project

  1. Open IntelliJ IDEA Ultimate (I used 2021.1.3) and click New Project
  2. Select Gradle type on the left (Intellij Platform Plugin is not recommended), specify JDK version 1.8 on the right, select Java and Intellij Platform Plugin, and click Next

3. Enter the project name first-plugin and click Finish

2. Modify the configuration information

1. Modify the build.gradle file

intellij {
    version = '2020.1' // Because Android Studio 4.1 is based on the IDEA 2020.1 Community version, this version is also specified for debugging
    plugins = ['android']
}

buildSearchableOptions {
    enabled = false
}

patchPluginXml {
    changeNotes = """ Add change notes here.

most HTML tags may be used"
"" sinceBuild = '191' // The range of IDEA versions that the plug-in applies to. This range basically covers the last two or three years of Android Studio untilBuild = '212. *' } Copy the code

2. Modify the plugin. XML file

  • Modify the contents in the

    ,

    , and

    labels as required
  • Under the

    tag add:
<depends>com.intellij.modules.platform</depends>
<depends>com.intellij.modules.xml</depends>// Supports XML file manipulation<depends>org.jetbrains.android</depends>/ / AS relevantCopy the code

3. Create the package structure

Right-click on the/SRC /main/ Java folder and create the com.haivo.plugin package with New package

2. Synchronous engineering

Click Gradle Sync to download idea-IC2020.1 and android dependencies for the first time. It may take 10-20 minutes to complete the synchronization.

3. Create a menu item (Action)

  1. Right-click on the com.haivo.plugin folder and select New > Plugin Devkit > Action

  1. Select ProjectViewPopupMenu to attach this menu to the right menu item. You can also select ToolsMenu to attach it to the Tools options menu at the top

  1. Another way to create an Action is to edit the “Actions” tag in plugin.xml and create a corresponding class. In this way, you can also create a flexible set of Actiongroups for the Action, so that the menu item can be expanded
<actions>
        <! -- Add your actions here -->
        <action id="com.haivo.plugin.importpictureaction" class="ImportPictureAction" text="Import picture"
                description="Batch Import pictures with one click">
            <add-to-group group-id="ProjectViewPopupMenu" anchor="last"/>
        </action>
</actions>
Copy the code

4. Create a Dialog

  1. Right-click on the com.haivo.plugin folder and choose New > Swing UI Designer > Create Dialog Class
  2. Enter the class name, uncheck the three boxes, and click OK

5. Implement the UI

  1. This is where JavaFx comes in, but don’t go too far. We’ll change the parent class of ImportPictureDialog to DialogWrapper, implement the createCenterPanel() method, modify the constructor, and add init(). Otherwise there is no interface!)
public class ImportPictureDialog extends DialogWrapper {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;

    public ImportPictureDialog(AnActionEvent event) {
        super(event.getProject());
        init();
    }

    @Override
    protected @Nullable JComponent createCenterPanel(a) {
        returncontentPane; }}Copy the code
  1. Open the importPictureDialog.form file. Familiar? This is very similar to the Layout Editor in AS,

Select OK button and press Delete to Delete. The same is true with cancel button, because DialogWrapper has these two buttons

  1. Drag JLabel and JTextField from the right component library into the box and modify the text and layout

6. Business implementation & debugging

  1. Modify ImportPictureAction
public class ImportPictureAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        // The code here is executed after the Action is clicked
        newImportPictureDialog(e).show(); }}Copy the code
  1. ▶ Plugin(Run Plugin) button will launch a new IDEA window instance, which will automatically install our Plugin. Select an Android Project to open, and right-click the app/ SRC /res folder in the left Project window. You will find an “Import image” option at the bottom of the menu. Click on the following image, and now our plug-in is almost complete

  1. The specific business code is not listed here. Just look at the source code, which is mainly in the importPictureDialog.java class

7. Plug-in packaging & distribution

  1. Click Gradle on the right, find publishPlugin, double-click to execute, or execute from the command line
.\gradlew publishPlugin
Copy the code
  1. The jar package can be found under build/libs

  1. Drag the JAR package into the Edit area of Android Studio and restart AS. The plug-in is installed