@SuppressWarnings vs @SuppressLint

Can someone explain the differences between @SuppressWarnings and @SuppressLint ? When should we use one over the other?

I read the documentation, but still not getting the differences. An explanation using the example / sample code would be much appreciated. Thanks.

+6
source share
1 answer

Actually there are two lints: one belongs to the compiler, therefore it is specific for Java, and one belongs to Google and has specifics of Android.

If your warning is related to something in Java that is not specific to Android, it is suppressed with @SuppressWarnings , and if it is Android dependent, it is suppressed with @SuppressLint .

Android Lint Warnings

Lint warnings are listed below: http://tools.android.com/tips/lint-checks

So, let's assume that you have a warning about missing permissions, and the warning description begins. "This check checks your code and libraries and looks at the APIs used and checks for the set of permissions needed to access these APIs." On the lint warning page above, we find the following:

Missingpermission

Summary: Missing Permissions

Priority: 9/10 Severity level: Error Category: Correctness

This check checks your code and libraries and looks at the APIs used, and checks for the set of permissions required to access these APIs. If code using these APIs is called at run time, the program will fail.

In addition, for permissions that can be revoked (using targetSdkVersion 23), client code must also be prepared to handle calls that throw an exception if the user rejects the request for permission at run time.

So, to suppress this, we put this annotation in the code:

 @SuppressLint("MissingPermission") 

Compiler warnings

Let's say we find this warning:

"Unchecked cast: 'java.lang.Object' in 'java.lang.Integer' ..."

If you read this in a popup popup in Android Studio, there is a More... link at the end. When you click the More... link, the text expands and you find it below:

"Hint: Pass -Xlint: not installed in javac for more details.

This suggests that you would use "unchecked" in the annotation as follows:

 @SuppressWarnings("unchecked") 

For a list of compiler warnings, run javac -X :

 C:\>javac -X -Xlint Enable recommended warnings -Xlint:{all,auxiliaryclass,cast,classfile,deprecation,dep-ann,divzero,empty,fallthrough,finally,options,overloads,overrides,path,processing,rawtypes,serial,static,try,unchecked,varargs,-auxiliaryclass,-cast,-classfile,-deprecation,-dep-ann,-divzero,-empty,-fallthrough,-finally,-options,-overloads,-overrides,-path,-processing,-rawtypes,-serial,-static,-try,-unchecked,-varargs,none} Enable or disable specific warnings . . . 

These are the values ​​you can use in @SuppressWarnings .

+4
source

Source: https://habr.com/ru/post/1011469/


All Articles