支持库设置
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
注意:Android 9.0(API 级别 28)发布后,新版支持库 AndroidX 也随之诞生,它属于 Jetpack 的一部分。除了现有的支持库,AndroidX 库还包含最新的 Jetpack 组件。
您可以继续使用此支持库。以往的工件(这里指的是打包为 android.support.*
的 27 版及更早版本)在 Google Maven 中仍然可用。不过,所有新库的开发都将在 AndroidX 库中进行。
我们建议在所有新项目中使用 AndroidX 库。您还应该考虑将现有项目迁移到 AndroidX。
如何在开发项目中设置 Android 支持库取决于您想要使用的功能,以及您希望应用支持的 Android 平台版本范围。
本文档将指导您下载支持库软件包以及向开发环境添加库。
支持库现在可以通过 Google 的 Maven 代码库获取。我们不再支持通过 SDK 管理器下载支持库,并且很快就会移除此功能。
选择支持库
在将支持库添加到应用之前,请确定您想要包含的功能以及想要支持的最低 Android 版本。如需详细了解不同的库提供的功能,请参阅支持库功能。
添加支持库
要使用支持库,您必须在开发环境中修改应用项目的类路径依赖项。您必须针对想要使用的每个支持库执行此流程。
要将支持库添加到应用项目中,请执行以下操作:
- 在项目的
settings.gradle
文件中添加 Google 的 Maven 代码库。
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
// If you're using a version of Gradle lower than 4.1, you must
// instead use:
//
// maven {
// url 'https://maven.google.com'
// }
}
}
- 对于要在其中使用支持库的每个模块,请在模块的
build.gradle
文件的 dependencies
块中添加相应库。例如,如需添加 v4 core-utils 库,请添加以下行:dependencies {
...
implementation "com.android.support:support-core-utils:28.0.0"
}
注意:使用动态依赖项(例如 palette-v7:23.0.+
)可能会导致意外的版本更新和回归不兼容问题。我们建议您明确指定库版本(例如 palette-v7:28.0.0
)。
使用支持库 API
为现有框架 API 提供支持的支持库类通常与框架类具有相同的名称,但它们位于 android.support
类软件包中,或者带有 *Compat
后缀。
注意:使用支持库中的类时,请确保从相应的软件包中导入类。例如,在应用 ActionBar
类时:
- 如果使用支持库,请从
android.support.v7.app.ActionBar
导入。
- 如果仅针对 API 级别 11 或更高级别进行开发,请从
android.app.ActionBar
导入。
注意:将支持库添加到应用项目后,我们强烈建议您对要发布的应用进行压缩、混淆和优化。除了通过混淆来保护源代码以外,还可以从添加到应用的任意库中移除不使用的类,从而确保最大程度地减少应用下载文件的大小。
如果在使用支持库的某些功能方面需要进一步的指导,请参阅 Android 开发者培训课程、指南和示例。如需详细了解支持库中的各个类和方法,请参阅 API 参考文档中的 android.support
软件包。
清单声明变更
如果您要提高现有应用的向后兼容性,以使其能够兼容带支持库的更早版本的 Android API,请确保更新应用的清单。具体而言,您应将清单中
<uses-sdk>
标记的 android:minSdkVersion
元素更新为较低的新版本号,如下所示:
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="23" />
此清单设置会告知 Google Play 您的应用可以安装在搭载 Android 4.0(API 级别 14)及更高版本的设备上。
如果您使用的是 Gradle build 文件,则 build 文件中的 minSdkVersion
设置会覆盖清单设置。
plugins {
id 'com.android.application'
}
android {
...
defaultConfig {
minSdkVersion 16
...
}
...
}
在这种情况下,构建文件设置会告知 Google Play 您应用的默认构建变体可以安装在搭载 Android 4.1(API 级别 16)及更高版本的设备上。如需详细了解 build 变体,请参阅构建系统概览。
注意:如果您要包含多个支持库,最低的 SDK 版本必须是任意指定库所要求的最高版本。例如,如果您的应用中包含 v14 Preference 支持库和 v17 Leanback 库,最低 SDK 版本必须为 17 或更高。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Support Library Setup\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\nHow you setup the Android Support Libraries in your development project depends on what features\nyou want to use and what range of Android platform versions you want to support with your\napplication.\n\nThis document guides you through downloading the Support Library package and adding libraries\nto your development environment.\n\nThe support libraries are now available through Google's Maven\nrepository. We no longer support downloading the libraries through the SDK\nManager, and that functionality will be removed soon..\n\nChoosing Support Libraries\n--------------------------\n\nBefore adding a Support Library to your application, decide what features you want to include\nand the lowest Android versions you want to support. For more information on the features\nprovided by the different libraries, see\n[Support Library Features](/tools/support-library/features).\n\nAdding Support Libraries\n------------------------\n\nIn order to use a Support Library, you must modify your application's project's\nclasspath dependencies within your development environment. You must perform this procedure for\neach Support Library you want to use.\n\nTo add a Support Library to your application project:\n\n1. Include Google's Maven repository in your project's `settings.gradle` file. \n\n ```groovy\n dependencyResolutionManagement {\n repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)\n repositories {\n google()\n\n // If you're using a version of Gradle lower than 4.1, you must\n // instead use:\n //\n // maven {\n // url 'https://maven.google.com'\n // }\n }\n }\n ```\n2. For each module in which you want to use a Support Library, add the library in the `dependencies` block of the module's `build.gradle` file. For example, to add the v4 core-utils library, add the following: \n\n ```groovy\n dependencies {\n ...\n implementation \"com.android.support:support-core-utils:28.0.0\"\n }\n ```\n\n\n**Caution:** Using dynamic dependencies (for example,\n`palette-v7:23.0.+`) can cause unexpected version updates and\nregression incompatibilities. We recommend that you explicitly specify a\nlibrary version (for example, `palette-v7:28.0.0`).\n\nUsing Support Library APIs\n--------------------------\n\nSupport Library classes that provide support for existing framework APIs typically have the\nsame name as framework class but are located in the `android.support` class packages,\nor have a `*Compat` suffix. \n**Caution:** When using classes from the Support Library, be certain you import\nthe class from the appropriate package. For example, when applying the `ActionBar`\nclass:\n\n- `android.support.v7.app.ActionBar` when using the Support Library.\n- `android.app.ActionBar` when developing only for API level 11 or higher.\n\n\n**Note:** After including the Support Library in your application project, we\nstrongly recommend that you [shrink, obfuscate, and optimize\nyour app](/studio/build/shrink-code) for release. In addition to protecting your source code with obfuscation, shrinking\nremoves unused classes from any libraries you include in your application, which keeps the\ndownload size of your application as small as possible.\n\nFurther guidance for using some Support Library features is provided in the Android developer\n[training classes](/training),\n[guides](/guide/components)\nand samples. For more information about the individual Support Library classes and methods, see\nthe [android.support](/reference/android/support/v4/app/package-summary) packages in the API reference.\n\n### Manifest Declaration Changes\n\nIf you are increasing the backward compatibility of your existing application to an earlier\nversion of the Android API with the Support Library, make sure to update your application's\nmanifest. Specifically, you should update the `android:minSdkVersion`\nelement of the [`\u003cuses-sdk\u003e`](/guide/topics/manifest/uses-sdk-element) tag in the manifest to the new, lower version number, as\nshown below: \n\n```xml\n \u003cuses-sdk\n android:minSdkVersion=\"14\"\n android:targetSdkVersion=\"23\" /\u003e\n```\n\nThe manifest setting tells Google Play that your application can be installed on devices with Android\n4.0 (API level 14) and higher.\n\nIf you are using Gradle build files, the `minSdkVersion` setting in the build file\noverrides the manifest settings. \n\n```groovy\nplugins {\n id 'com.android.application'\n}\n\nandroid {\n ...\n\n defaultConfig {\n minSdkVersion 16\n ...\n }\n ...\n}\n```\n\nIn this case, the build file setting tells Google Play that the default build variant of your\napplication can be installed on devices with Android 4.1 (API level 16) and higher. For more\ninformation about build variants, see\n[Build System Overview](/studio/build).\n\n\n**Note:** If you are including several support libraries, the\nminimum SDK version must be the *highest* version required by any of\nthe specified libraries. For example, if your app includes both the [v14 Preference Support library](/topic/libraries/support-library/features#v14-preference) and the\n[v17 Leanback library](/topic/libraries/support-library/features#v17-leanback), your minimum\nSDK version must be 17 or higher."]]