Android Studio plug-in development practices-from creation to release

Source: Internet
Author: User
Tags cdata

Objective

A few days ago on GitHub found a pretty good Android studio plug-in ectranslation, in some source code comments encountered in the non-recognized English words can be very convenient to view Chinese translation. At that time with Curiosity also want to try to develop a small plug-in, on-line search data found that plug-in development information is very few, most of the blog is simply to build a development environment and then pop up a Hello World dialog box is finished, JetBrains also provided only one copy of the Devguide and no more detailed API documentation. Therefore encountered most can only chew its English instruction manual and reference others posted in GitHub plug-in source. Now this small plug-in completed almost, want to put their own development process for the past few days to share, hoping to give other interested in trying to plugin development friends a little help.

Clear requirements

The first thing to consider when developing a plugin is of course what it is going to achieve. For example, I want to do is Varname-go-die the main function is to let developers sometimes encounter the variable name but do not know how to spell the English, do not need to switch to the translation software to find and copy it, just to enter the Chinese language in the editor can achieve network translation, And you can choose your own set of common variable format through a list.

This is what I consider to be implemented:
1. In the Android Studio settings interface has varnamegodie settings options, developers can according to their own name of the variable naming style to set
2. In the editor input and select the Chinese that you want to convert, the shortcut key launches a Changevar action, network find translation and pop-up settings in the variable name format list, select replace the editor in Chinese
3. Input English words in the editor can also be converted to format

For specific effects see:

Environment preparation and project creation

Android Studio is based on IntelliJ idea, found on the web as if it could be developed in IntelliJ idea, and new project in Android Studio has no plugin option.
1. Download IntelliJ idea,community is free of charge
2. Create the plugin project, File->new project, and follow the actions, note: For the project SDK, if you do not need to create one, click New, and then point to IntelliJ Idea's installation directory is OK.

After the successful establishment of the project structure is as follows:

The configuration documentation for the project, which is equivalent to the plugin.xml Android Project AndroidManifest.xml , is responsible for some action, extension, and so on, the project version information, the author's registration, and the declaration.
Default plugin.xml file:

<idea-plugin version="2">  <ID>Com.your.company.unique.plugin.id</ID><!--plugin ID, custom, if you want to upload to plugins warehouse cannot have duplicate ID--  <name>Plugin Display Name Here</name><!--plug-in name--  <version>1.0</version>  <vendor Email="[email protected]" url="/http/ Www.yourcompany.com ">YourCompany</Vendor><!--email and URL, upload to plugins warehouse will be displayed in your plugin interface--  <!--Your plugin's profile, which is also displayed in the Plugins warehouse information interface --  <description><!    [cdata[Enter Short description for your plugin here.<br> <em>most HTML tags could be used</em> ]]></Description>  <!--version update information -  <change-notes><! [cdata[Add Change notes here.<br> <em>most HTML tags could be used</em>]]>  </change-notes>  <!--See http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html For description -  <idea-version since-build="141.0"/><!--product selection, which will be mentioned later  <!--See Http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_ Compatibility.html on "to" target different products--  <!--uncomment to enable plugin in all products <depends>com.intellij.modules.lang</depends>-->< /c7><!--extension component registration If you use Applicationconfigurable, the project configuration, and so on, register here --  <extensions defaultextensionns="Com.intellij">    <!--Add your extensions here--  </Extensions><!--action registration, such as adding a button under a menu to register here--  <actions>    <!--Add your actions here-  </Actions></idea-plugin>
Writing a menu option

Click on the menu bar will come out a variety of menu options, most of the options will have shortcut key support, such as our usual undo, copy and so on.

Creating one of the menu options shown is simple:

Then fill in the configuration information for this action:


Action ID: Identity ID, just like the component of XML in Android @+id
class Name: Generated classes name
Name , Description: Names and descriptions of menu options
Groups: Define where this menu option appears, for example, when you click on the menu bar edit, the first item will appear with the test option, and the anchor on the right is the location where the option appears, and defaults to the top.

When you click OK, a class is automatically generated TestAction.java :

