androidx_androidx的现实

androidx

As a matter of fact, Android Jetpack is set of libraries, tools, and guidance for modern Android development, which has been introduced by Google recently. Furthermore, AndroidX is the package name for all libraries within Android Jetpack. Basically, AndroidX is a significant enhancement to the original Android Support Library. This essay will discuss some concepts and best practices in using AndroidX for your Android projects.

事实上,Android Jetpack是一套用于现代Android开发的库,工具和指南,最近Google对其进行了介绍。 此外,AndroidX是Android Jetpack中所有库的软件包名称。 基本上,AndroidX是对原始Android支持库的重大增强。 本文将讨论在Android项目中使用AndroidX的一些概念和最佳做法。

简介与概述 (Introduction and Overview)

As you know, Android Jetpack is set of libraries, tools, and guidance for modern Android development. Currently, there are four categories for using Jetpack, which includes: Architecture, UI, Behavior, and Foundation. In other words, these best practices help developers for writing high quality Android apps easier, limiting boilerplate code, and simplifying complex tasks. As a result, you can now focus on the code that you really care about. In addition, AndroidX is the package name for all libraries within Jetpack. Therefore, you can think of AndroidX as the open source project that you can be able to use to develop, test, version, and release Jetpack libraries. In fact, in I/O of 2018, Google has announced that they refactored the Support Library to the AndroidX namespaces. Thus, they did this similar task with Support Library 28. Also, they announced AndroidX 1.0. Basically, AndroidX is a considerable enhancement to the original Android Support Library that is no longer maintained. AndroidX packages completely replace the Support Library by providing some new features and libraries.

如您所知,Android Jetpack是一套用于现代Android开发的库,工具和指南。 当前,有四种使用Jetpack的类别,包括:体系结构,UI,行为和基础。 换句话说,这些最佳实践可帮助开发人员更轻松地编写高质量的Android应用,限制样板代码并简化复杂的任务。 因此,您现在可以专注于您真正关心的代码。 此外,AndroidX是Jetpack中所有库的软件包名称。 因此,您可以将AndroidX视为一个开源项目,可以用来开发,测试,版本化和发布Jetpack库。 实际上,在2018年的I / O中,Google宣布他们将支持库重构为AndroidX名称空间。 因此,他们使用Support Library 28完成了类似的任务。此外,他们宣布了AndroidX 1.0。 基本上,AndroidX是对不再维护的原始Android 支持库的重大增强。 AndroidX软件包通过提供一些新功能和库完全替代了支持库。

Artifacts within the androidx namespace comprise the Android Jetpack libraries. Like the Support Library, libraries in the androidx namespace ship separately from the Android platform and provide backward compatibility across Android releases.

androidx名称空间中的工件包含Android Jetpack库。 与支持库一样, androidx名称空间中的库与Android平台分开提供,并在Android版本之间提供向后兼容性。

Now, the primary question is that why should we migrate to AndroidX?

现在,主要的问题是我们为什么要迁移到AndroidX?

迁移到AndroidX的原因 (Reasons for migrating to AndroidX)

  1. Version 28.0.0 is the last release of the Support Library. This namespace android.support has no longer maintaining. So, if you want to fix bugs or use advanced features that normally would have eliminated in the Support Library, you have to migrate to AndroidX. All new feature development will be found in the androidx namespace.

    版本28.0.0是支持库的最新版本。 该名称空间android.support不再维护。 因此,如果您想修复错误或使用支持库中通常不会提供的高级功能,则必须迁移到AndroidX。 所有新功能开发都可以在androidx名称空间中找到。

  2. Currently, you can be able to have better package management. This means unlike the Support Library, androidx packages are separately maintained and updated. The androidx packages use strict Semantic Versioning, starting with version 1.0.0. In short, you can have better standardized naming and more frequent releases.

    当前,您可以进行更好的软件包管理。 这意味着与支持库不同,androidx软件包是分别维护和更新的。 androidx软件包从1.0.0 版本开始使用严格的语义版本控制 。 简而言之,您可以拥有更好的标准化命名和更频繁的发布。

  3. Other libraries and third party libraries have migrated to the androidx namespace such as Google Play services, Firebase, Butterknife, Mockito 2, and SqlDelight. Besides, all new libraries are releasing and utilizing in AndroidX such as Compose or CameraX.

    其他库和第三方库已迁移到androidx命名空间,例如Google Play服务,Firebase,Butterknife,Mockito 2和SqlDelight。 此外,所有新库都在AndroidX中发布和使用,例如Compose或CameraX。

