在 Android 上使用 MVVM 和 Jetpack 程式庫進行重構
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
《多鄰國 (Duolingo)》是世界上最受歡迎的語言學習應用程式,因為這款應用程式能讓人過去覺得輕鬆有趣。這種流暢的語言體驗需要不斷推出新功能和實驗,以及執行流暢、執行以上所有功能的應用程式。多虧了多端裝置上的應用程式,《多鄰國 (Duolingo)》才會沒有回應,代表學習者可能會感到不快。因此他們致力展現卓越的應用程式體驗,特別是有 60% 學員 (包括執行長) 透過入門級手機追蹤應用程式的 Android 裝置。因此,當多鄰國的 Android 開發團隊發現「應用程式無回應」錯誤、影格遺失,甚至是收到手寫申訴,就會立即採取行動。
他們很快就發現了應用程式效能問題的根源:現有的軟體架構未能隨著團隊成長而調整規模。應用程式必須完成必要的更新,造成應用程式效能出現劇烈的迴歸問題。他們的開發人員花了更多時間修正錯誤,也減少推出可產生收益的全新功能。他們過舊的軟體架構,越來越難受到嚴峻的拖延,除了對他們的團隊速度造成影響,也最重要的是,讓使用者享有更佳的體驗。他們需要從頭開始重新建構程式碼集。
如何做到這點
早期,這個團隊便已互相干擾。請問這是他們目前的優先順序嗎?有位機構認為,持續推送新功能和提升收益比以往更為重要,另一名則是將重心全心放在 Android 重新啟動上。最終,他們達成共識,更加努力對抗迴歸,避免他們全面停用道路地圖。他們必須全力以赴
團隊決定採用越來越多熱門的解決方案,他們決定使用 Model-View-ViewModel 重新編寫應用程式,這是 Google 支援的軟體模式,可讓開發人員建立明確的關注點分離,藉此簡化應用程式的架構。使用 MVVM 時,他們可以將圖形使用者介面 (檢視畫面) 的開發與商業邏輯 (模型) 的開發分開。他們可以製定明確且議定的模式,讓自己能夠更輕鬆地協調新功能,並培訓新進開發人員。
將各項功能拆分為各自的模組化架構後,團隊就能迅速提升工作效率,因為團隊可以將每項功能指派給不同的群組,讓這些群組同時處理各項功能,進而加快作業速度並減少錯誤。
我們以 Android 的 Jetpack 程式庫實作這個新架構,包括 Dagger 和 Hilt,以協助開發人員編寫在各種 Android 版本和裝置中都能維持一致的程式碼。上述兩個新增項目可讓他們建立更封裝的功能,並更有效率地使用 Android 內建模組。
成果
這些效能大幅提升了學員的 Android 使用體驗,尤其是在入門級裝置上。並推出更回應式的應用程式,以及旗艦裝置上的動畫更流暢。每日「應用程式無回應」或 ANR 發生率下降了 41%,應用程式低於目標影格速率的時間百分比下降 28%。最重要的是,當使用者捲動瀏覽主要畫面時,速度會提升 40%。
使用者留下筆記,讀取 ANR 次數減少 41%、影格速率提高 28%,運作速度加快 40%。
整個重新啟動花了八週,對所有《多鄰國》學員而言,這款應用程式的吸引力和愉悅的使用體驗。在此後的六個月內,該團隊至今沒有顯著的全新效能迴歸問題,幫助他們再次專心開發可產生收益的功能。他們將重心放在品質上的努力,獲得了回報。
多鄰國 (多鄰國) 付出的努力不懈,也因此成為全球最頂尖的語言學習應用程式。他們致力打造卓越的應用程式體驗,也就是在不犧牲無障礙功能的情況下,打造頂尖的教育體驗。
開始使用
如想深入瞭解 Duolingo 如何整合 Jetpack 的程式庫,以及 Model-View-ViewModel 如何改善應用程式,請參閱開發人員專用的技術個案研究。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2021-08-19 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間: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."]]