支持库
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
注意:Android 9.0(API 级别 28)发布后,新版支持库 AndroidX 也随之诞生,它属于 Jetpack 的一部分。除了现有的支持库,AndroidX 库还包含最新的 Jetpack 组件。
您可以继续使用此支持库。以往的工件(这里指的是打包为 android.support.*
的 27 版及更早版本)在 Google Maven 中仍然可用。不过,所有新库的开发都将在 AndroidX 库中进行。
我们建议在所有新项目中使用 AndroidX 库。您还应该考虑将现有项目迁移到 AndroidX。
在开发支持多个 API 版本的应用时,您可能需要一种标准的方法,以便对早期版本的 Android 系统提供新功能,或者优雅地回退到等效的功能。您可以利用这些库提供相应的兼容性层,而无需构建代码来处理早期版本的平台。此外,支持库还提供标准 Framework API 中未提供的额外的工具类和功能,使您能更轻松地面向更多设备进行开发和提供支持。
Android 支持库最初是适用于各种应用的单个二进制库,如今它已经发展为一套用于应用开发的库。现在,其中许多库在应用开发过程中即使不是必需的,我们也强烈推荐使用。
本文概要介绍了支持库,帮助您了解其组件以及如何在应用中有效地使用。
注意:从支持库版本 26.0.0(2017 年 7 月)开始,对于大多数库软件包,大部分支持库中支持的最低 API 级别已提升至 Android 4.0(API 级别 14)。对于
请参阅版本支持和软件包
名称。
支持库的用途
支持库有一些独特的用途,而针对平台早期版本的向后兼容性类只是其中之一。下表完整列出了支持库在应用中的用途:
-
新版 API 的向后兼容性 - 大量的支持库都为较新的框架类和方法提供向后兼容性。例如,
Fragment
支持类为搭载 Android 3.0(API 级别 11)之前版本的设备中的 fragment 提供支持。
-
工具类和辅助类 - 支持库提供了很多辅助类,特别是针对界面开发。对于
例如,
RecyclerView
类为用户提供
用于显示和管理超长列表的界面微件,可在
Android 版本(API 级别 7 及更高级别)。
-
调试和实用工具 - 很多功能可以提供您集成到应用中的代码所不具备的实用工具,包括
support-annotations
库,它改进了对方法输入进行的代码 lint 检查;以及 MultiDex 支持,用于通过 65536 种以上的方法配置和分发应用。
使用支持 API 与使用框架 API
支持库提供的类和方法与 Android 框架中的 API 非常相似。了解这点后,您可能想确定应该使用 API 的框架版本,还是使用等效的支持库。以下指南介绍了何时应使用支持库类代替框架 API:
提供平台 API 类兼容实现的支持库类可能无法提供最新版本中的全套功能,这是因为主机设备平台版本存在限制。在这种情况下,支持库类会优雅地降级,并且可能无法提供当前平台 API 的所有功能或数据。因此,您应该查看您所使用的库类和方法对应的参考文档,并在搭载您应用支持的最早版本平台的设备上进行全面测试。
注意:支持库并不会为每个框架 API 提供等效的类和方法。在某些情况下,您可能需要将框架方法调用封装在显式 SDK 版本检查代码块中,并提供替代代码以处理设备上不可用的方法。如需详细了解如何在代码中使用版本检查,请参阅支持不同的平台版本。
版本支持和软件包名称
部分支持库软件包的名称会指示其最初支持的最低 API 级别。这些名称使用 v# 表示法,例如 support-v4 软件包。从支持库版本 26.0.0(2017 年 7 月发布)开始,所有支持库软件包所支持的最低 API 级别已更改为 Android 4.0(API 级别 14)。因此,在使用任何最新版本的支持库时,您不应假设 v# 软件包表示法会指示最低 API 支持级别。最新版本中的这项更改还意味着,名称为 v4 和 v7 的库软件包与其支持的最低 API 级别本质上等效。例如,对于 26.0.0 及更高版本的支持库,support-v4 和 support-v7 软件包支持的最低 API 级别均为 14。
支持库发布版本
支持库的发布版本(例如 24.2.0 或 25.0.1)不同于相应版本中的任何库支持的最低 API 级别。发布版本号可以指示此版本是针对哪个版本的平台 API 而构建的,因此,可以指示此版本的库中可能包含哪些最新的 API。
具体而言,发布版本号的第一部分(例如版本号 24.2.0 中的 24)通常与相应版本发布时可用的平台 API 版本对应。支持库的发布版本级别可以指示此版本集成了相应 API 级别的部分功能,但您不应假设其可以兼容新的平台 API 版本中发布的所有功能。
库依赖项
Android 支持库套件中的大多数库都依赖于一个或多个库。例如,几乎所有支持库都依赖于 support-compat
软件包。通常,您无需担心支持库依赖项,因为 Gradle 构建工具会通过自动包含依赖的库来为您管理库依赖项。
如果您想要查看您的应用中包含哪些库和库依赖项,请在应用开发项目的构建根目录下运行以下命令,获取关于此项目的依赖项的报告,其中包括 Android 支持库和其他库:
gradle -q dependencies your-app-project:dependencies
如需详细了解如何使用 Gradle 向开发项目添加支持库,请参阅支持库设置。如需详细了解如何使用 Gradle,请参阅配置 build。
请注意,所有 Android 支持库都还依赖于某些基本级别的平台。对于最新版本,则依赖于 Android 4.0(API 级别 14)或更高版本。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-08-23。
[null,null,["最后更新时间 (UTC):2024-08-23。"],[],[],null,["# Support Library\n\n**Note:** With the release of Android 9.0 (API level 28) there is\na new version of the support library called\n[AndroidX](/jetpack/androidx) which is part of [Jetpack](/jetpack).\nThe AndroidX library\ncontains the existing support library and also includes the latest Jetpack components.\n\n\u003cbr /\u003e\n\n\nYou can continue to use the support library.\nHistorical artifacts (those versioned 27 and earlier, and packaged as `android.support.*`) will\nremain available on Google Maven. However, all new library development\nwill occur in the [AndroidX](/jetpack/androidx) library.\n\n\u003cbr /\u003e\n\n\nWe recommend using the AndroidX libraries in all new projects. You should also consider\n[migrating](/jetpack/androidx/migrate) existing projects to AndroidX as well.\n\n\nWhen developing apps that support multiple API versions, you may want a\nstandard way to provide newer features on earlier versions of Android or\ngracefully fall back to equivalent functionality. Rather than building code\nto handle earlier versions of the platform, you can leverage these libraries\nto provide that compatibility layer. In addition, the Support Libraries\nprovide additional convenience classes and features not available in the\nstandard Framework API for easier development and support across more\ndevices.\n\n\nOriginally a single binary library for apps, the Android Support Library has\nevolved into a suite of libraries for app development. Many of these\nlibraries are now a strongly recommended, if not essential, part of app\ndevelopment.\n\n\nThis document provides an overview of the support library to help you\nunderstand its components and how to use it effectively in your app.\n\n\n**Caution:** Starting with Support Library release 26.0.0\n(July 2017), the minimum supported API level across most support libraries\nhas increased to Android 4.0 (API level 14) for most library packages. For\nmore information, see [Version Support and Package\nNames](#api-versions) on this page.\n\nUses for the Support Libraries\n------------------------------\n\n\nThere are a few distinct uses for the support libraries. Backward\ncompatibility classes for earlier versions of the platform is just one of\nthem. Here is a more complete list of ways you can use the support libraries\nin your app:\n\n- **Backward Compatibility for newer APIs** - A large amount of the support libraries provide backward compatibility for newer framework classes and methods. For example, the [Fragment](/reference/androidx/fragment/app/Fragment) support class provides support for fragments on devices running versions earlier than Android 3.0 (API level 11).\n- **Convenience and Helper Classes** - The support libraries provides a number of helper classes, particularly for user interface development. For example the [RecyclerView](/reference/androidx/recyclerview/widget/RecyclerView) class provides a user interface widget for displaying and managing very long lists, useable on versions of Android from API level 7 and up.\n- **Debugging and Utilities** - There are a number of features that provide utility beyond code you incorporate into your app, including the [`support-annotations`](/studio/write/annotations) library for improved code lint checks on method inputs and [Multidex support](/studio/build/multidex) for configuring and distributing apps with over 65,536 methods.\n\nUsing Support versus Framework APIs\n-----------------------------------\n\n\nSupport Libraries provide classes and methods that closely resemble APIs in\nthe Android Framework. Upon discovering this, you may wonder if you should\nuse the framework version of the API or the support library equivalent. Here\nare the guidelines for when you should use support library classes in place\nof Framework APIs:\n\n- **Compatibility for a Specific Feature** - If you want to support a recent platform feature on devices that are running earlier versions of the platform, use the equivalent classes and methods from the support library.\n- **Compatibility for Related Library Features** - More sophisticated support library classes may depend on one or more additional support library classes, so you should use support library classes for those dependencies. For example, the [ViewPager](/reference/androidx/viewpager/widget/ViewPager) support class should be used with [FragmentPagerAdapter](/reference/androidx/fragment/app/FragmentPagerAdapter) or the [FragmentStatePagerAdapter](/reference/androidx/fragment/app/FragmentStatePagerAdapter) support classes.\n- **General Device Compatibility** - If you do not have a specific platform feature you intend to use with your app in a backward compatible way, it is still a good idea to use support library classes in your app. For example, you may want to use [ActivityCompat](/reference/androidx/core/app/ActivityCompat) in place of the framework [Activity](/reference/android/app/Activity) class, so you can take advantage of newer features later on, such as incorporating the new permissions model introduced in Android 6.0 (API level 23).\n\n\nSupport Library classes that provide a compatible implementation of platform\nAPI classes may not be able to provide the full set of functionality\navailable in the latest release, due to the limitations of the host device\nplatform version. In these cases, Support Library classes are designed to\ndegrade gracefully, and may not provide the all the functionality or data of\nthe current, platform API. For this reason, you should review the reference\ndocumentation for the library classes and methods you use, and thoroughly\ntest on devices running the earliest version of the platform supported by\nyour app.\n\n\n**Note:** The support libraries do not provide equivalent\nclasses and methods for each framework API. In some cases, you may need to\nwrap a framework method call with an explicit SDK version check and provide\nalternative code to handle methods not available on a device. For more\ninformation on using version checks in your code, see [Supporting Different\nPlatform Versions](/training/basics/supporting-devices/platforms).\n\nVersion Support and Package Names\n---------------------------------\n\n\nSome of the Support Library packages have package names to indicate the\nminimum level of the API they originally supported,\nusing a v# notation, such as the\nsupport-v4 package. Starting with Support Library version 26.0.0 (released in\nJuly 2017), the minimum supported API level has changed to Android 4.0 (API\nlevel 14) for all support library packages. For this reason, when working with\nany recent release of the support library, you should not assume that the\nthe *v#* package notation indicates a minimum API support level.\nThis change in recent releases also means that library packages with the v4\nand v7 are essentially equivalent in the minimum level of API they support.\nFor example, the support-v4 and the support-v7 package both support a minimum\nAPI level of 14, for releases of the Support Library from 26.0.0 and higher.\n\n### Support Library Release Versions\n\n\nThe [release version](/topic/libraries/support-library/revisions) of the Support Library, such as 24.2.0 or 25.0.1, is\ndifferent from the minimum API level supported by any library in that\nrelease.The release version number indicates which version of the platform\nAPI it was built against, and therefore, what the most recent APIs *may be\nincluded* in this version of the libraries.\n\n\nSpecifically, the first section of the release version number, for example\nthe 24 in version 24.2.0, generally corresponds with the version of the\nplatform API available when it was released. The release version level of the\nsupport library indicates it incorporates *some* features of that API\nlevel, but you should not assume it provides compatibility with *all*\nfeatures released in the new platform API version.\n\nLibrary Dependencies\n--------------------\n\n\nMost libraries in the Android Support Library suite have some dependency on\none or more libraries. For example, nearly all support libraries have a\ndependency on the `support-compat` package. In general, you do not\nneed to worry about support library dependencies, because the gradle build\ntool manages library dependencies for you, by automatically including\ndependent libraries.\n\n\nIf you want to see what libraries and library dependencies are included in\nyour app, run the following command at the build root of your app development\nproject to get a report of the dependencies for that project, including\nAndroid Support Libraries and other libraries: \n\n```\ngradle -q dependencies your-app-project:dependencies\n```\n\n\nFor more information about adding support libraries to your development\nproject using Gradle, see [Support\nLibrary Setup](/topic/libraries/support-library/setup). For more information about working with Gradle, see\n[Configure\nYour Build](/studio/build).\n\n\nNote that *all* Android Support Libraries also depend on some base level\nof the platform, for recent releases, that is Android 4.0 (API level 14) or\nhigher."]]