准备迁移到AndroidX的主要步骤 (Main steps to prepare for migrating to AndroidX)

  1. You should backup your Android project. Probably, most developers might be using Source Control tools. In any case, you should make a backup of your project because the migration will change many of the files in your Android project.

    您应该备份您的Android项目。 大概,大多数开发人员可能正在使用源代码管理工具。 无论如何,您都应该备份项目,因为迁移会更改Android项目中的许多文件。
  2. You should minimize feature development during the migration process. Furthermore, you should try to make these migrations on a separate branch when you are using a Source Control tool.

    您应在迁移过程中最大程度地减少功能开发。 此外,使用源代码管理工具时,应尝试在单独的分支上进行这些迁移。

Google推荐的迁移方法 (Recommended approach by Google on how to migrate)

  1. Updating the Support Library to version 28.

    将支持库更新到版本28。

As a matter of fact, if you have used an older version of the support library such as 26 and 27 versions, and try to make the migration to AndroidX, the first suggestion is that you should try to update to version 28. Also, you should address all of those API changes, and get your app compiling with 28. At that point, make them jump over to AndroidX. In fact, AndroidX artifacts with version 1.0.0 are binary equivalent to the Support Library 28.0.0 artifacts.That means just only the package name changes between two versions. It means all the APIs are similar. Thus, you will probably have a minimal set of things, which you require to fix between migrating from 28 to AndroidX.

事实上,如果您使用了支持库的旧版本(例如26和27版本),并尝试迁移到AndroidX,则第一个建议是您应该尝试更新到版本28。应该解决所有这些API更改,并使用28编译您的应用。那时,使它们跳转到AndroidX。 实际上,版本1.0.0的AndroidX工件与支持库28.0.0工件是二进制等效的,这意味着仅软件包名称在两个版本之间会发生变化。 这意味着所有API都是相似的。 因此,您可能只需要少量设置,就需要解决从28迁移到AndroidX之间的问题。

2. Enabling Jetifier in your Android application

2.在Android应用程序中启用Jetifier

Initially, Jetifier will help you migrate your third party dependencies to use AndroidX. This means is that Jetifier will change the dependencies to be compatible with your project that is using AndroidX. It will not change your source code, nor will it migrate your generated code. To use this feature, you should add a couple of lines to your gradle.properties files. In other words, useAndroidX is set to true, along with enableJetifier.

最初,Jetifier将帮助您迁移第三方依赖项以使用AndroidX。 这意味着Jetifier会将依赖关系更改为与使用AndroidX的项目兼容。 它不会更改您的源代码,也不会迁移您生成的代码。 要使用此功能,您应该在gradle.properties文件中添加几行。 换句话说, useAndroidXenableJetifier一起设置为true。

android.useAndroidX: If this flag is set to true, the Android plugin uses the appropriate AndroidX library instead of a Support Library. So, when you perform code auto completion and you import libraries, you will import the AndroidX version of that library instead of the old support Library version.

android.useAndroidX :如果此标志设置为true,则Android插件将使用适当的AndroidX库而不是支持库。 因此,当您执行代码自动完成并导入库时,将导入该库的AndroidX版本,而不是旧的支持库版本。

android.enableJetifier: If this flag is set to true, the Android plugin will automatically migrate existing third-party libraries to utilize AndroidX dependencies by rewriting their binaries.

android.enableJetifier :如果此标志设置为true,则Android插件将通过重写二进制文件来自动迁移现有的第三方库以利用AndroidX依赖项。

3. Updating your dependencies

3.更新依赖

Obviously, a number of third party dependencies may require to be updated before you want to start the migration process such as Butterknife, Glide, Mockito 2, and SqlDelight. So, if it is possible, you should try to update your third party dependencies first, before starting the migration. Now, if you are using a code generation library, Jetifier will not modify these. Therefore, you might need to check if the code generation library is compatible with AndroidX or not. Additionally, some common errors might occur if you want to skip steps 2 and 3. For instance, a third part library code that you are using is not compatible with AndroidX. Or, if you have an Android project that is partially migrated, you might get a duplicate class error.

显然,要启动迁移过程之前,可能需要更新许多第三方依赖项,例如Butterknife,Glide,Mockito 2和SqlDelight。 因此,如果可能的话,在开始迁移之前,您应该首先尝试更新第三方依赖项。 现在,如果您使用的是代码生成库,Jetifier将不会修改它们。 因此,您可能需要检查代码生成库是否与AndroidX兼容。 此外,如果您要跳过步骤2和3,则可能会发生一些常见错误。例如,您使用的第三方库代码与AndroidX不兼容。 或者,如果您的Android项目已部分迁移,则可能会收到重复的类错误。

4. Using Android Studio and bash script

4.使用Android Studio和bash脚本

In fact, from Android Studio 3.2 stable version and above, there is a handy migrate to AndroidX button. So, this button will perform the migration of your source code, and this will work for a majority of cases. However, some development teams may not use Android Studio. Also, there are some more sophisticated app architectures that the tool cannot exactly automate. As a result, there are two options in this situation. First, you could be migrated the source code by itself, and another option would be a bash script (you should replace all source code instances of support library packages with their AndroidX version). Another option is you can accomplish it manually. So, if you run into issues with migration as a manual option, you can be able to refer to tables to demonstrate the proper mappings from the support library to the corresponding AndroidX artifacts and classes:

实际上,从Android Studio 3.2稳定版本及更高版本开始,可以方便地迁移到AndroidX 按钮。 因此,此按钮将执行源代码的迁移,这在大多数情况下都适用。 但是,某些开发团队可能不使用Android Studio。 此外,还有一些工具无法完全自动化的更复杂的应用程序体系结构。 结果,在这种情况下有两种选择。 首先,您可以自行迁移源代码,另一个选择是bash脚本(您应将支持库软件包的所有源代码实例替换为其AndroidX版本)。 另一个选择是您可以手动完成。 因此,如果您遇到手动迁移的问题,则可以引用表来演示从支持库到相应的AndroidX工件和类的正确映射:

手动干预 (Manual intervention)

There are some situations that we cannot automate migration. This means some situations need manual intervention such as version configuration files, Proguard files, and Build scripts. Therefore, if you are using these features and you are also having package names in them, you will need to edit manually. For instance, to do version configuration in your build.gradle, you can be able to observe the versions variable numbers have not been changed after running the automate tool. So, it is clear that you will need to do a manual update on them.

在某些情况下,我们无法自动执行迁移。 这意味着某些情况需要手动干预,例如版本配置文件,Proguard文件和构建脚本。 因此,如果您正在使用这些功能,并且其中还包含程序包名称,则需要手动进行编辑。 例如,要在build.gradle中进行版本配置,您可以在运行自动工具后观察版本变量号未更改。 因此,很显然,您需要对它们进行手动更新。

合并冲突 (Merge conflicts)

To do merging process, Google has recommended to slow down or pause development. In fact, this manner diminishes the amount of merge conflicts that will occur. If it is possible, you should have a few people on your team for just only focusing on the migration process. Besides, you should try to do the migration on a separate branch. When you are doing this migration, it would be a great idea to just focus on addressing the errors, and getting your app to compile, and all tests passing again. In short, you should not accomplish and try to do refactoring, or do not try to pull in new features during this period of time. Furthermore, when you run the migration tool, you may have an alpha version or a stable version of the library, depending on the most recent release. In this case, depending on your particular needs in your project, you may want to use the alpha version or maybe you prefer stable. Thus, this is an issue that you will need to go back and manually check to observe which version it migrated to. Also, you should make sure that your selected version is appropriate for your needs.

为了进行合并,Google建议减慢或暂停开发。 实际上,这种方式减少了将要发生的合并冲突的数量。 如果有可能,您的团队中应该只有几个人,只专注于迁移过程。 此外,您应该尝试在单独的分支上进行迁移。 在进行此迁移时,最好只专注于解决错误,并让您的应用编译,并再次通过所有测试,这是一个好主意。 简而言之,您不应该在此期间完成并尝试进行重构,或者不要尝试引入新功能。 此外,在运行迁移工具时,您可能具有该库的Alpha版本或稳定版本,具体取决于最新版本。 在这种情况下,根据您项目中的特定需求,您可能要使用Alpha版本,或者您可能更喜欢稳定版本。 因此,这是一个问题,您需要返回并手动检查以观察其迁移到的版本。 另外,您应确保所选的版本适合您的需求。

结论 (In conclusion)

As you can see, Android Jetpack is set of libraries, tools, and guidance for modern Android development, which has been introduced by Google recently. Moreover, AndroidX is the package name for all libraries within Android Jetpack. Initially, AndroidX is a notable enhancement to the original Android Support Library. This article considered some concepts and best practices in using AndroidX for Android development.

如您所见,Android Jetpack是一组用于现代Android开发的库,工具和指南,最近Google对其进行了介绍。 此外,AndroidX是Android Jetpack中所有库的软件包名称。 最初,AndroidX是对原始Android支持库的显着增强。 本文考虑了使用AndroidX进行Android开发的一些概念和最佳做法。

翻译自: https://medium.com/kayvan-kaseb/the-reality-of-androidx-81df15ad54d

androidx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值