Search code examples
androidkotlinbitmapdeprecated

"deprecated" vs "Field requires API level 30 (current min is 21)"


If I write:

bmp.compress(Bitmap.CompressFormat.WEBP_LOSSLESS, 80, image)

Android Studio shows an error message:

Field requires API level 30 (current min is 21) And if I write:

bmp.compress(Bitmap.CompressFormat.WEBP, 80, image)

says:

WEBP is deprecated. Deprecated in Java.

Should I do this?:

if(android.os.Build.VERSION.SDK_INT >= 30) {
    bmp.compress(Bitmap.CompressFormat.WEBP_LOSSLESS, 80, image)
}else{
    bmp.compress(Bitmap.CompressFormat.WEBP, 80, image)
}

In this case, the target SDK that we have defined in Android Studio does not interfere with this code?


Solution

  • Yes, that's exactly what you have to, unfortunately. Android studio won't complain or interfere.

    There are lots of questions that involve the exact same block that you wrote, for example in the answers to How to deal with deprecated classes in Android to keep compatibility

    Once you drop support for Android versions below API level 30, you can remove this block.