支援資料庫設定
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
注意:Android 9.0 (API 級別 28) 推出後,支援程式庫也推出新版本,稱為 AndroidX,是 Jetpack 的一部分。AndroidX 程式庫包含現有的支援程式庫,以及最新的 Jetpack 元件。
您可以繼續使用支援程式庫。
Google Maven 仍會提供舊版構件 (版本 27 以前,且封裝為 android.support.*
)。不過,所有新程式庫開發作業都會在 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 核心公用程式庫,請新增下列項目: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
時使用支援資料庫。
android.app.ActionBar
,僅在開發 API 級別 11 以上版本時。
注意:在應用程式專案中加入支援程式庫後,強烈建議您縮減、混淆及最佳化應用程式,以便發布。除了使用模糊處理保護原始碼,縮減功能還會從您應用程式中包含的任何程式庫移除未使用的類別,盡可能縮減應用程式的下載大小。
如需使用部分支援資料庫功能的進一步指引,請參閱 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 建構檔案,建構檔案中的 minSdkVersion
設定會覆寫資訊清單設定。
plugins {
id 'com.android.application'
}
android {
...
defaultConfig {
minSdkVersion 16
...
}
...
}
在本例中,建構檔案設定會告知 Google Play,應用程式的預設建構變體可安裝在搭載 Android 4.1 (API 級別 16) 以上版本的裝置。如要進一步瞭解建構變數,請參閱「建構系統總覽」。
注意:如果您要加入多個支援程式庫,最低 SDK 版本必須是任何指定程式庫要求的最高版本。舉例來說,如果您的應用程式同時包含 v14 Preference Support 程式庫和 v17 Leanback 程式庫,最低 SDK 版本就必須是 17 以上。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[[["容易理解","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"]],["上次更新時間: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."]]