Android Annotation Frame Comparison

Source: Internet
Author: User

Java Annotations (Annotation) are equivalent to a tag, and adding annotations to a program is tantamount to marking a program with tags that can be added to packages, classes, properties, methods, and local variables. You can then write an annotation processor to parse the annotations (called compile-time annotations), or you can use the reflection to get annotations when the program is running (person-run-time annotations).
The endless Findviewbyid, Setonclicklistener, and so on when developing Android programs have been a headache for most developers. Fortunately, there are so-called annotated frameworks that can help developers simplify some of the process. The more popular are butterknife, annotations, Xutils, afinal, Roboguice and so on. Today we will compare these annotated frameworks.

Butterknife Framework Analysis
First look at the Butterknife, from the Jakewharton great God's masterpiece, characterized by simple access, relying on a library is good. There is also a plugin available on Android Studio that automatically generates annotations and class attributes.
The annotations currently supported by Butterknife are: View binding (BIND), resource binding (Bindbool, Bindcolor, Binddimen, binddrawble, Bindint, bindstring), event binding ( OnCheckedChanged, OnClick, Oneditoraction, Onfocuschange, Onitemclick, Onitemlongclick, onitemselected, OnLongClick, Onpagechange, OnTextChanged, OnTouch).

Androidannotations Framework Analysis
Again to analyze the famous annotations framework. The principle of the framework, like Butterknife, is to generate code at compile time, but annotations not generate code for the corresponding class call to the annotated variable, method assignment, but directly generate an inherited annotated class, the class has a variable assignment, The code for the annotation method call. At run time, the annotations generated classes are run directly, not the classes we write

Method call Chain: OnCreate (Bundle saveinstancestate)----> Setcontentview ()----> Onviewchangednotifier_. Notifyviewchanged (), and the Onviewchanagednotifier_.notifyviewchanged () method eventually calls the onviewchanged (Hasviews HasViews) method, In this method, there is a variable assignment, the event method to set the code, pay attention to the name of the auto-generated class, find the law, is that we write the name of the class followed by a ' _ ' symbol, now know why with the annotations framework, Our androidmanifest.xml in the activity of the configuration, the name of the activity to add a ' _ ' symbol. Because the code generated by the androidannotations is actually loaded. It's written here. No, there's no reflection in the annotations frame. This framework is not used for reflection, no initialization, and all work is done at compile time without any speed impact on our program.
What annotations does the annotations support? Since annotations performance with butterknife similar, that function? Translate the features of the official website here.
1, Dependency Injection: Inject views, extras, system services, resources, ...
2. Simplify threading mode: Add a comment on the method to make the method run on the UI thread or the child thread.
3. Event binding: Add a comment on the method to make the method handle those views of that event.
4, REST client: Create a client interface, Androidannotations will generate implementation code, this is about the network.
5, clear: Androidannotations will automatically generate the corresponding subclass at compile time, we can see the corresponding subclass to understand how the program is running.
Xutils Framework Analysis
Xutils frame is the frame we are using now, let's analyze his annotation function. Xutils is used in the same way as Butterknife, which adds a comment on a member variable, a method, and then calls a method (Xutils is the Viewutils.inject () method) to assign a value to a member variable and to set the event method to the view. The difference is that Butterknife is called automatically generated code to assign values, and Xutils is implemented by reflection.

You can see reflection, reflection, and reflection, although now the reflection speed is fast, but still can not be compared with the native code, once the annotation is used more, this initialization speed will be slower. As can be seen from the code processed above, the annotations supported by Xutils currently have UI, resources, events, sharedpreference bindings. Like Xutils, the framework for using reflection to parse annotations is also afinal, Roboguice, and so on.
There are many other annotation frameworks on the market, but original aim, not reflection, is the automatic generation of code. Although the reflection function is powerful, it is not desirable, it will not only slow down the speed will also break the packaging of the program. Personally think that the code generation scheme is better, all the functions are done at compile time, and will not affect the user experience, the only drawback is more difficult to achieve than reflection, but our program is not to leave their difficulties to their own, happy to leave the user!

Finally, summarize the above three kinds of frameworks.

Android Annotation Frame Comparison

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.