AndroidStudio Plugin Dev——RemoveButterKnife design to implement[pw1]

ReomveButterKnife Plugin

This is an Android Studio plugin to help remove use of ButterKnife, we will talk about how to dev it.
GitHub addressRemoveButterKnife
QQ截图20160624161742

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.
step1
step2

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?

1

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.
plugin

Now right click the src file, new a Action file.
QQ截图20160624145412

Fill some info about your plugin
QQ截图20160624145701

Now, we have this code.

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:
QQ截图20160624154939

after:
QQ截图20160624155124

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
QQ截图20160624161742

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)





u3coding
A software developer

2 Comments

  1. Hello,

    Nice article, good point of focus for starter. I have a doubt here, what will be the exact output format of our plugin?

    I think, .jar file will be executable file, which needs to add into Android studio, as our custom plugin, is it right.

    Thanks,
    Sumeet…

    1. Yes, the output is a .jar file, you could install it to the Android Studio by choose install local plugin on the plugin install page.

      And you can also upload it to the repository of jetbrain(the idea soft maker) as i mentioned on the end of article

Leave a Comment

Your email address will not be published. Required fields are marked *

*