The foreword 0.

Now a popular way of app development is component-based development. I once wrote an article Kotlin builds component-based application templates to introduce how to implement component-based development. However, there is a problem that every time a business module is built, the Androidmanifest file and build file need to be modified. Creating various packages feels extremely cumbersome, so I’m going to write a plug-in to automatically generate template modules.

1. The demand

Make plug-in, generate corresponding Module template according to module name and package name

Code 2.

2.1 Downloading Intellij Idea

2.2 Creating a Plugin project

image

2.3 Defining the plugin.xml configuration file

< the idea - the plugin > < id > com. The skateboard. Modulegenerator < / id > < name > modular template module generator < / name > < version > 1.0 < / version > < vendor email="[email protected]" url="http://www.yourcompany.com">YourCompany</vendor> <description><! [CDATA[used to generate template module]]></description> <change-notes><! [CDATA[ Add change notes here.<br> <em>most HTML tags may be used</em> ]]> </change-notes> <! -- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -- > < idea - version since - build = "173.0" / > <! -- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html 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 --> <group id="com.skateboard.modulegenerator" popup="true" text="GenerateModule"> <action id="MultiModuleGenerator" class="com.skateboard.modulegenerator.GeneratorAction" text="ModuleGenerator" description="Use  to generate module"> <add-to-group group-id="FileMenu" anchor="first"/> <keyboard-shortcut keymap="$default" first-keystroke="shift ctrl P"/> </action> </group> </actions> </idea-plugin>Copy the code

The <name> and <description> are the plug-in name and feature description, which will be displayed when you install the plug-in. The < Actions > tag defines some of the actions the plug-in takes. <group> defines the name and style of the menu the plug-in will display, and <add-to-group> defines the GenerateModule menu bar that will be added to the File menu. <actiton> defines the location of the action code.

2.4 Compilation of dialog boxes

When you click on the ModuleGenerator option, a dialog box will appear asking you to enter the module name and package name, written in Java Swing

ublic class ModuleInfoDialog extends JDialog { private DialogCallback dialogCallback; private final static String OK = "OK"; private final static String CANCEL = "CANCEL"; Private final static String TITLE = "Create new Module "; Private final static String MODULE_NAME_HINT = "Please input module name :"; Private final static String PACKAGE_NAME_HINT = ""; Private final static String ERROR_MESSAGE = "Module name and package name cannot be empty "; public ModuleInfoDialog(DialogCallback dialogCallback) { setSize(450, 300); setModal(true); setTitle(TITLE); prepareUI(); this.dialogCallback = dialogCallback; } private void prepareUI() { GridBagLayout gridBagLayout = new GridBagLayout(); JPanel contentPannel = new JPanel(); contentPannel.setLayout(gridBagLayout); GridBagConstraints constraints = new GridBagConstraints(); constraints.gridx = 0; constraints.gridy = 0; constraints.gridwidth = 1; constraints.weightx = 0; JLabel moduleHintLabel = new JLabel(); moduleHintLabel.setText(MODULE_NAME_HINT); contentPannel.add(moduleHintLabel, constraints); constraints.gridx = 0; constraints.gridy = 1; constraints.weightx=0; JLabel packageNameHintLabel = new JLabel(); packageNameHintLabel.setText(PACKAGE_NAME_HINT); contentPannel.add(packageNameHintLabel, constraints); constraints.gridx = 1; constraints.gridy = 0; constraints.weightx=1; constraints.fill=GridBagConstraints.HORIZONTAL; JTextField moduleNameImp = new JTextField(); moduleNameImp.setForeground(Color.WHITE); contentPannel.add(moduleNameImp, constraints); constraints.gridx = 1; constraints.gridy = 1; constraints.weightx=1; constraints.fill=GridBagConstraints.HORIZONTAL; JTextField packageNameImp = new JTextField(); packageNameImp.setForeground(Color.WHITE); contentPannel.add(packageNameImp, constraints); JLabel errorLabel = new JLabel(); errorLabel.setForeground(Color.RED); errorLabel.setVisible(false); JButton okBtn = new JButton(); okBtn.setText(OK); okBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (moduleNameImp.getText().isEmpty() || packageNameImp.getText().isEmpty()) { errorLabel.setVisible(true); errorLabel.setText(ERROR_MESSAGE); return; } if (dialogCallback ! = null) { dialogCallback.onOkClicked(moduleNameImp.getText(), packageNameImp.getText()); } dispose(); }}); JButton cancelBtn = new JButton(); cancelBtn.setText(CANCEL); cancelBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (dialogCallback ! = null) { dialogCallback.onCancelClicked(); } dispose(); }}); constraints.anchor=WEST; constraints.weightx=0; constraints.gridx = 0; constraints.gridy = 2; contentPannel.add(okBtn, constraints); constraints.fill=GridBagConstraints.NONE; constraints.gridy = 2; constraints.gridx = 1; contentPannel.add(cancelBtn, constraints); constraints.gridy = 3; constraints.gridx = 0; contentPannel.add(errorLabel, constraints); setContentPane(contentPannel); } interface DialogCallback { void onOkClicked(String moduleName, String packageName); void onCancelClicked(); } public static void pop(DialogCallback callback) { ModuleInfoDialog infoDialog = new ModuleInfoDialog(callback); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); infoDialog.setLocation(screenSize.width / 2 - infoDialog.getWidth() / 2, screenSize.height / 2 - infoDialog.getHeight() / 2); infoDialog.setVisible(true); }}Copy the code

Nothing special, just build a dilaog in which the static method pop () is used to display the dialog

2.5 the preparation of the action

When we click on this option for ModuleGenerator, an action will be generated and this is where we will implement the corresponding logic, which is where we will create the template file. Create a GeneratorAction class that inherits from AnAction and needs to override the actionPerformed method. Look at GeneratorAction’s actionPerformed method

ModuleInfoDialog.pop(new ModuleInfoDialog.DialogCallback() {
            @Override
            public void onOkClicked(String moduleName, String packageName) {
                generateModuleFiles(moduleName, packageName, e.getProject().getBasePath());
                e.getProject().getBaseDir().refresh(true, true);

            }

            @Override
            public void onCancelClicked() {

            }
        });Copy the code

The first step is to create the dialog for entering module and package names. When the OK button is clicked, the project template file and the synchronization project file will be created. GenerateModuleFils method is to create the corresponding folder and file, the specific implementation of the code is good, nothing to say.

Problem 3.

The version number of Intellij specified in the Gradle file

Intellij {version '2018.1'}Copy the code

The build version of the idea specified in plugin. XML

The idea - version since - build = "181.0" / >Copy the code

Plugin *** is incompatible with this installation unless compatible with the Android Studio version

4. Project address

Github Gradle runs the runIde task

image

Follow my official account