Androidannotations is an open source framework designed to accelerate the efficiency of Android development. By using its open annotations API, you can almost use it in no matter where, greatly reducing the amount of insignificant code, so that developers can go outside, have enough time to focus on the real business logic. And by simplifying your code, it also improves the stability of the code and later maintenance costs. below androidannotations abbreviation for AA
There may be objections to the performance of our mobile device, which does not have enough memory and computing power than the backend server. When a lot of use of annotations, will not be the impact of the app, will not affect the performance of the app? The first clear statement here is that AA will not bring any side effects to the app, but its powerful and easy-to-use API will give you an unprecedented programming experience.
At present, the mainstream annotation framework has xutils, Butterknife, Dragger and Roboguice, and their implementation principles are all consistent, and are realized by reflection mechanism. By reflecting the annotated field and method in the class during runtime, the corresponding logical code of the annotation is then run. We all know that the reflection mechanism is running in the running period of the app, which will cause the inefficiency of running and the disadvantage of the running time getting longer. When you use a lot of reflection-based annotations in our app, it can seriously affect performance. But the logic of the implementation of AA is not based on this.
The principle of AA work is in fact also very easy, it uses the Java Annotation Processing Tool introduced by JDK 1.6,
Adds an extra layer of self-initiated compilation steps to the compiler to generate code based on your source code. The generated code is a direct subclass of your source code, and the name of the class you are actively generating is the parent class name followed by an underscore. For example, using the myactivityof @eactivity annotations, AA will voluntarily help you generate a class named Myactivity_ . Annotations that use AA generate the corresponding subclass on their own initiative during compilation, and the execution is actually the subclass, so the use of AA does not adversely affect the execution performance of the app.
AA Development Environment Construction: Right-click =>properties=>java Compiler = Annotation processing = Factory Path.
Here's a demonstration sample to get a quick look at this powerful framework that focuses on some frequently used annotations.
I. Annotations to Components
@EActivity This annotation is used to decorate the activity, to inject the layout into the activity, but also to set the page style to full screen, without title. It is not very convenient to use these annotations with notional. Support for other components is also fairly straightforward, such as @eservice, @EReceiver, @EProvider, @EApplication, @EApplication, @EFragment. You can also modify your own definition control at the same time, annotated as @eview, @EViewGroup. Support is not quite comprehensive.
Ii. Annotations to resource references
With a AA, a variety of annoying findviewbyid from now on, you can simply use @viewbyid to bind the controls inside the layout, assuming that your variable name and the control's ID value are consistent, even the point of the ID can be omitted. And without the ID in the annotations, assuming the compiler cannot find the corresponding variable ID name in the r file, the compiler will give you a hint, very friendly.
At the same time, if you want to reference a resource in a member variable, you can simply add the corresponding annotation modifier to the variable, and the same assumption variable name and resource ID will be saved when the ID is identical. Support all the resource files, all of them.
Suppose you want to get a system service, just add @systemservice annotations before your variables.
Get the value passed in the intent, plus the @extra annotation, the same time the fault tolerance is very good, assuming that the key corresponding to the value, no problem, you can set the default value. Then there is a strong turn failure will not cause crash, for example, the transfer is an int value, received when it is a string, there is no problem, just receive failure.
Very powerful. The annotations that modify member variables are mainly used to solve their initialization problems, so that the declaration is initialized and the function that can be used is taken. There are also a lot of attributes that are not introduced.
Iii. Event Binding annotations
AA supports all basic native event bindings, and the demo sample shows three common types. A simple event annotation plus a listener's control ID will be able to complete the complex event bindings that have been done, inner class implementations and so on. And the method name can be arbitrarily customized, assuming that the method name is consistent with the ID of the control, the ID in the annotations can be omitted, the same assumption does not match, the compiler is compiled just. The list of the methods can also be defined by themselves, when the need for a number of parameters, the native monitoring method of the list of the table can be directly used. Other frequently used @textchange, @ItemClick, @SeekBarProgressChange.
Iv. interaction of asynchronous threads with the UI thread
When the view-related member variable is initialized, the method that owns the @afterviews annotation is called, and you can initialize some interface controls inside. Assuming that other member variables are done, the @afterinject is called.
For example, the logic of most applications is this, after initializing the interface, it initiates time-consuming data requests, then parses the acquired data and then sets it to the interface. When it comes to interacting with the asynchronous task, the UI thread is relatively cumbersome. Let's see how AA is implemented.
Very easy bar, the UI thread to run the method of adding a @uithread, asynchronous threading method add a @background, the interaction of the two is the method of direct mutual invocation, the other you do not care, all the implementation is a AA compiler to self-generated interactive code. The process of interaction, completely without the feeling of running asynchronously, no longer using handler to send a message. Two comments on the once a bunch of code implementation of the function to achieve, really give the greatest praise.
Five, Rest API
The rest API is also supported in AA, and all HTTP request methods are supported. The following demo is a GET request.
Define a request interface and then be able to use it directly, without having to implement it yourself. This is closely related to the background interface design of the company, assuming that your company interface interaction is a restful style, you can rewrite it and enjoy the magic of AA.
written in the last : Androidannotations is a powerful, well-deserved king in the annotation framework, and the flexible API greatly improves the efficiency of development and reduces the cost of maintenance. Assuming that it has any drawbacks, I can only say no, it has no drawbacks. If the hard to come to a word, but also only can be its annotations more, familiar with the need for a period of time, assuming the entire development team technology migration, the upfront technical cost is slightly higher. But the so-called firewood does not mistake sharpening, or can not be attributed to a disadvantage. AA also has a lot of powerful useful functions, confined to the space is not expanded to say, to explore it yourself.
Well, that's all for today's dry goods.
AA Address: Https://github.com/excilys/androidannotations
If you think it's helpful, you're welcome to subscribe to my public account-Android dry Sharing (Id:android_share). The following is the QR code, to provide you with timely high-quality Android dry. Technical Exchange QQ Group: 318588906, welcome everyone to add a group, together to explore the next Android and Java technology, together to expand our dry sharing community.
Annotation Frame---androidannotations