Android Developers Blog
The latest Android and Google Play news for app and game developers.
πŸ”
Platform Android Studio Google Play Jetpack Kotlin Docs News

09 δΈ€ζœˆ 2023

Extending the Android SDK


Link copied to clipboard

Posted by Anton Hansson, Software EngineerAndroid 10 and higher support Modular System Components that allow us to expedite functional and security updates to the Android ecosystem outside of major API level releases and make new functionality backward compatible on already-released Android versions. These improvements help make development more flexible and broaden the reach for app developers. We've built a new Extension SDK framework for you to integrate with these APIs, and today, we’re releasing the first public version of the Extension SDK (Extension Level 4).

Faster API and feature introductions

Having the ability to introduce new functionality outside of major API level releases allows faster innovations. As shared in a previous post, beginning this year we plan to roll out the initial Privacy Sandbox on Android Beta release to Android 13 devices. You can start using the Extension SDK to integrate your solutions with the AdServices APIs to prepare for limited production testing. Learn more on how to participate in the Privacy Sandbox Beta release, and set up your development environment with a test device or emulator.

Backward compatibility

Extension SDKs also allow us to extend the support of certain platform functionality to existing Android versions, increasing user reach. For example, the PhotoPicker APIs previously available only on API level 33 (Android T) and above are now also available all the way back to API level 30 (Android R) through the Extension SDK on devices with an R extension version of at least 2.
Moving image showing Photopicker API in action on a cellphone screen

Check for API availability

To help you identify extension API availability, we’ve added additional information to the API reference that indicates for which API levels and the minimum extension versions that the API is available. For example, the API reference for ACTION_PICK_IMAGES indicates its availability on “Android R Extensions version 2” and above.
Action_Pick_Images link Added in API level 33 Also in R Extensions 2
You can query the extension version at runtime in a similar way to how Build.VERSION.SDK_INT is commonly used to check for the Android version. For example, if you need to verify the availability of the PhotoPicker APIs, use the new API SdkExtensions.getExtensionVersion. For the R extensions, the version code (30) that corresponds to R is used:
fun isPhotoPickerAvailable(): Boolean { return SdkExtensions.getExtensionVersion(VERSION_CODES.R) >= 2 }

The alternative check, via Build.VERSION.SDK_INT, would look like this:

fun isPhotoPickerAvailable(): Boolean { return Build.VERSION.SDK_INT >= 33 }

This check is still safe and correct, but this function would return false on some devices where the API is now available. As a result, the SDK_INT check is not optimal, and the extension version check is a better way to check for API availability. All devices with SDK_INT >= 33 also have an R extension version of >= 2, but there are devices with SDK_INT < 33 with R extension versions >= 2.

Similarly, the AdServices API reference may indicate that it’s “added in Ad Services Extensions 4”. The Ad Services extension uses the SdkExtensions.AD_SERVICES constant. The availability check looks like this:

fun isAdServicesAvailable(): Boolean { return SdkExtensions.getExtensionVersion(SdkExtensions.AD_SERVICES) >= 4 }

For developer convenience, we are extending Jetpack to make it easier to work with extension versions. For example, you can use a Jetpack library function to check for PhotoPicker availability, which abstracts away the conditional version checks. We expect to be releasing more Jetpack libraries (such as the Privacy Preserving APIs in the Privacy Sandbox) to aid the correct use of APIs released via Extension SDKs.

Tooling support

To help ensure app quality, we added Extension versions tooling support to Android Lint's NewApi check. Since Android Studio Flamingo, it can auto-generate the correct version checks for APIs that have been launched via SDK extensions. Using these new version checks is completely optional, but adopting them could help lead to more widespread use of new APIs when they exist.
Screen grab of version check in use

Get familiarized with SDK extensions

We’re just beginning the SDK Extension developer journey and plan to make more features available in the future. You can get the latest SDK extension 4 available in the SDK Manager today. Learn more about the SDK Extensions and our documentation on the Privacy Sandbox Beta and the photo picker.