📜 ⬆️ ⬇️

How to improve the readability of our Android code or use android-annotations

In a tiny project I decided to try this library and see what happens. I want to tell about this experience in this topic.


Library android-annotations allows you to reduce the amount of code for such actions as:
1. Resource load
2. Search for a view by ID (findViewById method)
3. Linking handlers to buttons
4. Getting services (links to classes of services), background processing and other useful things in the simplest form.

How to install.
')
Download from the link above two jar-a: androidannotations-XXXjar (annotation processor) and the api itself: androidannotations-XXX-api.jar. Next, go to Eclipse in the project properties in the Java Compiler, select Annotation Processors, then put a check in this menu - Enable project specific settings. After that, go to the Factory Path sub-item, put the same checkbox again and click on Add externak JARs to add the path to the annotation processor.
That's not all. After this, you need to go to the Java Build Path item and add androidannotations-XXX-api.jar in the Libraries tab of this JAR. Now everything is set up and ready.

An example of use.

This library requires the following from you: when you use annotations from this library, then your activation in the manifest should end with a "_". I mean, you have SMSSender activations, then you need to add SMSSender_ activations to the manifest. That's it.

Further I will give a piece of an example:
import com.googlecode.androidannotations.annotations.*; import com.googlecode.androidannotations.annotations.res.*; @EActivity(R.layout.main) // setContentView() public class SMSSenderActivity extends Activity { @ViewById(R.id.ok_button) // findViewById() Button buttOk; @StringRes //   R.string.message String message; ... @Click(R.id.ok_button) //     void okClickHandler(){ ... } 


Beautiful isn't it? Note that we do not have the usual onCreate method and everything works fine! But nothing prevents him from adding, like other methods of activation states.

How it works? Take a look at the wiki: “AndroidAnnotations works in a very simple way. It automatically adds an extra compilation step that generates the source code, using the standard Java Annotation Processing Tool. ”Actually, this is why you need to add a prefix to the manifest or when the activation starts. Also note that if you do not specify id, the resource will be loaded by the name of the variable, which is also convenient for readability of the code.

A more complete example with many possibilities is presented here:
code.google.com/p/androidannotations/source/browse/trunk/HelloWorldEclipse/src/com/googlecode/androidannotations/helloworldeclipse/MyActivity.java

It should be noted that the @BeforeViews annotation (as in the code from the link above) I did not find, but there is @BeforeCreate.

Here are some annotations available:
@ColorRes
@StringRes
@AnimationRes
@BooleanRes
@ColorStateListRes
@DimensionRes
@DimensionPixelOffsetRes
@DimensionPixelSizeRes
@DrawableRes
@IntArrayRes
@IntegerRes
@LayoutRes
@MovieRes
@TextRes
@TextArrayRes
@StringArrayRes
Extra
@SystemService
Background
@UiThread
@UiThreadDelayed
@RoboGuice

And many others. Yes, you can share RoboGuice with the library.
Many things this nice library really makes it easier, reducing the amount of routine code, which is constantly repeated.
Many examples and docks can be found on the project homepage.

Conclusion

In conclusion, I want to add an interesting point that I noticed for the week: when you declare a <uses-tag> tag, you need to describe it BEFORE the application tag, otherwise the rights will not be applied to the application. At least I had it so three times, which speaks of the non-randomness of this moment. So, be careful!

As usual, all suggestions and questions we write in a personal.
Who used this library to describe your experience, it will be all good.

Also forgive me to choose which topic will be interesting to read first:
1) Authorization Vkontakte through Android: the publication of entries on the wall.
2) Processes and threads in Android: we write AsyncTask correctly.
3) Putting together a full-fledged application with Scripting Layer for Android (SL4A) code.google.com/p/android-scripting
4) Your option.

Source: https://habr.com/ru/post/123966/


All Articles