问题
I use a version switch to support older Android versions.
int sdk = Build.VERSION.SDK_INT;
if (sdk < Build.VERSION_CODES.HONEYCOMB) {
ColorDrawable colorDrawable = new ColorDrawable(shapeColor);
//noinspection deprecation
viewHolder.shape.setBackgroundDrawable(colorDrawable);
} else {
viewHolder.shape.setColor(shapeColor);
}
When build the project with Gradle from the command line the following warning is output by Lint:
app/src/main/java/com/example/MyApp/CustomListAdapter.java:92: warning:
[deprecation] setBackgroundDrawable(Drawable) in View has been deprecated
viewHolder.shape.setBackgroundDrawable(colorDrawable);
^
Can I annotate the specific line or method to mute the warning (since I do it on purpose)? I do not want to disable all warnings.
回答1:
Just something new: Not sure about Android Studio, but, to remove this warning from this line, you can use:
//noinspection deprecation
This removes the warning from the next line. E.g:
//noinspection deprecation
e.setBackgroundDrawable(editTextDrawable);
It won't show an error. However, as @JJD said, this still outputs the warning to the console. But at least you can have a nice error-less code which can be useful like for Git for example. And, this prevents the problem with @SupressWarnings
, which is it ignores all warnings in the method. So if you have something deprecated that you are not aware of, @SupressWarnings
will hide it and you will not be warned. That is the advantage of the //noinspection
回答2:
I've noticed that the @SuppressLint("deprecated")
inline annotation won't be picked up anymore - while @SuppressWarnings("deprecation")
is being picked up.
one can disable the Deprecation
checks for the Gradle linter with lintOptions
within the module-level build.gradle
file; while there is no chance to define individual files like that:
android {
lintOptions {
disable 'Deprecation'
}
}
or on can assign one rather detailed lint.xml
configuration file with LintOptions:lintConfig (when settings showAll true
, it will still show the warnings - no matter the provided XML configuration):
android {
lintOptions {
lintConfig file("lint.xml")
showAll false
}
}
where one can add individual files, by adding their paths:
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<issue id="Deprecation" severity="Error">
<ignore path="app/src/main/java/com/example/MyApp/CustomListAdapter.java" />
</issue>
</lint>
The source code of com.android.builder.model.LintOptions
might explain, what actually happens there (and confirms about 50% of what I've wrote).
in order to get rid of the inline warnings in Android Studio... that linter appears to be another linter - and these annotations do not affect the linter of the Gradle build (it may be required to use this combined with one of the methods stated above, in order to ignore known deprecated classes and methods):
//noinspection deprecation
update The Android Studio 2.3 release notes mention a new feature:
Lint Baseline: With Android Studio 2.3, you can set unresolved lint warnings as a baseline in your project. From that point forward, Lint will report only new issues. This is helpful if you have many legacy lint issues in your app, but just want to focus on fixing new issues. Learn more about Lint baseline and the new Lint checks & annotations added in this release.
here it's explained, how to create a Lint warnings baseline
- which records the detected warnings into an XML file and then mutes them (which is way better than to have the code annotations inline, distributed all over the place); I'd assume, that options lintConfig
and baseline
should be combine-able (depending on the requirements).
android {
lintOptions {
baseline file("lint-baseline.xml")
}
}
回答3:
I ran into a similar problem. First I got a compiler warning:
:compileDebugJava
Note: /path/file.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Which you can suppress with @SuppressWarnings("deprecation")
or just ignore since it is a warning and does cause your build to fail.
Additionally I got the lint error (details in build/lint-results.html
):
Call requires API level 13 (current min is 9)
This could be suppressed by adding @SuppressLint("NewApi")
. Alternatively you could use @TargetApi(13)
to hint that the method/class may use methods that depend on API version 13, rather than what you have set as minSdkVersion
(e.g. 9).
The annotations can only be done at a class or function level, not for a single line. Also note that "deprecation" should not be capitalized, while that didn't seem to matter for "NewApi".
回答4:
You need to create a lint.xml file to tell lint what to ignore.
http://tools.android.com/tips/lint/suppressing-lint-warnings see this for more details
yours might look a little like this
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<!-- Disable the given check in this project -->
<issue id="Deprecation">
<ignore path="app/src/main/java/com/example/MyApp/CustomListAdapter.java" />
</issue>
</lint>
To handle this in the source you should use something like
@SuppressLint("Deprecation")
回答5:
Case is important, use the following either inline or class-wide:
@Suppress("DEPRECATION")
This is in Kotlin.
回答6:
Try to find a method from ViewCompat
to replace the deprecated method.
In your case, use ViewCompat.setBackground(View, Drawable)
.
There are many classes named XXXCompat
for cases like that, such as ContextCompat
, ActivityCompat
and so on.
回答7:
To avoid lint warnings, always split functions so one function deals with the old system and other one deals with the new system. The old can supress the warning safely. The new one should be annotated to be used only on newest api levels.
This is an example on how it should look:
@SuppressWarnings("deprecation")
private static int getVersionCode_old(@NonNull Context appContext) {
PackageInfo pInfo;
try {
pInfo = appContext.getPackageManager().getPackageInfo(appContext.getPackageName(), 0);
return pInfo.versionCode;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
@RequiresApi(api = Build.VERSION_CODES.P)
private static int getVersionCode_new(@NonNull Context appContext) {
PackageInfo pInfo ;
try {
pInfo = appContext.getPackageManager().getPackageInfo(appContext.getPackageName(), 0);
return (int) pInfo.getLongVersionCode();
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public static int getVersionCodeUniversal(@NonNull Context appContext)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
return getVersionCode_new(appContext);
}
else
{
return getVersionCode_old(appContext);
}
}
Another important hint to avoid lint warnings: if you are using a whole deprecated class then you should remove all explicit imports for that class. Then just access to that class directly using its full path, and only do it in the old versions of your functions.
And finally, you should consider start using androidX, the new Google libraries where you will find a lot of universal functions ready to use. Then you can save a lot of time with this kind of small problems. For example, you can remove all the code of the above example and simply use this new and universal androidX function:
PackageInfo.getLongVersionCode()
回答8:
Just use @SuppressWarnings("deprecation")
above the function to suppress that specific warning for that function only.
Works like a charm!
@Blackd has the better answer. You should accept that!
来源:https://stackoverflow.com/questions/24208410/how-to-suppress-specific-lint-warning-for-deprecated-android-function