是否有一个注释表示最大 Android API 版本?

ash*_*hes 14 android backwards-compatibility android-appcompat android-annotations androidx

该注释@RequiresApi有助于要求仅在给定的 API 级别或更高级别上调用带注释的元素。然而,似乎没有相应的注释来要求仅在给定的 API 或更低版本上调用带注释的元素。

例如,给出类似的内容:

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

doSomethingWithExternalStorage()从不包含检查的代码调用,例如:

if (VERSION.SDK_INT < 29) {
    doSomethingWithExternalStorage()
}
Run Code Online (Sandbox Code Playgroud)

如果没有上述 API 检查,doSomethingWithExternalStorage()将显示 lint 错误/警告,类似于如何@RequiresApi显示。

据我了解,这也和工作原理类似@DeprecatedSinceKotlin

是否有现有的注释可以满足这些要求,或者是否有其他方法可以达到预期的结果?

k3b*_*k3b 0

尝试这个

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

来自androidx-annotation-DeprecatedSinceApi-docs

表示该 API 仅在给定的 API 级别之前有用;之后就有更合适的平台API可用。