publicclass TestAction extends AnAction {    @Override    publicvoidactionPerformed(AnActionEvent e) {        // TODO: insert action logic here        //点击菜单Edit的test后会跳进这个方法    }}

And plugin.xml there is a bit more code, that is, the configuration information just filled out:

<actions>  <!--Add your actions here-  <action id="Test.id" class="Testaction" text=  "Test" description="test test">    <add-to-group group-id="MainMenu" anchor="First"/ >    <keyboard-shortcut keymap="$default" first-keystroke="alt T"/ >  </Action></Actions>

In this way, a menu option is completed, and the next step is to implement the function code when the user taps the test menu or presses the shortcut key alt T .

dialog Box dialog Create

As with action creation, Dialog can also right-->new->dialog directly in SRC or under the package name. Fill in the class name will generate a Xxx.java and Xxx.form file, Xxx.java is an inherited JDialog class, understand a bit of Java swing programming students can understand, and xxx.form is IntelliJ idea comes with the GUI Designer, you can easily create user interface layouts with a visual interface design.


Just by dragging different components from the right to the corresponding location in the middle layout, and then setting the appropriate properties in the lower left corner, these properties can be automatically bind to the corresponding component in the Xxx.java file. This simplifies the tedious operation of writing interface layouts for developers, and it's easy to implement your own interface even if you don't know much about swing programming.

When you design the dialog interface and implement the data loading and monitoring of the buttons or other events inside, when you want to show it, you just need two simple lines of code:

new TestDialog();dialog.setVisible(true);    
Write a configurable function

When your plugin needs or allows users to customize some configuration, such as my plugin allows users to define the style of code they want to generate, just open Settings->other settings will see a configuration interface:


The first need to implement this function, looked for a long time, in the view of some of the more fire plug-ins found Butterknifezelezny project also has this function, so go to GitHub to find the project source code and imitate the realization of this function. So when it comes to some feature implementations that don't find good information, check out some other authors ' projects to see if you can find a similar learning experience.

Implementing a configuration interface requires you to implement the Setup interface and implement the configurable interface. The implementation interface like the creation of dialog, New->gui form will also generate a Java file and a form file, the same design interface, and then implement the configurable interface in the Java file, need Override some methods:


getDisplayName(): The configuration name shown under Other settings
getHelpTopic(): Look at the information displayed when the method name is for help, which is useless to
createComponent(): component creation and initial data configuration
apply(): When the configuration interface clicks on the Apply button below the method is called, it is usually here to save the modified data
reset(): Configuration Interface Click Reset in the upper right corner to call this method, general restore initialization data

When you design the interface, sometimes you need to customize some components, such as the need to add Jcheckbox in JList, and so on, directly in the form of Jcheckbox dragged into the jlist it seems to be impossible, The custom create item needs to be checked in the Property-value configuration bar of the corresponding component in the lower right corner of the form interface, the method is generated in the Java file and createUIComponents then created and added in this method.

Once the interface is designed and implemented in a Java file, the plugin.xml configuration interface can be implemented after registering:

<extensions defaultExtensionNs="com.intellij">    <applicationConfigurable instance="com.royll.varnamegodie.settings.SettingsUI"/></extensions>

At this point, the basic interface design is almost complete, the following talk about my development projects encountered some of the specific functional issues.

The editor gets the user selection and replaces

Varname-go-die first need to get the user to select the English/Chinese phrases to convert, how to get the user at this time to select the content?

Editor mEditor = e.getData(PlatformDataKeys.EDITOR);String selectText = mEditor.getSelectionModel().getSelectedText();

So the user selects the content at this time to obtain, then how to replace it with other content?

Private void Changeselecttext(String text)    {Project Mproject = E.getdata (Platformdatakeys.project);    Document document = Meditor.getdocument (); Selectionmodel Selectionmodel = Meditor.getselectionmodel ();Final intStart = Selectionmodel.getselectionstart ();Final intEnd = Selectionmodel.getselectionend (); Runnable Runnable =NewRunnable () {@Override         Public void Run() {document.replacestring (Start, end, text);    }    };    Writecommandaction.runwritecommandaction (Mproject, runnable); Selectionmodel.removeselection ();}

With the above code, you can successfully replace the user selected content.

Settings configuration information Save

When the user sets a custom configuration in settings, you need to save it and read it when it is applied.

PropertiesComponent.getInstance().setValue() //保存基本类型及String等PropertiesComponent.getInstance().setValues() //可保存数字

The way to get parameters is similar to that of Android developers, who can easily think of similar sharedpreferences in Android.

Run and debug plugins


Running the plugin by clicking Run will re-open a IntelliJ idea, basic setup and create test project after the plug-in project effect is reflected in the new project.

Plug-in package release, upload plugins warehouse

After the plug-in code implementation and debugging success, if you want to open source for more small partners can use, you just need to package their own project into a jar, and then sent to the need, the other side in the Settings->plugins interface through the install plugin from Disk and then find the jar file installed locally to be ready to use. But this is too much trouble, you want the small partner directly through the browse repositories in the warehouse can find their own development plug-in, then you need to upload their own jar to the corresponding IDE plugins warehouse and wait for approval.
Package : Right-click project name->prepare Plugin Module ' xxxx ' for Deployment, and later build jar package under Project
Post :
1. Plugin published to the official warehouse address
2. Remember the plugin.xml code in the note:

  <!-- 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>  -->

This is the product type that specifies that your plug-in publishes to the JetBrains Plugins warehouse, jetbrains company has many kinds of products, and all support plug-in development, such as IntelliJ Idea,android Studio and so on, if you comment on the above code, Then you upload on the above site will be uploaded by default to IntelliJ idea's product warehouse, then only in the IntelliJ idea of the warehouse search for your plugin, Android Studio is not. For detailed configuration instructions, refer to the Web site shown in the comments above to view the configuration. My plugin will be opened by default <depends>com.intellij.modules.lang</depends> , which is uploaded to the product repository by default, and can be found in multiple IDE plugin repositories.
3. After modifying plugin.xml and generating the jar, go to the official online registration user in step 1, then add New Plugin, fill in the information of the plug-in, the remaining as long as waiting for 1 days or so of the audit, you can in the plug-in warehouse query to their own plug-in and installation use!

Thanks
    • Ectranslation
    • Android-butterknife-zelezny
    • IntelliJ Platform SDK Devguide
Written in the last

Spent a few days a variety of data review various toss finally to want to realize the function, the feeling is pretty good, but also I share this development experience, I hope to give other developers want to try to bring a little help, but also hope that more have a wonderful idea of friends to us to share more can let us more "lazy "Plug-in.
At the same time my Code also open source on GitHub-varname-go-die, welcome everyone star&fork&follow! : )

Android Studio plug-in development practices-from creation to release

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.