Annotation framework --- AndroidAnnotations

Source: Internet
Author: User

Annotation framework --- AndroidAnnotations

AndroidAnnotations is an open-source framework designed to accelerate Android development efficiency. By using its open annotation api, you can use it almost anywhere, greatly reducing the amount of irrelevant code and allowing developers to leave it out, have enough time to focus on the real business logic. By keeping your code concise, it also improves code stability and later maintenance costs.The following AndroidAnnotations are abbreviated as AA.

Someone may raise an objection that the performance of our mobile devices is no better than that of backend servers with sufficient memory and computing capabilities. When a large number of annotations are used, will it affect the APP performance and performance? Here, we should clarify that AA will not bring any side effects to the APP. On the contrary, its powerful and easy-to-use api can bring you an unprecedented programming experience.

Currently, mainstream annotation frameworks include xUtils, ButterKnife, Dragger, and Roboguice. Their implementation principles are consistent and are implemented through reflection mechanisms. By de-reflecting fields and methods with annotations in the Runtime class, and then executing the logical code corresponding to the annotation. We all know that the reflection mechanism is executed during the APP runtime, which will lead to a reduction in execution efficiency and a longer execution time. When a large number of reflection-based annotations are used in our APP, the performance will be seriously affected. However, the implementation logic of AA is not based on this.

The working principle of AA is actually very simple. It uses the Java Annotation Processing Tool introduced by jdk 1.6,

An additional automatic compilation step is added to the compiler to generate code based on your source code. The generated code is the direct subclass of your source code, and the automatically generated class name is the name of the parent class followed by an underscore. For example, if the @ EActivity annotation is usedMyActivity, AA will automatically generateMyActivity _. The corresponding subclass has been automatically generated during compilation using the annotation of AA. The runtime Runtime is actually this subclass. Therefore, the usage of AA will not negatively affect the APP's execution performance.

Set up the AA Development Environment: Right-click Properties => Java Compiler => Annotation Processing => Factory Path.

Next we will give an example to briefly understand this powerful framework and mainly introduce some common annotations.

I. Component Annotation

@ EActivity: This annotation is used to modify the Activity and inject layout into the Activity. You can also set the page style to full screen without Title. Is it convenient to use meaningful annotations for implementation. It is also quite simple to support other components, such as @ EService, @ EReceiver, @ EProvider, @ EApplication, @ EApplication, and @ EFragment. The custom control can also be modified, and the annotation is @ EView and @ EViewGroup. Is the support quite comprehensive.

Ii. resource reference Annotation

With AA, various annoying findviewbyids are no longer returned from this moment. You can simply use @ ViewById to bind controls in the layout, if your variable name and Control id are the same, you can skip pointing to the id. In addition, if no id is written in the annotation, the compiler will prompt you if the compiler cannot find the corresponding variable id in the R file, which is very friendly.

If you want to reference resources in a member variable, you only need to add the corresponding annotation to the variable. Similarly, if the variable name is consistent with the resource id, id. All resource files are supported.

If you want to obtain the system service, you only need to add the @ SystemService annotation before your variable.

Get the value passed in Intent, add @ Extra annotation, and have good fault tolerance. If you cannot receive the value corresponding to this key, you can set the default value. In addition, a strong conversion failure will not cause crash. For example, if an int value is passed and a String is received, there is no problem, but the receipt fails.

It is very powerful. The annotation for modifying member variables is mainly used to solve their initialization problems, so that declarations are called initialization and can be used as a function. There are many other attributes, so we will not discuss them one by one.

Iii. Event binding Annotation

AA supports binding all native events. Three common events are shown in the example. With a simple event annotation and a Monitoring Control id, you can bind complex events and implement internal classes. The method name can be customized at will. If the method name is the same as the control id, the id in the annotation can also be omitted. If the method name does not match, the compiler cannot compile it. The parameter list of the method can also be customized. When a parameter is required, you can directly use the parameter list of the native listening method. Other commonly used tools include @ TextChange, @ ItemClick, and @ SeekBarProgressChange.

Iv. Interaction between asynchronous threads and UI threads

After the View-related member variables are initialized, the @ AfterViews annotation method is called. You can initialize some interface controls. If other member variables are completed, @ AfterInject is called.

For example, the logic of most applications is as follows. After the interface is initialized, time-consuming data requests are initiated, the obtained data is parsed, and then set to the interface. Generally, the interaction between the UI thread and the asynchronous task is relatively troublesome. Let's take a look at how AA is implemented.

Simple: Add @ UiThread to the method executed by the UI thread, and add @ Background to the asynchronous thread method. The interaction between the two is that the method calls each other directly, and you don't need to worry about others, all implementations are implemented by the AA compiler to automatically generate interactive code. The interaction process is completely unaware of asynchronous execution, and Handler is no longer used to send and receive messages. The two annotations implement the functions implemented by a bunch of code in the past, and truly give the greatest likes.

5. Rest API

It also supports Rest APIs in AA and all HTTP request methods. The following shows a GET request.

Define a request interface, and then you can use it directly without implementing it yourself. This is closely related to the company's background interface design. If your company's interface interaction is Rest-style, you should rewrite it to better experience the charm of AA.

Written at the end: AndroidAnnotations is powerful and well-deserved king in the annotation framework. Flexible APIs greatly improve development efficiency and reduce maintenance costs. If it has any disadvantages, I can only say NO. It has NO disadvantages. If you want to add one, you can only add more annotations. It may take some time to get familiar with it. If the entire development team's technology is migrated, the technical cost in the early stage will be slightly higher. However, the so-called cutting firewood without mistaken sharpening still cannot be attributed to a drawback. AA also has many powerful and practical functions, so I will not talk about it for a long time. Let's explore it by yourself.

Well, today's dry goods are all here.

 

 

Related Article

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.