The @TargetApi(11) hard-coded number?


Hi. Loving the book so far. I got to the challenge for chapter 6 and noticed that:

[code] Build.VERSION_CODES.HONEYCOMB = 11[/code]

So, why would you hard-code 11 in the @TargetApi? I found this works too:

[code] @TargetApi(Build.VERSION_CODES.HONEYCOMB)[/code]



Please answer this question. Is using a hard coded number interchangeable with using the static variable that is set to the same thing? Why use the variable in code and a hard coded integer in the annotation? Is it a moot point? Should we care? Is there no convention rule on this?



No problem.

TargetApi is one of the Annotation type, not variable.
It implements java.lang.annotation.Annotation interface like any other annotations.

There are many annotations (types) in Java.
They are used by Java compiler like Directives or by Javadoc for documentation or Android Lint for API version safety.

TargetApi is used by Lint only.
And the assigned value can be numeric literal or static(not instance) variable. Because when checking the code, Lint can know about it.

“Lint should treat this type as targeting a given API level, no matter what the project target is.”

Same name of classes or methods can be exist in different versions of SDK and their implementation can be different.
In this case if we use them without TaregetApi and Lint checking, our code can be crashed when running(such as “unresolved symbol”).
So it would be better to use TargetApi and Lint, I think.

Enjoy Android Programming!


Thanks! So, it’s just a debugging thing for Lint. I thought it was weird that such an annointation could take a static variable as input.

I was also posting this question to see if the authors are still around to answer questions. A hidden agenda. :smiley: