Mobile App Development : Tutorial How To Use Android Support Annotations Library

(11 votes, average: 5.00 out of 5)

votes, average:out of 5)

Loading... Loading...

The Android Support Annotations Library allows you to provide hints to code inspections tools like lint to help detect subtle code problems. They are added as metadata tags and you attach them to variables, parameters, and return values to inspect method return values, passed parameters, and local variables and fields. When used with code inspections tools, annotations can help you detect problems, such as null pointer exceptions and resource type conflicts.

Tung Dao Xuan, tungdx1210@gmail.com, is the author of this article and he contributes to RobustTechHouse Blog

Using The Library

Here we show you some examples for using Android Support Annotations Library.

2.1 Nullness Annotations

@NonNull

@Nullable

Add @Nullable and @NonNull annotations to check the nullness of a given variable, parameter, or return value. For example, if a local variable that contains a null value is passed as a parameter to a method with the @NonNull annotation attached to that parameter, building the code generates a warning indicating a non-null conflict.

Example for @Nullable :

Example for @NonNull :

2.2 Resource Type Annotations

@AnimatorRes

@AnimRes

@AnyRes

@ArrayRes

@AttrRes

@BoolRes

@ColorRes

@DimenRes

@DrawableRes

@FractionRes

@IdRes

@IntegerRes

@InterpolatorRes

@LayoutRes

@MenuRes

@PluralsRes

@RawRes

@StringRes

@StyleableRes

@StyleRes

@XmlRes

Add @StringRes annotations to check that a resource parameter contains a R.string reference. During code inspection, the annotation generates a warning if a R.string reference is not passed in the parameter.

For example for @StringRes:

For example for @DrawableRes:

For example for @MenuRes:

2.3 Threading Annotations

@UiThread

@MainThread

@WorkerThread

@BinderThread

If your method should only be called from a specific type of thread, you can annotate it with one of these 4 annotations.

Add @WorkerThread to specify the method should only be called from worker thread.

Example:

2.4 Value Constraints

@Size

@IntRange

@FloatRange

Add @IntRange or @FloatRange to specify your parameter must be in a certain integer, long or float, double range.

Add @Size to specify constraints on the size of the collection, length of arrays and the length of strings.

2.5 IntDef/StringDef: Typedef Annotations

@IntDef

@StringDef

Use the @IntDef and @StringDef annotations so you can create enumerated annotations of integer and string sets to validate other types of code references, such as passing references to a set of constants.

The following example illustrates the steps to create an enumerated annotation that ensures a value passed as a method parameter references one of the defined constants.

The line “@Retention(RetentionPolicy.SOURCE)” tells the compiler that usages of the new typedef annotation do not need to be recorded in .class files.

@StringDef is used for the same purpose but for strings.

2.6 RGB Color Integers

@ColorInt

You can specify @ColorRes when your API expects a color resource, but that won’t help when you have the opposite scenario: your API doesn’t expect a color resource id, but an actual RGB or ARGB color integer.

In that case, you can use the @ColorInt annotation, which states that an integer is expected to represent a color integer:

Example:

2.7 Overriding Methods

@CallSuper

If your API allows callers to override a method, but you need your own method to be invoked from any overriding methods as well, you can annotate the method with @CallSuper.

2.8 Return Values

@CheckResult

If your method is returning a value that callers are expected to do something with, annotate the method with @CheckResult.

References

Android Support Annotations Library references:

http://developer.android.com/reference/android/support/annotation/package-summary.html

http://tools.android.com/tech-docs/support-annotations

Conclusion

The Android Support Annotations Library is very easy to use and it helps you catch bugs very effectively. Hope you enjoy it. Thanks for reading!

If you like our articles, please follow and like our Facebook page where we regularly share interesting posts.

RobustTechHouse is a leading tech company focusing on mobile app development, ECommerce, Mobile-Commerce and Financial Technology (FinTech) in Singapore. If you are interested to engage RobustTechHouse for your projects, you can contact us here.