Brief introduction
Butterknife is an Android view injection framework that allows you to bind the view's properties or methods in a "note" manner.
Using it, for example, can reduce the writing of Findviewbyid (), making the code more concise and without consuming additional performance.
Of course, this also has a drawback, is that readability will be poor, fortunately butterknife is relatively simple, learning difficulty is not very small.
Add dependency
Here's an example of Android Studio Gradle, adding butterknife to the project, noting that the two steps are complete:
1. Project's Build.gradle add:
dependencies {classpath ' com.neenbedankt.gradle.plugins:android-apt:1.8 '}
2. Add Build.gradle to the APP:
Applyplugin: ' com.neenbedankt.android-apt ' dependencies {compile ' com.jakewharton:butterknife:8.0.1 ' apt ' com.jakewharton:butterknife-compiler:8.0.1 '}
Here by the way a plug-in called Android Butterknife Zelezny, can be downloaded in the Settings–plugins, provide a key to generate annotation features, here do not repeat, you can see the following articles:
Lazy Plugin for butterknife annotations framework
Use of Butterknife
The use of Butterknife is relatively simple, specific use can refer to the official document or search other blog material, here is only a brief description of its purpose:
- Eliminate Findviewbyid by using @BindView
- Organize multiple View into one list and manipulate them at once
- Eliminate listener anonymous inner classes by using @onClick to listen for View bindings
- Eliminate resource lookups by using resource annotations such as @BindColor
We see, it seems to eliminate a lot of things, in fact, is the meaning of simplification.
How to use Integration
In general, Butterknife has the following uses:
- View Bindings
- Resource binding
- Non-Activity binding
- View List Bindings
- Listener binding
For specific use, refer to the documentation!
Butter Knife
Principle Simple analysis Java Annotation processing
Note processor, called APT in Java5, is not familiar:
1 Dependencies {2 classpath ' com.neenbedankt.gradle.plugins:android-apt:1.8 '3} 4 5// introduction of Butterknifeprocessor, etc. 6 apt ' Com.jakewharton: butterknife-compiler:8.0.1 '7
This is a tool for scanning and parsing Java annotations at compile time, through which we can define annotations ourselves and define parsers to handle them. The principle is to read the Java source code, parse the annotations, and then generate new Java code, and the newly generated code is finally compiled into Java bytecode.
Of course, we're not going to delve into the principle of APT, but to know that this tool is used in Butterknife.
Butterknife process
Here with a chestnut to step-by-step observation, Butterknife workflow.
Take a look at the definition of @BindView annotations before:
@Retention (CLASS) @Target (FIELD) public @Interface bindview { /***/ @IdResint Value ();}
The focus is on @Retention (CLASS), which indicates that the annotation is persisted at compile time, but the JVM ignores it at run time. Therefore, using Butterknife annotations does not consume the performance of the runtime.
Scanning Butterknife Annotations
First we use annotations to declare one of our View:
@BindView (R.ID.TEXT1) TextViewtext1;
So when we compile, the Butterknifeprocessor class's process () method executes, searches for all the Butterknife annotations (@BindView), and then generates a Java class.
Based on annotations, generate Java classes
We found the generated mainactivity$ $ViewBinder file in App\build\generated\source\apt\, which contains a bind method:
Public Unbinderbind (finalfinal T target, Object source) { = Createunbinder (target); Viewview; = Finder.findrequiredview (source, 2131492944, "Field ' Text1 '"); = Finder.castview (view, 2131492944, "Field ' Text1 '"); return Unbinder;
It is more and more like our handwriting Findviewbyid () here.
Dynamic injection
Finally, when we execute Butterknife.bind (this), Butterknife loads the class generated above and then calls its bind method.
- Here first called the Findrequiredview to find the corresponding control r.id.text1, in fact, the equivalent of our Findviewbyid ()
- Next call Castview, equivalent to type conversion, to convert the found View to the TextView type
At this point, we have completed a butterknife workflow.
Summarize
In fact, we found that the principle of butterknife is relatively simple, because its main difficulty lies in the use of APT, which belongs to other aspects of the content, we do not delve into this.
In this framework we see the trade-offs between "reflection" and "Annotation processing", butterknife use the latter to avoid the consumption of additional performance.
Android Butterknife Implementation principle