Lint does not build with the "WrongConstant: Invalid constant" security error. IntDef Annotations

In my Result class, I annotated @IntDef the first integer parameter in the newInstance() method, for example:

 public class Result { public static final int SUCCESS = 0; public static final int FAIL = 1; public static final int UNKNOWN = 2; // ... private Result(@Status int status, Uri uri) { mStatus = status; mUri = uri; } public static Result newInstance(@Status int status, Uri uri) { return new Result(status, uri); } @Retention(RetentionPolicy.SOURCE) @IntDef({ SUCCESS, FAIL, UNKNOWN }) @interface Status {} } 

Next, in my Utils class, I call this method and pass the correct constant as a parameter. I guarantee that I use a specific set of constants, for example:

 public static Result foo() { // ... return Result.newInstance(Result.SUCCESS, contentUri); // line 45 } 

But lint cannot build with security error

"WrongConstant: Invalid constant"

../../src/main/java/my/package/Utils.java: 45: Must be one of: 0, 1, 2

I know that this error can simply be suppressed. But I would like to know what happened to my code? Or maybe this is another problem?

+5
source share
2 answers

I had a similar problem with @StringDef constant. I assume there are some problems with this particular Lint check.

In the meantime, you can use the @SuppressLint annotation as a workaround:

 public static Result foo() { // ... @SuppressLint("WrongConstant") return Result.newInstance(Result.SUCCESS, contentUri); } 

Edit: this problem seems to be fixed with gradle plugin version 1.4.0-beta1
Problem 182179 - android - Lint gives @StringDef error errors in androidTests

+5
source

Like the error, the value should be 0,1,2.

Result .SUCCESS is -1

0
source

All Articles