Use Android annotations to improve code and android annotations

Source: Internet
Author: User

Use Android annotations to improve code and android annotations

Last night I saw a good article, then English, so I decided to translate to share with you, this is the original link: http://www.michaelevans.org/blog/2015/07/14/improving-your-code-with-android-support-annotations/

If you have never heard of Android support for annotation libraries, you will miss a lightweight and flexible package that helps you capture bugs. This library contains many Java annotations, which will help Android Studio check your code for possible errors and send feedback to you. There are quite a few annotations in this library. I only want to describe a few annotations here. However, if you need other complete and clear instructions, you can check out the docs here.


@ NonNull and @ Nullable

@ NonNull and @ Nullable may be the most basic annotation support, but they are useful in most cases. If the return value of a parameter or method may be null, it is expressed by commenting on a parameter or method. Currently, Android Studio provides a good warning when we perform some insecure and rigorous encoding operations.

For example:

The modification is as follows:


Better yet:We can even use the @ CheckResult annotation as in the example to tell us that the return value type of this method is expected to be used, rather than the method of the Error Effect:


@ StringRes and @ DrawableRes

Do you call the setText Method on TextView? You may encounter inexplicable exceptions.java android.content.res.Resources$NotFoundException: String resource ID #0x3039? If you set an integer to setText, TextView assumes that the integer is the resource id of a string. To set the string text, this integer is queried. The @ StringRes annotation can be used to fix the problem that the integer type is invalid resource id.

public void setText(@StringRes int id) {     // Do something like getString(id), etc. }

If you set an invalid string resource id to the setText method, it will be shown as follows:

This library also provides the corresponding resource annotations for all resource types:java@DrawableRes, @ColorRes,@InterpolatorResAnd so on.

@ Keep
Today we found a new support annotation @ Keep. According to the description in the annotation document, this annotation has not yet been connected to the Gradle plug-in, but it can be retained by the annotation method and class during app obfuscation.
This convenient annotation will notify the obfuscator to retain the corresponding methods and classes, just like this:

public class Example {     @Keep public void doSomething() {         // hopefully this method does something     }     ... }

If you are using a appcompat-v7 package and you have included the support-annotations library, enjoy it! (You need to translate such a simple article. It's so ugly... Orz !)

AboveCheck out the docsThe link must be crossed to open it!

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.