We've had requests for this for Apple platforms too (SR-1778 and a handful of Radars). It's less important there because Apple does support adopting newer APIs while still targeting older OSs, but it still comes up during beta periods where someone wants to add features only available in (say) the iOS 13 beta, but still support building against the GM Xcode with the iOS 12 SDK from the same source code.
Since Android does not have weak linking, we can't support the runtime check if #available(Android 17, *) as a way to access newly-added APIs; it would at best be sugar for checking the OS version in ways you already can do. That in turn means there's no point in @available(Android 17, *) annotations either—while that restricts which clients can use something, the body of such a function is still compiled. We really do need something like what @Geordie_J started off with: a compile-time check with #if, if we want this at all.
What does this look like? It could be similar to the existing #available syntax, as shown in the original post:
#if sdk(Android 24, *)
// I am compiling against Android 24, something newer than Android 24, or a non-Android platform
#else
// I am compiling for Android, but the NDK I'm using is older than Android 24
#endif
#if sdk(iOS 13, tvOS 13, *)
// I am compiling against an iOS 13+ SDK, a tvOS 13+ SDK, or for a platform that is neither iOS nor tvOS
#else
// I am compiling for iOS or tvOS but with an older SDK
#endif
Or it could be similar to the os(…) check:
#if sdk(Android 24)
// I am compiling against Android 24 or newer
#else
// I am compiling against an old version of Android or another platform
#endif
#if sdk(iOS 13) || sdk(tvOS 13)
// I am compiling against an iOS 13+ SDK or a tvOS 13+ SDK
#else
// I am compiling against an old version of iOS or tvOS, or for another platform
#endif
We could even make it an error to check the SDK version if you're on the wrong platform.
#if sdk(Android 24)
// I am compiling against Android 24 or newer
#else
// I am compiling against an old version of Android
#endif
#if os(Android) && sdk(Android 24)
// I am compiling against Android 24 or newer
#else
// I am compiling against an old version of Android, or for another platform
#endif
I think given the Android (and Windows) development models, this is a worthwhile feature to add, and then we might as well add it for Apple SDKs too. It means the compiler has to know how to find an SDK's version, though, for each platform it supports.
cc also @compnerd, especially while we're debating the meaning of the word "SDK" in a Swift context in another thread.