Is there an annotation that denotes a max Android API version?

795 views Asked by At

The annotation @RequiresApi is helpful for requiring that the annotated element is only called on the given API level or higher. However, there does not appear to be a corresponding annotation for requiring that the annotated element is only called on the given API or lower.

For example, given something like:

@RequiresMaxApi(28) // or @DeprecatedSinceAndroid(29)
fun doSomethingWithExternalStorage() {
    val dir = Environment.getExternalStorageDirectory() // This is deprecated in API 29
    // ...
}

Calling doSomethingWithExternalStorage() from code that does not include a check like:

if (VERSION.SDK_INT < 29) {
    doSomethingWithExternalStorage()
}

Without the above API check, doSomethingWithExternalStorage() would display a lint error/warning, similar to how @RequiresApi does.

From my understanding, this is also similar to how @DeprecatedSinceKotlin works.

Is there an existing annotation that meets these requirements, or is there another way to achieve the desired result?

1

There are 1 answers

0
k3b On

Try this

@androidx.annotation.DeprecatedSinceApi(29)
fun doSomethingWithExternalStorage() {
    val dir = Environment.getExternalStorageDirectory() // This is deprecated in API 29
    // ...
}

From androidx-annotation-DeprecatedSinceApi-docs :

Denotes that this API is only useful until the given API level; after that, a more suitable platform API is available.