首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >提示[译] Plaid 应用迁移到 AndroidX 的实践经历

提示[译] Plaid 应用迁移到 AndroidX 的实践经历

作者头像
Android 开发者
发布于 2019-05-17 07:16:46
发布于 2019-05-17 07:16:46
1.2K00
代码可运行
举报
文章被收录于专栏:Android 开发者Android 开发者
运行总次数:0
代码可运行

一份 AndroidX 的迁移指南

Virginia Poltrack 提供图片。

Plaid 是一款呈现 Material Design 风格和丰富交互界面的有趣应用。最近这款应用通过现今的 Android 应用开发技术实现了一番重构。获取更多应用信息和重新设计的视觉效果,可以查阅 Restitching Plaid

和大多数 Android 应用一样,Plaid 依赖 Android Support Library,该库可以为新 Android 特性提供向后兼容性,以便可以运行在旧版操作系统的 Android 机上。在 2018 年的 9 月份,最新的 Support Library 版本(28.0.0)被发布,和 Support Library 一起发布的 Android 库已经被迁移到 AndroidX(除了 Design 库被迁移到 Android 的 Material Components),并且这些库的新增开发都是基于 AndroidX。因此,接收 bug 修复、新功能和其他库更新的唯一选择就需要将 Plaid 迁移到 AndroidX。

什么是 AndroidX?

在 2018 Google I/O 大会上,Android 团队发布了 AndroidX。它是 Android 团队用于开发、测试、打包、定版以及在 Jetpack 中发布库时所用到的开源代码。和 Support Library 类似,每一个 AndroidX 库都是独立于 Android OS 来发布,并且提供了跨 Android 版本的向后兼容性。它是对 Support Library 的重大改进和全面替代方案。

阅读下文来了解我们如何为迁移过程准备自己的代码,以及执行迁移过程。

迁移前准备

我强烈建议在一个版本可控的分支做迁移工作。这样你可以逐步解决可能出现的任何迁移问题,同时分离出每个变更用于分析定位问题。你可以在这个 Pull Request 下查看我们的讨论过程,并且通过点击下面的提交链接来跟进最新信息。另外 Android Studio 提供了一个迁移前做工程备份的可选服务。

和任何大规模代码的重构工作一样,最好在迁移到 AndroidX 期间,迁移分支与主要开发分支之间做到最少合并来避免合并冲突。虽然对其他应用来说不可行,但是我们团队能够临时暂停向主分支提交代码以帮助迁移。一次性迁移整个应用也非常必要,因为部分迁移——同时使用 AndroidX 和 Support 库将会导致迁移过程中的失败。

最后,请阅读 developer.android.com 网站上迁移至 AndroidX 文中的提示。现在让我们开始吧!

依赖标识

在你开始之前,对代码准备的最重要的一点建议是:

确保你正在使用的依赖库是与 AndroidX 兼容的。

依赖于一个旧版 support 库的第三方库可能与 AndroidX 不兼容,这很有可能导致你的应用在迁移到 AndroidX 后无法编译。检查你的应用任意依赖是否兼容的一个方法是访问这些依赖的项目站点。一个更直接的方法是开始迁移,并且检查可能出现的报错。

对于 Plaid 应用,我们使用了一个与AndroidX 不兼容的图形加载库 Glide 的旧版本(4.7.1)。这导致迁移后出现一个让应用无法构建的代码生成问题(这是一个记录在 Glide 工程下的类似问题),在开始迁移之前我们把 Glide 更新到版本 4.8.0(参考这次提交),这个版本添加了对 AndroidX 注解的支持。

关于这一点,请尽可能地更新到你的应用所依赖第三方库的最新版本。这对 Support 库而言尤其是一个好主意,因为升级到 28.0.0(截至撰写本文的最终版本)将使迁移更加顺畅。

使用 Android Studio 进行重构

迁移过程中我们使用了 Android Studio 3.2.1 版本中内置的重构工具。 AndroidX 迁移工具位于菜单栏的 Refactor > Migrate to AndroidX 选项。这个选项将迁移整个项目的所有模块。

运行 AndroidX 重构工具后的预览窗口。

如果你不使用 Android Studio 或者更倾向于其他工具来做迁移,请参考 ArtifactClass 来对比新旧支持库间架构和类的改动,这些材料也有提供 CSV 格式。

Android Studio 中的 AndroidX 迁移工具是 AndroidX 迁移的主要方式。这个工具正在持续的优化中,所以如果你遇到问题或者希望查看某个功能,请在 Google 问题追踪页提交一票

迁移应用

变更最少的代码以保证应用可以仍能正常运行。

在运行 AndroidX 迁移工具后,大量的代码被变更,然而项目却无法编译成功。此时,我们仅仅做了最少量的工作来使应用重新运行起来。

这个方法有利于把流程拆解为可控的步骤。我们留下了一些任务,诸如修复导入顺序、提取依赖变量、减少完整 classpath 的使用,以便后续的清理工作。

刚开始出现的报错之一是重复的类 —— 像这种情况,PathSegment

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.

> com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives:

如何解决这个问题参考这里: https://developer.android.com/studio/build/dependencies#duplicate_classes.

Program type already present: androidx.core.graphics.PathSegment
复制代码

这是一个由迁移工具生成错误依赖(androidx.core:core-ktx:0.3)导致的报错。我们手动更新(参考这次提交)到正确的依赖版本(androidx.core:core-ktx:1.0.0)。这个bug 已经在 Android Studio 3.3 Canary 9 及之后的版本被修复。我们指出这点是因为你或许在迁移过程中会遇到类似的问题。

接下来,Palette API 在新版中变得可以为空,为了暂时避开(参考这次提交)这点,我们添加了!!非空断言操作符)。

然后我们遇到了一个 plusAssign 缺失的报错。这个加载在 1.0.0 版本中被移除。plusAssign 的使用被临时注释掉了(参考这次提交)。本文的后面我们会研究对 PaletteplusAssign 问题的可持续解决方案。

现在应用可以运行了,到清理代码的时候了!

清理代码

应用在运行中,但是我们的持续集成系统报告了代码提交后的构建错误:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Execution failed for task ':designernews:checkDebugAndroidTestClasspath'.

> Conflict with dependency 'androidx.arch.core:core-runtime' in project ':designernews'. 

Resolved versions for runtime classpath (2.0.0) and compile classpath (2.0.1-alpha01) differ. This can lead to runtime crashes. 

To resolve this issue follow advice at https://developer.android.com/studio/build/gradle-tips#configure-project-wide-properties.

Alternatively, you can try to fix the problem by adding this snippet to /.../plaid/designernews/build.gradle:

  dependencies {
    implementation("androidx.arch.core:core-runtime:2.0.1-alpha01")
  }
复制代码

我们依照测试日志中的参考建议,添加了缺失的依赖模块(参考这次提交)。

我们也借此机会更新了我们的 Gradle 插件版本、Gradle wrapper 版本、Kotlin 版本(参考这次提交)。Android Studio 推荐我们安装 28.0.3 版本的构建工具,我们也照做了。在使用 Gradle 3.3.0-alpha13 版本插件时我们遇到的问题,通过降级到 3.3.0-alpha8 版本的方式得到解决。

迁移工具的一个缺点是:如果你在依赖版本项使用了变量,迁移工具把它们自动内联。我们从 build.gradle 文件中重新提取了这些版本(参考这次提交)。

上文中我们提到了运行 AndroidX 迁移工具后对 plusAssignPalette 问题的临时解决方案。我们通过将 AndroidX 版本降低来重新添加了 plusAssign 函数和相关测试(参考这次提交),并且恢复了被注释了的代码。与此同时,我们把 Palette 参数更新到可以为空的这个版本(参考这次提交),这样就无需使用操作符 !!

同样的,自动转化可能使得某些类需要使用它们的完整类路径。做最少的手工修正是一个好的思路。作为清理工作的一部分,我们移除了完整类路径,并在必要时重新添加了相关引用。

