When writing conveniently, you can use annotations to declare some parameters to explicitly indicate the type of parameters, making the code more secure. We see extensive use of annotations in the Android source code. I have compiled some notes as follows:


@nonnull tells the compiler that this parameter is non-null and the compiler will check it for you.

Example:

@NonNull
public static Snackbar make(@NonNull View view, @StringRes int resId, @Duration int duration) {
    return make(view, view.getResources().getText(resId), duration);
}
Copy the code

@nullable declares that arguments can be null and the compiler will check for you

Example:

@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {}

@idres declares that int is an ID, and the compiler will check for you

public View findViewById(@IdRes int id) {

return getDelegate().findViewById(id);

}

@stringres declares that the int argument is a string resource, and the compiler will check for you

Example:

@NonNull

public Snackbar setAction(@StringRes int resId, View.OnClickListener listener) {

return setAction(mContext.getText(resId), listener);

}

@styleres declares that the argument is of type style, and the compiler will check for you

@Override

public void setTheme(@StyleRes final int resid) {}

The @layoutres declaration argument is of type layout, and the compiler checks it for you

Example:

public void setContentView(@LayoutRes int layoutResID)

There are a lot of similar, see the name to know what to use

@DimenRes @DrawableRes @RawRes @ColorRes @XmlRes

@BoolRes @IntegerRes @StyleableRes

The other:

@keep declares that this method is not to be confused