当前位置:首页 > 未命名 > 正文内容

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

u3blog8年前 (2016-06-26)未命名258

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.
import com.intellij.openapi.actionSystem.AnAction;
import com.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: 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)

扫描二维码推送至手机访问。

版权声明:本文由u3blog发布,如需转载请注明出处。

本文链接:https://u3blog.xyz/?id=475

分享给朋友:

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。