@TargetApi Compiler Error


#1

I’m on the section Suppressing Lint compatibility errors. I’ve used the @TargetApi(11) annotation and I’m still getting an error and can’t run. The error says:

Multiple markers at this line
- The attribute value is undefined for the annotation type TargetApi
- TargetApi cannot be resolved to a type

There is no error of the if Build.VERSION.SDK_INT statement anymore though.


#2

I figured it out. I had to import the annotation. The book doesn’t mention the need to organize your imports again after using @TargetApi


#3

When in doubt, organize imports :slight_smile:


#4

I had the same problem. Quick Google did not solve the problem, so I came here and found this post.

Organize Imports solved it for me. I would recommend adding this to future updates of the book. Despite having passed the Sun Certified Java Programmer 5 exam (admittedly a few years ago now :blush:), I did not know, or perhaps did not recall that custom annotations like @TargetApi had to be/could be imported.

I think a number of developers will get tripped up by this.

With the exception of this minor glitch, the book so far has been technically excellent - thanks Brian!

best regards,

Andrew


#5

Yes, following import statement is needed to refer TargetApi by compiler.

import android.annotation.TargetApi;

There’s no mention about it in the book. But downloaded project code is OK.

Have a nice day!


#6

Thanks, I literly copied the above “import"android.annotation.TargetApi;” to fix this syntax. The “Organize Imports” for some reason didn’t work for me.