使用 MVVM 和 Jetpack 库在 Android 上重构 Duolingo
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
多邻国 (Duolingo) 是一款风靡全球的语言学习应用,它能够让过去人们觉得棘手的功能变得更加轻松和有趣。这种轻松愉快的语言体验需要源源不断地进行新功能和实验,还需要一款能提供所有新功能和实验的流畅运行的应用。对 Duolingo 来说,如果在世界上的任何地方安装应用无响应的应用,学习者可能会因此而打消学习的念头。这让他们致力于打造卓越的应用,尤其是在 60% 的学员使用的 Android 设备上,包括其首席执行官,他们通过入门级手机跟踪应用。因此,当 Duolingo 的 Android 开发团队注意到“应用无响应”错误、丢帧甚至收到手写投诉时,他们立即采取行动。
他们很快发现了应用性能问题的根源:现有的软件架构未随着团队规模的扩大而实现良好的扩缩。必要更新会导致应用性能下降,从而造成痛苦。他们的开发者将更多时间投入到修复 bug 上,而没有投入更多时间来推出新的创收功能。他们的过时软件架构开始成为一种不可接受的阻碍,对他们、团队速度,最重要的是影响用户体验。他们需要彻底重建代码库。
他们是如何做到的
起初,团队内部也一片混乱。现在这是他们唯一要优先考虑的问题吗?其中一类用户认为继续推出新功能和提高收入更重要,而另一类用户则认为,将注意力集中在 Android 重新启动上是未来的发展方向。最终,他们达成了共识:在与退步作斗争时加大的努力有可能会彻底破坏他们的路线图。他们必须全力以赴。
该团队最终确定了一款越来越受欢迎的解决方案。他们决定使用 Model-View-ViewModel 重写应用,Model-View-ViewModel 是 Google 支持的一种软件模式,可让开发者通过明确分离关注点来简化应用架构。借助 MVVM,他们可将图形界面(视图)的开发与业务逻辑(模型)的开发分离开来。他们可以建立明确且已商定的模式,从而更轻松地调整自己的方法以引入新功能,并引导新加入的开发者加入进来。
通过将每项功能拆分成各自的模块化部分,该团队能够快速恢复工作效率,因为他们可以分配多个小组并行处理每项功能,在减少错误的同时提高速度。
他们利用 Android Jetpack(包括 Dagger 和 Hilt)中的库实现了这种新架构,以帮助编写可在各种 Android 版本和设备中一致运行的代码。这两项增加使他们能够打造更好的封装功能,并更高效地利用 Android 的内置模块。
成果
这些表现显著提升了学习者在 Android(尤其是入门级设备)上的体验。他们还打造了一款响应更灵敏的应用,在旗舰设备上提供了更流畅的动画。每日“应用无响应”或 ANR 发生率下降了 41%。应用低于目标帧速率的时间百分比降低了 28%。最重要的是,用户在滚动浏览关键屏幕时,速度提升了 40%。
手持笔记的 ANR 发生率降低了 41%,帧速率提升了 28%,体验速度提升了 40%。
整个重新启动过程花了 8 周时间,让所有“多邻国”学习者都能从该应用中获得显著的吸引力和愉悦的体验。在这六个月里,该团队没有记录到任何明显的新性能下降情况,这让他们能够再次专注于开发可带来收入的功能。团队集中精力提升质量的决定取得了良好成效。
多邻国对使命的全力奉献使其成为语言学习领域的全球热门应用。他们致力于打造卓越应用,在不影响无障碍功能的前提下打造先进的教育体验,这是他们的决定因素。
开始使用
如果您想深入了解 Duolingo 如何集成 Jetpack 的库,以及 Model-View-ViewModel 如何改进其应用,请阅读我们面向开发者的技术案例研究。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2021-08-19。
[null,null,["最后更新时间 (UTC):2021-08-19。"],[],[],null,["# Duolingo refactors on Android with MVVM and Jetpack libraries\n\nDuolingo is the world's most popular language learning app because it's managed to make something people have found daunting in the past feel easy and fun. This breezy language experience requires a constant stream of new features and experiments --- and a smooth-running app that can deliver all of them. To Duolingo, an unresponsive app on a device anywhere in the world could mean a potentially discouraged learner. This commits them to app excellence, particularly on the Android devices used by sixty percent of their learners, including their CEO, who keeps track of the app from an entry-level phone. And so, when Duolingo's Android development team noticed an increase in \"App not Responding\" errors, dropped frames --- and even received hand-written complaints --- they took action immediately.\n\nThey soon uncovered the source of the app's performance issues: the existing software architecture wasn't scaling well with the growth of their team. Necessary updates were causing painful performance regressions on the app. Their developers were devoting more time to fixing bugs, and less to shipping new revenue-generating features. Their outdated software architecture was starting to become an unacceptable drag --- on them, their team velocity, and, above all, on their users' experience. They needed to rebuild their codebase from the ground up.\n\nHow they did it\n---------------\n\nAt the beginning, the team was torn. Should this be their sole priority now? One group felt that it was more important to continue shipping new features and driving revenue, while the other believed that focusing all their attention on an Android reboot was the way forward. Eventually, they reached the consensus that the increasing effort of fighting regressions risked derailing their road map entirely. They had to go all-in.\n\nThe team settled on an increasingly popular solution. They decided to rewrite their app\nusing [Model-View-ViewModel](https://developer.android.com/jetpack/guide?gclid=Cj0KCQjw6s2IBhCnARIsAP8RfAj6I4mhRRdSqjYfPlvGET9S6mmRGJrc8Inkc-cCe1IVdTDEvlE3hFEaApHkEALw_wcB&gclsrc=aw.ds), a software pattern supported by Google that allows developers to streamline an app's architecture by creating clear separation of concerns. With MVVM, they could separate the development of the graphical user interface (the view) from the development of the business logic (the model). They could establish clear and agreed upon patterns, making it easier to both align their approach to new features and onboard incoming developers.\n\nBreaking each feature into its own modular piece allowed the team to quickly regain productivity, as they could assign small groups to work on each feature in parallel, increasing velocity while reducing errors.\n\nThey implemented this new architecture with libraries from Android's [Jetpack](https://developer.android.com/jetpack), including [Dagger](https://developer.android.com/training/dependency-injection/dagger-basics) and [Hilt](https://developer.android.com/training/dependency-injection/hilt-android), to help them write code that works consistently across Android versions and devices. These two additions enabled them to create better encapsulated features and [utilize Android's built-in modules more efficiently](https://developer.android.com/training/dependency-injection).\n\nResults\n-------\n\nThese performance gains significantly improved learners' experience on Android and in particular on entry-level devices. They also led to a more responsive app with smoother animations on flagship devices. The daily \"App Not Responding\" or ANR rate dropped 41%. The percentage of time that the app fell below its target frame rate decreased by 28%. Most importantly, their users experienced a 40% increase in speed when scrolling through key screens.\n\n\n*Hands holding notes reading 41% less ANRs, 28% improved frame rate and 40% faster experience.*\n\n\u003cbr /\u003e\n\nThe whole reboot took eight weeks and made the app considerably more engaging and delightful for all Duolingo learners. In the six months since, the team has recorded no significant new performance regressions, allowing them to focus again on shipping revenue-generating features. The decision to focus the team's efforts on quality had paid off.\n\nDuolingo's dedication to their mission made them the world's top app in the language learning space. Their commitment to app excellence --- creating cutting edge educational experiences without compromising accessibility --- is what kept them there.\n\nGet started\n-----------\n\nIf you feel like diving into the nitty-gritty of how Duolingo integrated Jetpack's libraries, and how Model-View-ViewModel improved their app, read our [technical case study](https://android-developers.googleblog.com/2021/08/android-app-excellence-duolingo.html) for developers."]]