Android 简明教程

Android - Support Library

当您在类似 5.x 的最新版本的 Android 上开发应用程序,并且还希望在运行较低版本 Android(例如 3.2 等)的设备上运行时,在向代码添加向后兼容性之前无法实现。

When you develop an app on a latest version of android like 5.x and you also want it to run on those devices which are running older versions of android like 3.2 e.t.c. you can’t do that until you add backward compatibility to your code.

为了提供这种向后兼容性,Android 为您提供了 Android Support Library 包。Android 支持库包是一组代码库,提供了 Android 框架 API 的向后兼容版本以及仅通过库 API 可用的功能。每个支持库都向后兼容于特定的 Android API 级别。

To provide this backward compatibility android provides you the Android Support Library package. The Android Support Library package is a set of code libraries that provide backward-compatible versions of Android framework APIs as well as features that are only available through the library APIs. Each Support Library is backward-compatible to a specific Android API level.

根据应用程序的目标平台版本范围及其使用的 API,将支持库包含在您的 Android 项目中被认为是应用程序开发人员的最佳实践。

Including the Support Libraries in your Android project is considered a best practice for application developers, depending on the range of platform versions your app is targeting and the APIs that it uses.

Support Library Features

Android 支持库包包含可在您的应用程序中包含的多个库。这些库中的每一个都支持特定范围的 Android 平台版本和功能集。

The Android Support Library package contains several libraries that can be included in your application. Each of these libraries supports a specific range of Android platform versions and set of features.

为了有效地使用这些库,考虑要定位的 API 级别非常重要,因为每个库都支持不同的 API 级别。

In order to effectively use the libraries, it is important to consider that which API level you want to target as each library supports different API level.

以下是 Android 支持库及其支持的 API 级别的简要说明。

Following is a brief description of android support libraries and API level they support.

Sr.No

Version & Features

1

v4 Support Library This library is designed to be used with Android 1.6 (API level 4) and higher.

2

v7 Support Library There are several libraries designed to be used with Android 2.1 (API level 7) and higher.

3

v8 Support Library This library is designed to be used with Android (API level 8) and higher.

4

v13 Support Library This library is designed to be used for Android 3.2 (API level 13) and higher.

请记住,鼓励和建议在您的应用代码中使用 Android Support Library。通过使用这些库,您可以增加您的目标市场和目标受众。

Please Remember that use of Android Support Library in your app code is encouraged and preferred. By using these libraries you can increase your target market and target audience.

Downloading the Support Libraries

请注意,在安装 support library 软件包之前,您应该明确您要在您的应用中要使用哪些功能。

Please note that before installing the support library packages you should be clear that what feature you want to use in your app.

Android Support Library 软件包可以通过 Android SDK Manager 获取。

The Android Support Library package is available through the Android SDK Manager.

按照下列步骤通过 SDK Manager 下载 support library 软件包。

Follow the following steps to download the support library package through the SDK Manager.

  1. Start the android SDK Manager.

  2. In the SDK Manager window, scroll to the end of the Packages list, find the Extras folder.

  3. Select the Android Support Library item.

  4. Click the Install packages button.

support lib

下载后,该工具会将 support library 文件安装到您现有的 Android SDK 目录中。该库文件位于您 SDK 的以下子目录中:/extras/android/support/ directory。

After downloading, the tool installs the Support Library files to your existing Android SDK directory. The library files are located in the following subdirectory of your SDK: /extras/android/support/ directory.

Choosing Support Libraries

在向您的应用程序中添加 Support Library 之前,决定您要包含哪些功能以及您要支持的最低 Android 版本。

Before adding a Support Library to your application, decide what features you want to include and the lowest Android versions you want to support.

Changes in Android build.gradle

如果您正在使用 support library 将您现有应用程序向后兼容性的增加到更早版本的 Android API,请确保更新您的应用程序的 build.gradle。具体来说,您应该将 build.gradle 中的 compileSdkVersion 元素更新为新的较低版本号,如下所示:

If you are increasing the backward compatibility of your existing application to an earlier version of the Android API with the Support Library, make sure to update your application’s build.gradle. Specifically, you should update the compileSdkVersion element in the build.gradle to the new, lower version number, as shown below −

android {
   compileSdkVersion 24
   buildToolsVersion "24.0.1"

   defaultConfig {
      applicationId "com.example.tutorialspoint7.myapplication"
      minSdkVersion 23
      targetSdkVersion 24
      versionCode 1
      versionName "1.0"
   }

   buildTypes {
      release {
         minifyEnabled false
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      }
   }
}

这一更改会告诉 Google Playstore 应用您的应用程序可以安装在最低 Android 版本为 23 的设备上。

This change tells Google Playstore app that your application can be installed on devices with Android minimum version of 23.