AndroidStudio Plugin Dev——RemoveButterKnife design to implement

Datetime:2016-08-22 23:19:26          Topic: Android Studio           Share

What AndroidStudio Plugin is?

I think you have interested in Android Dev if you read this blog, so, AndroidStudio is our most use IDE.

What AndroidStudio Plugin is?

AndroidStudio is a Google recommend IDE for Android develop, but we also need some other function when use it, we can use plugin to add the function, such as make your AndroidStudio support vim by install a vim plugin.

We can find a magnifier in AndroidStudio’s top right, click, and input plugin, click the result, you can search and install plugin in the pop window.

How to use AndroidStudio plugin

Now, we had install the plugin, how to use it?

Simple, just follow the introduction of the plugin, such as IdeaVim, it’s active after you installed it, just input in your AS with vim way!

RemoveButterKnife’s porpuse

You may hear about ButterKnife, use it and the plugin named android butterknife zelezny, you can gen code by a few click.

But some times, we may want to remvo it from our code, it’s very boring task to delete annotation and write findviewbyid code.

So, why there can’t be a plugin to help us do this boring task?

Tools need to dev a plugin

1.An idea, because AndroidStudio is dev base on idea.

2.JDK

Plugin dev 101

Now we can start coding!

At first new a plugin project in your Idea.

Now right click the src file, new a Action file.

Fill some info about your plugin

Now, we have this code.

importcom.intellij.openapi.actionSystem.AnAction;
importcom.intellij.openapi.actionSystem.AnActionEvent;
 
public class test extends AnAction {
 
    @Override
    public void actionPerformed(AnActionEvent e) {
        // TODO: insert action logic here
    }
}

actionPerformed will invoked every time when the plugin was triggered.

Now we just finish our first plugin!

RemoveButterKnife function

Here is the RemoveButterKnife’s mainly function

1.Use to Activity/Fragment witch use ButterKnife to inject view.

2.Delete import code of ButterKnife

3.Delete butterknife annotation(@bind(R.id.xyz))

4.Gen findviewbyid code

5.Delete ButterKnife.inject(this) and some thing like this

Now, there is a sample of above function

before:

after:

The implement detail

Now we clear our mind and write down the implement details.

1.Record import codes line number

2.Use Regular Expressions to find annotation code record line number and gen a map with name and id.

3.Find and record ButterKnife method invode code line number.

4.Delete the code on recorded number.

5.Find oncreate/oncreateview method, gen findviewbyid under the layout set code use the map with name and id we have create at step2

Code

To implement the function we have talk above, we use three class

1.MainAction ———— To find and record line numbers, gen map with id and name.

2.DeleteAction ———— Delete code on recorded line number

3.FindViewByIdWriter ———— Gen findviewby id code

You can find the whole code on below GitHub link

RemoveButterKnife

In the code, i use some idea sdk api, you can search it on google to find out what it means.

Publish your plugin to repositories

**You can publish your plugin to repositories by visit This Page and register an account, then click add new plugin. **

If you think my blog is make your life better, how about buy me a cup of coffee?:)(1USD)





About List