Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android @Suppress errors vs @TargetApi

Sometimes I case out code for certain versions of Android that may run my application. Eclipse with the latest Android tools still notices that my Manifest document allows lower versions of Android and therefore decides my code has errors in it.

A solution to this is to add a @Suppress tag above the method so it doesn't report the error, or another suggestion is to add a @TargetApi tag above the function

I dont understand the differences, or the consequences

like image 703
CQM Avatar asked Jul 26 '26 19:07

CQM


1 Answers

@TargetApi(NN) says "Hey, Android! Yes, I know I am using something newer than what is allowed for in my android:minSdkVersion. That's OK, though, 'cause I am sure that I am using Build (or something) such that the newer code only runs on newer devices. Please pretend that my minSdkVersion is NN for the purposes of this (class|method)".

@SuppressLint, to address the same error, says "Hey, Android! Yes, I know I am using something newer than what is allowed for in my android:minSdkVersion. Quit complaining.".

Hence, given a choice of @TargetApi(NN) or @SuppressLint, go with @TargetApi(NN). There, if you start using something newer than NN -- and therefore your existing version-checking logic may be insufficient -- you will get yelled at again.

like image 150
CommonsWare Avatar answered Jul 28 '26 08:07

CommonsWare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!