Last night saw a good article, and then the English, so decided to share the translation to everyone, this is the original link: http://www.michaelevans.org/blog/2015/07/14/ improving-your-code-with-android-support-annotations/
If you haven't heard of the Android support annotations Library, you'll miss a lightweight and flexible new package that can help you catch bugs. This library contains a lot of Java annotations that will help Android studio to check your code for possible errors, and give you feedback. This library has quite a lot of annotations, and I'm only going to explain a few of them here, but if you need a complete and clear description of the rest of the documentation, you can check out here: Check out the docs.
@NonNull and @Nullable
@NonNull and @Nullable may be the most basic in supporting annotations, but in most cases it is useful. If the return value of a parameter or method may be null, it is represented by a parameter or method of comment. Now Android Studio gives a good warning when we do some unsafe and rigorous coding operations.
For example, this:
This is done after the modification:
What's even better: we can even go a step further using the @checkresult annotation as an example to tell us that the method's return value type is what we expect to use instead of the error effect:
@StringRes and @DrawableRes, etc.
Are you in the TextView to increase the SetText method, often appear inexplicable anomalies java android.content.res.Resources$NotFoundException: String resource ID #0x3039
? If you set an integer to Settext,textview, the integer number is assumed to be the resource ID of a string, and in order to set the string literal it will be queried. An issue with an integer number that is not an invalid resource ID can be remedied by @stringres annotations.
publicvoidsetTextint id) { // Do something like getString(id), etc. }
If you set an invalid string resource ID to the SetText method, it will appear as follows:
The library also provides a corresponding resource annotation for all resource types: [email protected], @ColorRes,@InterpolatorRes
, and so on.
@Keep
A new support note @keep was found today. According to the note document, this annotation is not yet connected to the Gradle plug-in, but it can be preserved by commenting on the application while confusing the app.
This handy note informs the obfuscation to keep the corresponding methods and classes, just like this:
publicclass Example { @KeeppublicvoiddoSomething() { // hopefully this method does something } ... }
If you're using a APPCOMPAT-V7 package, you've included the Support-annotations library, so start using it happily! (tease than such a simple article also want you to translate, smelly not face of ...) orz! )
Above check out the docs link need to turn over the wall to open!
Use Android annotations to improve your code