61

In my Kotlin Android project, I am using a function that has been deprecated starting from api 23, which is quite recent. So I need a way to disable those deprecated warnings. Is there an easy way to do so?

Cœur
  • 37,241
  • 25
  • 195
  • 267
loloof64
  • 5,252
  • 12
  • 41
  • 78
  • Please give a thumbs up on https://youtrack.jetbrains.com/issue/KT-8087, if you're interested in suppressing all deprecations. – TWiStErRob Feb 17 '23 at 12:40

2 Answers2

112

Use @Suppress annotation with argument "DEPRECATION":

@Suppress("DEPRECATION")
someObject.theDeprecatedFunction()

Instead of a single statement, you can also mark a function, a class or a file (@file:Suppress("DEPRECATION") in its beginning) with the annotation to suppress all the deprecation warnings issued there.

In IntelliJ IDEA this can also be done through Alt+Enter menu with caret placed on code with deprecation warning.

hotkey
  • 140,743
  • 39
  • 371
  • 326
  • 2
    Fyi, if it were an overridden function, using Alt+Enter would give you @Suppress("OverridingDeprecatedMember") – Aba Nov 03 '17 at 11:09
  • Is there a way to suppress the warning for just one method? I am using a deprecated class in only one method, but kotlin still complains about the import unless I suppress the whole file. – Magnus Sep 26 '19 at 02:28
24

In Kotlin

@SuppressWarnings

is changed to

@Suppress

For removing the strike through deprecation warnings you should add,

 @Suppress("DEPRECATION")

to remove the warning from the super method. By adding

@Suppress("OverridingDeprecatedMember")

The warning of the function will get removed. So, the complete annotation will be;

@Suppress("OverridingDeprecatedMember", "DEPRECATION")

As an additional note the deprecation should be written as "DEPRECATION" ( use capital letters)

Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77