SDK 扩展利用模块化系统组件将 API 添加到之前发布的特定 API 级别的公共 SDK 中。当最终用户通过 Google Play 系统更新收到模块更新后,这些 API 即可在设备上使用。应用开发者可以在其应用中利用这些 API,以提供这些先前 Android 版本的 SDK 原本不支持的其他功能。
API 版本控制
从 Android 11(API 级别 30)开始,Android 设备就包含一组 SDK 扩展。添加的新 API 会包含在某个 API 级别中,但也可能包含在特定版本的 SDK 扩展中。例如,照片选择器的 ACTION_PICK_IMAGES API 添加到了 Android 13(API 级别 33)的公共 SDK 中,但它也可通过 SDK 扩展(从 R 扩展版本 2 开始)来使用。SDK 扩展名称对应一个整数常量(来自 Build.VERSION_CODES 的常量或 SdkExtensions 类中定义的常量,例如 SdkExtensions.AD_SERVICES)。
funisPhotoPickerAvailable():Boolean{returnSdkExtensions.getExtensionVersion(Build.VERSION_CODES.R)>=2// Safely use extension APIs that are available with Android 11 (API level 30) Extensions Version 2, such as Photo Picker.}
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# SDK Extensions leverage [modular system\ncomponents](https://source.android.com/docs/core/ota/modular-system) to add APIs\nto the public SDK for certain previously released API levels. These APIs are\ndelivered to devices when end-users receive module updates through [Google Play\nsystem\nupdates](https://support.google.com/product-documentation/answer/11462338). App\ndevelopers can utilize these APIs in their apps to provide additional\nfunctionality that wasn't originally available in the SDK for these previous\nversions of Android.\n\nAPI Versioning\n--------------\n\nStarting with Android 11 (API level 30), Android devices include a set of SDK\nExtensions. When new APIs are added, they are included in an API level, but may\nalso be included in an SDK extension of a particular version. For example, the\n[`ACTION_PICK_IMAGES`](/reference/android/provider/MediaStore#ACTION_PICK_IMAGES)\nAPI for Photo Picker was added to the public SDK in Android 13 (API level 33),\nbut is also available through SDK extensions starting in R Extensions Version 2.\n[SDK Extension names](#extension-names) correspond to an integer constant--either\na constant from\n[`Build.VERSION_CODES`](/reference/android/os/Build.VERSION_CODES), or one\ndefined in the `SdkExtensions` class (such as\n[`SdkExtensions.AD_SERVICES`](/reference/android/os/ext/SdkExtensions#AD_SERVICES)).\n\nDetermine which SDK Extensions to use\n-------------------------------------\n\nBefore you can use SDK Extension APIs, you first need to determine which SDKs\ninclude the APIs that support your app's use cases.\n\nThe API reference pages for SDK Extension APIs specify the earliest SDK\nextension version that your app can use to access an API. If the documentation\nalso specifies an Android platform version (referenced by API level), then that\nAPI is also available for all devices running that version of Android or higher.\n\nFor example, `ACTION_PICK_IMAGES` is available generally in the public SDK\nstarting with Android 13 (API level 33), but is also available on devices as far\nback as Android 11 (API level 30) so long as the device has at least R\nExtensions Version 2:\n\nTo use this API, you need to compile against an SDK that is at least API level\n33, or Extension Level at least 2.\n\nTo use an extension SDK, follow these steps:\n\n1. Look up the minimum extensions version that you need by checking the feature documentation and API reference for the APIs that you want to use.\n2. After you determine the required extension version for your feature set, open the SDK Manager in Android Studio.\n3. Select the Android SDK Platform entry with the corresponding extension version (or a higher version, as the APIs are additive). For example: Android SDK Platform 33, Extension Level 4.\n4. Declare these values in your app's `build.gradle.kts` or `build.gradle`\n file:\n\n ### Groovy\n\n ```groovy\n android {\n compileSdk 33\n compileSdkExtension 4\n ...\n }\n ```\n\n ### Kotlin\n\n ```kotlin\n android {\n compileSdk = 33\n compileSdkExtension = 4\n ...\n }\n ```\n\nCheck whether SDK Extensions are available\n------------------------------------------\n\nYour app can check what SDK Extension versions are available at runtime, and\nwhile developing you can look up the extension versions using Android Debug\nBridge (adb) commands, as described in the following sections.\n\n### Check at runtime\n\nYour app can check at runtime whether SDK Extensions are available for a given\nplatform version using the\n[`getExtensionVersion()`](/reference/android/os/ext/SdkExtensions#getExtensionVersion(int))\nmethod. For example, the following code would check whether extension version 2\nor higher for the Android 11 (API level 30) SDK Extension is available: \n\n### Kotlin\n\n```kotlin\nfun isPhotoPickerAvailable(): Boolean {\n return SdkExtensions.getExtensionVersion(Build.VERSION_CODES.R) \u003e= 2\n // Safely use extension APIs that are available with Android 11 (API level 30) Extensions Version 2, such as Photo Picker.\n}\n```\n\n### Java\n\n```java\npublic static final boolean isPhotoPickerAvailable() {\n return SdkExtensions.getExtensionVersion(Build.VERSION_CODES.R) \u003e= 2;\n}\n```\n\nThis is similar to doing a check based on\n[`Build.VERSION.SDK_INT`](/reference/android/os/Build.VERSION#SDK_INT): \n\n### Kotlin\n\n```kotlin\nfun isPhotoPickerAvailable(): Boolean {\n return Build.VERSION.SDK_INT \u003e= 33\n}\n```\n\n### Java\n\n```java\npublic static final boolean isPhotoPickerAvailable() {\n return Build.VERSION.SDK_INT \u003e= 33;\n}\n```\n\nThis `SDK_INT` check is still safe and valid, but `isPhotoPickerAvailable` would\nreturn false on some devices even though the extension API is available. As a\nresult, the `SDK_INT` check is not optimal, and the extension version check is a\nbetter way to check for API availability. All devices with `SDK_INT` greater\nthan or equal to `33` (Android 13 or higher) have the Photo Picker APIs in the\npublic SDK, but there are devices with `SDK_INT` less than 33 (such as Android\n11, 12, and 12L) that could also access the APIs if they have R extension\nversions of at least `2`.\n\nIn this case, using an extension version check can help your app deliver\nadditional functionality to more users. See [SDK Extension names and\nconstants](#extension-names) for a list of all the constants that you can use to\ncheck for certain SDK Extensions on a device.\n\n#### Ad Services Extensions\n\nSimilar to the general set of SDK Extensions, the `AdServices` API reference\nsometimes indicates that an API is part of an \"Ad Services Extensions\" version.\nUnlike the general SDK Extensions, Ad Services Extensions use the\n`SdkExtensions.AD_SERVICES` constant to determine which version is on a device: \n\n### Kotlin\n\n```kotlin\nfun isAdServicesAvailable(): Boolean {\n return SdkExtensions.getExtensionVersion(SdkExtensions.AD_SERVICES) \u003e= 4\n}\n```\n\n### Java\n\n```java\npublic static final boolean isAdServicesAvailable() {\n return SdkExtensions.getExtensionVersion(SdkExtensions.AD_SERVICES) \u003e= 4;\n}\n```\n\nTo learn more about the features in Ad Services Extensions and how to get\nstarted, see the [Ad Services Extensions\ndocumentation](/design-for-safety/privacy-sandbox/program-overview).\n\n#### Utility methods\n\nIn some cases, SDK Extensions have Jetpack utility methods for checking the\navailability of their SDK Extension APIs. For example, you can use a [Jetpack\nlibrary function to check for PhotoPicker\navailability](/reference/kotlin/androidx/activity/result/contract/ActivityResultContracts.PickVisualMedia#isPhotoPickerAvailable(android.content.Context)),\nwhich abstracts away the conditional version checks.\n\n#### Tools support\n\nIn Android Studio Flamingo \\| 2022.2.1 or higher, the lint tool can scan for\nissues with SDK Extension versions as part of its NewAPI check. In addition,\nAndroid Studio can auto-generate the correct version checks for APIs that are\nlaunched using SDK Extensions.\nThe lint tool flags instances where the minimum SDK Extensions version required to call an API has not been met.\n\n#### SDK Extension names and constants\n\nThe following table describes how the different sets of SDK Extensions that are\nlisted in API reference documentation map to constants that your app can use to\ncheck for API availability at runtime. The general set of SDK Extensions for\neach public SDK maps to values of\n[`Build.VERSION_CODES`](/reference/android/os/Build.VERSION_CODES).\n\n| SDK Extension name | Constant | Eligible devices |\n|------------------------|-----------------------------------|--------------------------------------|\n| R Extensions | `VERSION_CODES.R` | Android 11 (API Level 30) and higher |\n| S Extensions | `VERSION_CODES.S` | Android 12 (API Level 31) and higher |\n| T Extensions | `VERSION_CODES.TIRAMISU` | Android 13 (API level 33) and higher |\n| U Extensions | `VERSION_CODES.UPSIDE_DOWN_CAKE` | Android 14 (API level 34) and higher |\n| V Extensions | `VERSION_CODES.VANILLA_ICE_CREAM` | Android 15 (API level 35) and higher |\n| Ad Services Extensions | `SdkExtensions.AD_SERVICES` | Android 13 (API level 33) and higher |\n\n### Check using adb\n\nTo check which SDK Extensions are available on a device using adb, run the\nfollowing command: \n\n```\nadb shell getprop | grep build.version.extensions\n```\n\nAfter running the command, you'll see output that looks similar to this: \n\n [build.version.extensions.r]: [3] # Android 11 (API level 30) and higher\n [build.version.extensions.s]: [3] # Android 12 (API level 31) and higher\n [build.version.extensions.t]: [3] # Android 13 (API level 33) and higher\n\nEach line shows an SDK Extension that's present on the device along with their\ncorresponding extensions version (3 in this case)."]]