Android 中的支持库包是一组代码库,其主要目的是为代码和 Android API 框架提供向后兼容性。在现实世界中,在最新 Android 版本上开发的应用程序很可能会在较旧版本的 Android 操作系统上使用。因此,在项目文件中包含支持库是开发 Android 应用程序时要遵循的最佳实践。此外,仅通过库 API 提供的功能也需要这些库。
Android 支持库(com.android.support)
这些库包由 Google 提供,以向后兼容 Android 应用程序。这些包的名称以库支持的最低 Android API 版本结尾。例如,包名support-v4和support-v7 分别表示支持的最低 Android API 版本为 4 和 7。然而,库函数和模块的不断进步导致最低支持的Android API级别版本随着2017年7月支持库版本26.0.0的发布而更改为级别14(Android 4.0) 。
AndroidX 包库(androidx.*)
随着2018 年Android Jetpack 的发布,AndroidX 是一种全新的组织支持库的方式。较旧的支持库让开发人员有些困惑,因为人们无法仅通过查看名称就知道特定库中包含哪些所有类。为了解决这个问题,谷歌随着Android 9.0(API 级别 28)的发布推出了AndroidX(代表 Android EX 张力)库。任何以androidx开头的库名称。会自动成为 Jetpack 的一部分。它包括现有的 android 支持库以及最新的 Jetpack 组件。此外,开发人员无需担心库的版本,因为 Jetpack 知道要使用什么代码。
差异表
Android Support Libraries |
AndroidX Package Libraries |
---|---|
Syntax of writing dependency: com.android.support:recyclerview-v7 | Syntax of writing dependency: androidx.recyclerview:recyclerview |
While using support libraries in the project, it is mandatory to keep the same version for all the support libraries. | Developers are free to use a different version of dependency for different android components. |
Dependencies that are required to add in order to use an android component in the application include various other things that are of no use. | Developers are allowed to add only those dependencies which they are going to use in the application. |
Tha package of support library is bundled with the Android operating system. For eg: android.content.Intent | Unbundled libraries are moved to androidx.* namespace and are packed with application’s APK. For eg: androidx.fragment.app.Fragment |
The package name contains the minimum supported API level. | The package has no dependency on the API level. |
All dependencies are required to update before using because all the support libraries need to have the exact same version specification. | AndroidX libraries can be updated individually. Thus, developers can update only those libraries which are needed in the project. |
The support of com.android.support libraries have been stopped by Google. | It is the recommended library package by Google to use in the project. |
Note:
With the release of Android API level 28 i.e, Android 9.0, Google has stopped the support for com.android.support libraries and advised the developers to use AndroidX libraries which are a part of Jetpack for all new projects. To know what is the new mapping of support libraries in AndroidX, click here.
Google also provide the feature to migrate an existing project in order to use the AndroidX libraries. Visit this link to read the official Google documentation.