最后,一些少量测试相关的修改被加入工程,围绕着测试过程中的依赖冲突(参考这次提交)和 Room 的测试用例(参考这次提交)。这时我们的工程完成全部转化,并且我们的测试都已通过。

结束过程

尽管遇到了一些障碍,AndroidX 的迁移进展得比较顺利。遇到的问题主要涉及依赖库或类的错误转换,以及新库中的 API 变化。 幸运的是这些都相对容易解决。Plaid 现在已经准备好再被用起来了!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019年05月16日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
LeetCode 134. Gas Station题目分析
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations. Return the starting gas station's index if you can travel around the circuit once, otherwise return -1. Note:The solution is guaranteed to be unique. Subscribe to see which companies asked this question. 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油gas[i],并且从第i个加油站前往第i+1个加油站需要消耗汽油cost[i]。 你有一辆油箱容量无限大的汽车,现在要从某一个加油站出发绕环路一周,一开始油箱为空。 求可环绕环路一周时出发的加油站的编号,若不存在环绕一周的方案,则返回-1。 注意事项 数据保证答案唯一。 样例 现在有4个加油站,汽油量gas[i]=[1, 1, 3, 1],环路旅行时消耗的汽油量cost[i]=[2, 2, 1, 1]。则出发的加油站的编号为2。
desperate633
2018/08/22
7570
图论--网络流--最小割 HDU 2485 Destroying the bus stations(最短路+限流建图)
Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “impossible” mission ----- to make it slow for the army of City Colugu to reach the airport. City Colugu has n bus stations and m roads. Each road connects two bus stations directly, and all roads are one way streets. In order to keep the air clean, the government bans all military vehicles. So the army must take buses to go to the airport. There may be more than one road between two bus stations. If a bus station is destroyed, all roads connecting that station will become no use. What’s Gabiluso needs to do is destroying some bus stations to make the army can’t get to the airport in k minutes. It takes exactly one minute for a bus to pass any road. All bus stations are numbered from 1 to n. The No.1 bus station is in the barrack and the No. n station is in the airport. The army always set out from the No. 1 station. No.1 station and No. n station can’t be destroyed because of the heavy guard. Of course there is no road from No.1 station to No. n station. Please help Gabiluso to calculate the minimum number of bus stations he must destroy to complete his mission.
风骨散人Chiam
2020/10/28
4070
PAT 1033 To Fill or Not to Fill (25分) 贪心思想
With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.
vivi
2020/07/14
7220
回溯/贪心高频题
"有关递归的算法,都离不开“树”的遍历这一抽象模型。只不过对于不同的算法,在前(中)后序遍历的时候,所做的事不同而已。 "
王脸小
2019/10/28
1.5K0
【CodeForces】638A - Home Numbers(水)
The main street of Berland is a straight line with n houses built along it (n is an even number). The houses are located at both sides of the street. The houses with odd numbers are at one side of the street and are numbered from 1 to n - 1 in the order from the beginning of the street to the end (in the picture: from left to right). The houses with even numbers are at the other side of the street and are numbered from 2 to n in the order from the end of the street to its beginning (in the picture: from right to left). The corresponding houses with even and odd numbers are strictly opposite each other, that is, house 1 is opposite house n, house 3 is opposite house n - 2, house 5 is opposite house n - 4 and so on.
FishWang
2025/08/26
1190
【CodeForces】638A - Home Numbers(水)
HDUOJ----2485 Destroying the bus stations(2008北京现场赛A题)
Destroying the bus stations                                                                                     Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)                                                            
Gxjun
2018/03/22
5590
【CodeForces】703C - Chris and Road(思维,好题)
And while Mishka is enjoying her trip...
FishWang
2025/08/26
1790
【CodeForces】703C - Chris and Road(思维,好题)
Codeforces Round #491 (Div. 2)部分题解
这场比赛好鬼畜啊,,A题写崩了wa了4遍,心态直接爆炸,本来想弃疗了,结果发现BCD都是傻逼题。。 A. If at first you don't succeed...(容斥原理) 题目大意: 有$N$个人参加了考试,考试完成后在通过的人中,有$A$个人去了第一个酒店聚会,有$B$个人去了第二个酒店聚会,有$C$个人同时去了两个酒店聚会。 问有多少个人没有通过考试(主角没有通过考试) Sol 小学生容斥,参加了聚会的肯定有$A+B-C$个人,用$N$减去这个数就是不合格的喽 一开始wa了4发心态爆炸然后加
attack
2018/07/04
4170
CodeForces - 1245 D - Shichikuji and Power Grid
Shichikuji is the new resident deity of the South Black Snail Temple. Her first job is as follows:
风骨散人Chiam
2020/10/28
6000
CodeForces - 1245 D - Shichikuji and Power Grid
codeforces 349B(贪心)
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya’s house. Igor thinks that the larger the number is, the more chance to win Tanya’s heart he has.
dejavu1zz
2020/10/23
3000
Codeforces 839C Journey【DFS】
C. Journey time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the
Angel_Kitty
2018/04/09
9250
Codeforces 839C Journey【DFS】
R语言二手车汽车销售数据可视化探索:预处理、平滑密度图、地理空间可视化
本文用爬虫采集了汽车销售数据(查看文末了解数据获取方式),后来对其进行了扩展,创建这个数据集,其中包括境内的所有二手车辆或者经销商车辆条目数据(点击文末“阅读原文”获取完整代码数据)。
拓端
2023/08/31
3790
R语言二手车汽车销售数据可视化探索:预处理、平滑密度图、地理空间可视化
CodeForces 666B World Tour(spfa+枚举)
B. World Tour time limit per test 5 seconds memory limit per test 512 megabytes input standard input output standard output A famous sculptor Cicasso goes to a world tour! Well, it is not actually a world-wide. But not everyone should have th
ShenduCC
2018/04/26
5960
CodeForces 666B World Tour(spfa+枚举)
CodeForces 24A Ring road(dfs)
A. Ring road time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams
ShenduCC
2018/04/27
6890
Codeforces Round #179 (Div. 2)A、B、C、D
n个元素的数组,每个元素不超过1000,可以交换相邻两个元素,问是否可以在有限次的操作之后使得相邻两个元素的值不相同。
xindoo
2021/01/22
2940
2020 Multi-University Training Contest 6
给定 n 个数,每个数有价值 val[i] 。随机选择 i\leq j 的 (i,j) 点对,问下标 i 到 j 的子数组价值和的平均数的期望。
wenzhuan
2022/08/15
3980
最短路专题1 | CodeForces 601A - 混合Dijkstra算法
这个十一没有出去玩,花了一些时间在写之前提过的 markdown 编辑器,本文就是用这个编辑器写的 2333,今天准备写咱们的新专题 — 最短路。另外之前提过专题的题目主要使用 kuangbin 系列,现在改变主意了,专题题目全部使用 CodeForces 上的题目,原因主要是 POJ 等国内的 OJ 系统不能看源代码,而且题目质量稍微欠缺一些,然后没有区分度。
ACM算法日常
2019/10/09
5240
最短路专题1 | CodeForces 601A - 混合Dijkstra算法
CF思维联系– CodeForces - 991C Candies(二分)
After passing a test, Vasya got himself a box of n candies. He decided to eat an equal amount of candies each morning until there are no more candies. However, Petya also noticed the box and decided to get some candies for himself.
风骨散人Chiam
2020/10/28
5460
【CodeForces 602C】H - Approximating a Constant Range(dijk)
In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. There is also an absurdly simple road network — for each pair of different towns x and y, there is a bidirectional road between towns x and yif and only if there is no railway between them. Travelling to a different town using one railway or one road always takes exactly one hour.
饶文津
2020/05/31
5130
ZOJ 3635 Cinema in Akiba(线段树)
Cinema in Akiba (CIA) is a small but very popular cinema in Akihabara. Every night the cinema is full of people. The layout of CIA is very interesting, as there is only one row so that every audience can enjoy the wonderful movies without any annoyance by other audiences sitting in front of him/her.
全栈程序员站长
2022/07/07
2170
相关推荐
LeetCode 134. Gas Station题目分析
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验