Home » Android » java – Android Support Annotation "should have @Retention(RetentionPolicy.SOURCE)"

java – Android Support Annotation "should have @Retention(RetentionPolicy.SOURCE)"

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am using the @Retention and @StringDef annotations for some of my methods in a library and I face a strange warning, I want to understand.

In a static class, SIP, I use this annotation:

public static final String CODEC_SPEEX_16K  = "speex/16000/1";
public static final String CODEC_SPEEX_8K   = "speex/8000/1";
public static final String CODEC_SPEEX_32K  = "speex/32000/1";
public static final String CODEC_ILBC_8K    = "iLBC/8000/1";
public static final String CODEC_GSM_8K     = "GSM/8000/1";
public static final String CODEC_PCMU_8K    = "PCMU/8000/1";
public static final String CODEC_PCMA_8K    = "PCMA/8000/1";
public static final String CODEC_G722_16K   = "G722/16000/1";

@Retention(RetentionPolicy.CLASS)
@StringDef({
    CODEC_SPEEX_16K,
    CODEC_SPEEX_8K,
    CODEC_SPEEX_32K,
    CODEC_ILBC_8K,
    CODEC_GSM_8K,
    CODEC_PCMU_8K,
    CODEC_PCMA_8K,
    CODEC_G722_16K
    })
public @interface CodecName {}

which compiles fine, without any warnings.

In the static class Tools I use this annotation:

public static final String RES_TYPE_STRING = "string";
public static final String RES_TYPE_DRAWABLE = "drawable";
public static final String RES_TYPE_LAYOUT = "layout";
public static final String RES_TYPE_VIEW = "id";
public static final String RES_TYPE_DIMEN = "dimen";
public static final String RES_TYPE_COLOR = "color";
public static final String RES_TYPE_ANIM = "anim";
public static final String RES_TYPE_MIPMAP = "mipmap";

@Retention(RetentionPolicy.CLASS)
@StringDef({
    RES_TYPE_STRING,
    RES_TYPE_DRAWABLE,
    RES_TYPE_LAYOUT,
    RES_TYPE_DIMEN,
    RES_TYPE_COLOR,
    RES_TYPE_ANIM,
    RES_TYPE_VIEW,
    RES_TYPE_MIPMAP
})
public @interface ResourceType {
}

and I get the warning:

The typedef annotation ….toolbox.Tools.ResourceType should have
@Retention(RetentionPolicy.SOURCE)

It seems to be uncritical, everything works fine.
But can someone please explain me, why

Annotation#1 does not get a warning ;

Annotation#2 gets a warning;

Both are built identical, both only used in static context. From my point of view, both are the same.

How to&Answers: