迁移到androidx_迁移到androidx技巧和指南

迁移到androidx

Jetpack is a set of libraries, tools, and guidance to help you write high-quality apps more easily. Jetpack makes coding easier through best practices, limiting boilerplate code, and simplifying complex tasks. All with the goal of enabling you to focus on the code that you really care about.

Jetpack是一组库,工具和指南,可帮助您更轻松地编写高质量的应用程序。 Jetpack通过最佳实践,限制样板代码并简化复杂的任务,使编码变得更容易。 所有这些目标都是使您能够专注于您真正关心的代码。

AndroidX is the package name for all the libraries within Jetpack. Think of AndroidX as the open-source project used to develop, test, version, and release Jetpack libraries.

AndroidX是Jetpack中所有库的软件包名称。 将AndroidX视为用于开发,测试,版本化和发布Jetpack库的开源项目。

Back at I/O 2018, we announced that Support Library would be refactored into the AndroidX namespace, which was completed with Support Library 28 and the announcement of AndroidX 1.0.

在I / O 2018上,我们宣布将支持库重构为AndroidX命名空间,该命名空间已通过Support Library 28和AndroidX 1.0的发布完成。

为什么要迁移? (Why migrate?)

The time is right now to migrate from using the Android Support Library to AndroidX. There are four reasons behind this:

现在是时候从使用Android支持库迁移到AndroidX。 这背后有四个原因:

  1. The Android Support Library has reached the end of its life. 28.0 was the last release of the Android Support namespace and the namespace is no longer maintained. So, if you want bug fixes or new features that would have previously gone into the Support Library, you need to migrate to AndroidX.

    Android支持库已寿终正寝。 28.0是Android支持名称空间的最新版本,并且不再维护该名称空间。 因此,如果您想要以前在支持库中获得的错误修复或新功能,则需要迁移到AndroidX。
  2. Better package management. With AndroidX, you get standardized and independent versioning, as well as better standardized naming and more frequent releases.

    更好的包装管理。 使用AndroidX,您可以获得标准化和独立的版本控制,以及更好的标准化命名和更频繁的发行。
  3. Other libraries have migrated to use the AndroidX namespace libraries, including Google Play services, Firebase, Butterknife, Mockito 2, and SQLDelight among others.

    其他库已迁移为使用AndroidX名称空间库,包括Google Play服务,Firebase,Butterknife,Mockito 2和SQLDelight等。
  4. All new Jetpack libraries will be released in AndroidX namespace. So, for example, to take advantage of Jetpack Compose or CameraX, you need to migrate to the AndroidX namespace.

    所有新的Jetpack库都将在AndroidX名称空间中发布。 因此,例如,要利用Jetpack ComposeCameraX ,您需要迁移到AndroidX命名空间。

准备迁移 (Preparing to migrate)

Before you start to migrate to AndroidX you should:

在开始迁移到AndroidX之前,您应该:

  • back up your project. If you’re using source control tools, it is still recommended that you make a backup, because migration will change many of the files in your project.

    备份您的项目。 如果使用的是源代码管理工具 ,仍建议您进行备份,因为迁移会更改项目中的许多文件。

  • create a new branch on which to make the migration changes.

    创建一个新分支,在该分支上进行迁移更改。
  • if possible, suspend or minimize development (at least don’t try to do refactoring or pull in new features) during the migration, as this will help reduce the number of merge conflicts that could happen.

    如果可能的话,在迁移过程中暂停或最小化开发(至少不要尝试进行重构或引入新功能),因为这将有助于减少可能发生的合并冲突。

迁移 (Migrate)

Throughout the migration steps focus on addressing errors, getting your app to compile, and passing all tests.

在整个迁移步骤中,着重于解决错误,编译应用程序以及通过所有测试。

步骤1:更新到支持库版本28 (Step 1: Update to Support Library version 28)

Migrating directly to AndroidX from an older version of the Support Library, say 26 or 27, isn’t recommended: not only would you need to address the namespace changes, you would also need to address the API changes between the older version and AndroidX.

不建议从支持库的旧版本(例如26或27)直接迁移到AndroidX:不仅需要解决名称空间更改,还需要解决旧版本和AndroidX之间的API更改。

It is, therefore, recommended that you update to version 28, address all of the API changes, and ensure your app compiles with version 28.

因此,建议您更新到版本28,解决所有API更改,并确保您的应用使用版本28进行编译。

Support Library version 28 and AndroidX 1.0 are binary equivalent, meaning that only the package name changes between those two versions: all the APIs are the same. This means that you should have very little to fix when migrating from 28 to AndroidX.

支持库版本28和AndroidX 1.0是等效的二进制文件,这意味着在这两个版本之间只有程序包名称更改:所有API都相同。 这意味着从28迁移到AndroidX时,您几乎不需要修复。

步骤2:启用Jetifier (Step 2: Enable Jetifier)

Jetifier helps migrate third-party dependencies to use AndroidX. Jetifier will change the byte code of those dependencies to make them compatible with projects using AndroidX. However, Jetifier won’t change your source code or migrate your generated code.

Jetifier帮助迁移第三方依赖项以使用AndroidX。 Jetifier将更改这些依赖项的字节码,以使其与使用AndroidX的项目兼容。 但是,Jetifier不会更改您的源代码或迁移您生成的代码。

To enable Jetifier in your app, add the following to your gradle.properties files:

要在您的应用中启用Jetifier,请将以下内容添加到gradle.properties文件中:

android.useAndroidX=trueandroid.enableJetifier=true

Now, when code auto-completion import libraries, you’ll import the AndroidX version of that library instead of the old Support Library version.

现在,当代码自动完成导入库时,您将导入该库的AndroidX版本,而不是旧的支持库版本。

步骤3.更新依赖关系 (Step 3. Update dependencies)

Before starting the migration, you should update each third-party library — such as Butterknife, Glide, Mockito 2, and SqlDelight — to the most recent version of the library. Not doing so could result in unexplained compilation errors.

开始迁移之前,应将每个第三方库(例如Butterknife,Glide,Mockito 2和SqlDelight)更新到该库的最新版本。 否则可能导致无法解释的编译错误。

If you’re using a code generation library, Jetifier won’t modify these. So you will need to check that the code generation library is compatible with AndroidX.

如果您使用的是代码生成库,Jetifier将不会对其进行修改。 因此,您需要检查代码生成库是否与AndroidX兼容。

If you consider skipping steps 2 and 3 here are some of the errors you could encounter:

如果您考虑跳过步骤2和3,则可能会遇到一些错误:

  • the third-party code you’re using is not compatible with AndroidX. In this case, a stack trace similar to the one below will show you that it’s trying to pull in the old version of the Support Library:

    您使用的第三方代码与AndroidX不兼容。 在这种情况下,类似于下面的堆栈跟踪将向您显示它正在尝试获取旧版本的支持库:
…Error : Program type already present: android.support.v4.app.INotificationSideChannel$Stub$Proxy |Reason: Program type already present: android.support.v4.app.INotificationSideChannel$Stub$Proxy…
  • if you have a project that’s partially migrated, you might get a duplicate class error, where it’s trying to pull in the same code from both the Support Library and AndroidX. The stack trace will show you something like this:

    如果您有部分迁移的项目,则可能会收到重复的类错误,该错误试图从支持库和AndroidX提取相同的代码。 堆栈跟踪将显示如下内容:
…Duplicate class android.support.v4.app.INotificationSideChannel found in modules classes.jar (androidx.core:core:1.0.0) and classes.jar (com.android.support:support-compat:28.0.0)…

步骤4:更新您的原始码 (Step 4: Update your source code)

You have 3 options to update your source code to use AndroidX:

您有3种选择来更新源代码以使用AndroidX:

  • Android Studio

    Android Studio
  • manual update

    手动更新
  • Bash script

    Bash脚本

If you use Android Studio 3.2 stable or above, you can update your source code using the Migrate to AndroidX option on the Refactor menu. This is the recommended way as Android Studio can examine your source code to make correct decisions when refactoring.

如果您使用的是稳定的Android Studio 3.2或更高版本,则可以使用“ 重构”菜单上的“ 迁移到AndroidX”选项来更新源代码。 这是推荐的方法,因为Android Studio可以在重构时检查您的源代码以做出正确的决定。

Image for post

If you don’t use Android Studio or employ a more sophisticated app architecture that the Migrate to AndroidX option doesn’t cover, you should instead leverage the class mapping csv file to implement a find and replace bash script. This script should find all source code instances of the android.support classes and then replace them with their AndroidX equivalent.

如果您不使用Android Studio或使用“迁移到AndroidX”选项无法涵盖的更复杂的应用程序体系结构,则应改用类映射csv文件来实现查找和替换bash脚本。 此脚本应该找到android.support类的所有源代码实例,然后将其替换为同等的AndroidX。

More concretely, the script should take the CSV file that provides the class mapping and then run a grep command followed by a sed command to replace the package names. However, because this is a brute force approach to migration, this basic find and replace may not sufficiently or correctly complete the migration.

更具体地说,脚本应采用提供类映射的CSV文件,然后运行grep命令和sed命令以替换程序包名称。 但是,由于这是一种蛮力的迁移方法,因此这种基本的查找和替换可能无法充分或正确地完成迁移。

Additionally, the update can also be done manually.

此外,更新也可以手动完成。

If you decide to take the manual approach, visit the Migrate to AndroidX page, where you can find an artifact mapping that details the Support Library packages and their corresponding class mapping is AndroidX. You can also download the CSV file from this page.

如果您决定采用手动方法,请访问“迁移到AndroidX”页面,您可以在其中找到详细描述支持库软件包及其对应类映射为AndroidX的工件映射 。 您也可以从此页面下载CSV文件。

And that’s it, you should now have a project that compiles and uses AndroidX.

就是这样,您现在应该拥有一个可编译并使用AndroidX的项目。

那些讨厌的例外 (Those pesky exceptions)

While the tools and processes we’ve discussed here should guide most apps smoothly through the migration, we’ve found that there are some cases where you may need to make changes manually.

尽管我们在此讨论的工具和流程应能顺利指导大多数应用程序迁移,但我们发现在某些情况下您可能需要手动进行更改。

版本配置文件 (Version configuration files)

You will need to manually reapply variables that define library versions. To illustrate, your build.gradle file might look like this prior to migration:

您将需要手动重新应用定义库版本的变量。 为了说明这一点,在迁移之前,您的build.gradle文件可能如下所示:

ext.versions = [    ‘drawer’ : ‘28.0.0’,    ‘rview’ : ‘28.0.0’]implementation “com.android.support:drawerlayout:${versions.drawer}”implementation “com.android.support:recyclerview-v7:${versions.rview}”

Then, after running the migration tool you end up with this:

然后,在运行迁移工具后,您将得到以下结果:

ext.versions = [    ‘drawer’ : ‘28.0.0’,    ‘rview’ : ‘28.0.0’]implementation “androidx.drawerlayout:drawerlayout:1.0.0”implementation “androidx.recyclerview:recyclerview:1.0.0”

DrawerLayout and RecyclerView are using the AndroidX packages, but the version numbers are now inline. Also, the versions variable numbers have not been changed. So you’ll need to do a manual update to this:

DrawerLayout和RecyclerView使用的是AndroidX软件包,但是版本号现在是内联的。 同样,版本变量号未更改。 因此,您需要对此进行手动更新:

ext.versions = [    ‘drawer’ : ‘1.0.0’,    ‘rview’ : ‘1.0.0’
]implementation “androidx.drawerlayout:drawerlayout:${versions.drawer}”implementation “androidx.recyclerview:recyclerview:${versions.rview}”

ProGuard和构建脚本 (ProGuard and build scripts)

The migration tool doesn’t automate updates to ProGuard and any associated built scripts. So, if you’re using these and have package names in them, you’ll need to edit these manually.

迁移工具不会自动更新ProGuard和任何关联的内置脚本。 因此,如果您正在使用这些文件,并且其中包含程序包名称,则需要手动进行编辑。

获取最新的稳定版本 (Get the latest stable version)

The migration tool pulls in the most recent release of the AndroidX libraries. As a result, you may end up with an alpha version of some libraries rather than the stable version. For example, the support library version before migration might be:

迁移工具引入了最新版本的AndroidX库。 结果,您可能最终得到某些库的Alpha版本,而不是稳定版本。 例如,迁移之前的支持库版本可能是:

implementation ‘com.android.support:appcompat-v7:28.0.0’

And after migration, you would be using the alpha version of the corresponding AndroidX library:

迁移之后,您将使用相应AndroidX库的Alpha版本:

implementation ‘androidx.appcompat:appcompat:1.1.0-alpha01’

So, if you prefer to use the stable version of the library, you’ll need to manually update the version:

因此,如果您喜欢使用该库的稳定版本,则需要手动更新该版本:

implementation ‘androidx.appcompat:appcompat:1.0.2’

更多帮助和资源 (More help and resources)

You can find more information about this process, the tools, and issues you may encounter on the developer.android.com Migrating to AndroidX page. The page includes an overview of the AndroidX project, a migration guide, and the mapping table of the old Support Library to the new stable and alpha AndroidX versions with the CSV files you need to script your migration.

您可以在developer.android.com 迁移到AndroidX页面上找到有关此过程,工具以及可能遇到的问题的更多信息。 该页面包括AndroidX项目的概述,迁移指南,以及旧的支持库到新的稳定版和Alpha AndroidX版本的映射表,以及用于编写迁移脚本的CSV文件。

Elsewhere, there is also an article detailing the migration to AndroidX in the Plaid example project.

在其他地方,也有一篇文章详细介绍了在Plaid示例项目中向AndroidX迁移

And finally, there is an issue tracker where you see the migration tool issues that the team is working on. You can, of course, report any issues you find with the migration tool here using the button at the top left of the page.

最后,还有一个问题跟踪器 ,您可以在其中查看团队正在处理的迁移工具问题。 当然,您可以使用页面左上方的按钮报告在迁移工具中发现的任何问题。

最后的想法 (Final thoughts)

If you haven’t upgraded to AndroidX, this is a great time to do so and take advantage of the streamlined development that the Jetpack libraries offer your apps. By following the guidance in this article you should find that, in most cases, migration is a straightforward and easy thing to do.

如果您尚未升级到AndroidX,那么这是个不错的时机,并利用Jetpack库为您的应用程序提供的简化开发优势。 通过遵循本文中的指导,您应该发现在大多数情况下,迁移是一件简单而容易的事情。

Do you have thoughts on AndroidX? Let us know in the comments below or tweet using #AndroidStudio and we’ll reply from @AndroidDev, where we regularly share news and tips on how to be successful on Android.

您对AndroidX有想法吗? 在下面的评论中让我们知道,或者使用#AndroidStudio进行鸣叫,我们将回复@AndroidDev,我们在其中定期分享有关如何在Android上取得成功的新闻和提示。

翻译自: https://medium.com/androiddevelopers/migrating-to-androidx-tip-tricks-and-guidance-88d5de238876

迁移到androidx

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值