Google Home 讓 #1 當機的原因減少了 33%
透過集合功能整理內容
你可以依據偏好儲存及分類內容。

Google Home 應用程式可協助設定、管理及控制 Google Home、Google Nest 和 Chromecast 裝置,以及數千種智慧聯網家庭產品,例如燈具、攝影機和溫度控制器。
Google Home 應用程式幕後的工程團隊受益於使用 Kotlin 和 Android Jetpack 程式庫,來提升工程工作效率和開發人員滿意度。
具體措施
Google Home 團隊決定在程式碼集中納入 Kotlin,提升程式設計的效率,並支援使用變數/值、智慧投放、協同程式等現代語言功能。截至 2020 年 6 月,約有 30% 的程式碼集是以 Kotlin 編寫,我們建議所有新功能都使用 Kotlin 開發。
該團隊還採用 Jetpack 程式庫,以提升開發人員的速度,減少對樣板程式碼維護的需求,並減少必要的程式碼量。Jetpack 程式庫也有助於讓程式碼更容易測試,因為功能界線和 API 更清楚明確。
成果
「提高效率並撰寫更少的程式碼,是用 Kotlin 實現『速度』的提升。」- Google Home 軟體工程師 Jared Burrows
與現有的 Java 程式碼相較,改用 Kotlin 後,要求的程式碼數量減少。其中一個範例是使用資料類別和 Parcelize 外掛程式:在 Java 中,以 Java 撰寫的 126 行手寫行現在只需 23 行,就能在 Kotlin 中表示,減少 80%。此外,系統還能自動產生相等和包裹方法,並保持在最新狀態。許多巢狀迴圈和篩選檢查也使用 Kotlin 提供的功能方法簡化。
由於 Kotlin 可將是否可為空值設為語言的一部分,因此避免陷入棘手情況,例如在 Java 中使用是否可為空值註解不一致時,可能會導致發生遺漏的錯誤。自從團隊開始改用 Kotlin 開發新功能後,NullPointerExceptions 的量減少了 33%。由於這是 Google Play 管理中心最常見的當機類型,因此減少這些類型可大幅改善使用者體驗。
隨著 Google Home 等大型成熟版應用程式 (內含超過一百萬行程式碼) 提供,您可以逐步新增 Jetpack 程式庫。透過整合這些方法可讓團隊整合及取代自訂的專屬解決方案,有時即使只有單一程式庫也一樣。由於 Jetpack 程式庫可協助工程師遵循最佳做法且較不詳細 (例如使用 Room 或 ConstraintLayout),因此可讀性也有所提升。團隊考慮到許多較新的 Jetpack 程式庫「必備項目」,包括 ViewModel 和 LiveData,而這兩者已廣泛用於 Google Home 程式碼集。
Google Home 應用程式團隊發現 Jetpack KTX 與 Kotlin 協同程式整合,因此特別實用。現在,該團隊可將協同程式與生命週期感知元件 (例如 ViewModel) 建立關聯,藉此避免複雜的非同步程式設計錯誤。
Java 是 Oracle 和/或其關係企業的註冊商標。
立即開始
進一步瞭解如何以 Kotlin 編寫 Android 應用程式和使用 Android Jetpack 程式庫。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2020-07-13 (世界標準時間)。
[null,null,["上次更新時間:2020-07-13 (世界標準時間)。"],[],[],null,["# Google Home reduces #1 cause of crashes by 33%\n\nThe [Google Home app](https://play.google.com/store/apps/details?id=com.google.android.apps.chromecast.app&hl=en_US)\nhelps set up, manage, and control your Google Home, Google Nest, and Chromecast\ndevices---plus thousands of connected home products like lights, cameras,\nthermostats, and more.\n\nThe engineering team behind the Google Home app benefits from using Kotlin and\nAndroid Jetpack libraries to boost engineering productivity and developer\nhappiness.\n\nWhat they did\n-------------\n\nThe Google Home team decided to incorporate Kotlin into their codebase to make\nprogramming more productive and to enable the usage of modern language features\nlike var/val, smart casts, coroutines, and more. **As of June 2020, about 30% of\nthe codebase is written in Kotlin**, and Kotlin development is encouraged for\nall new features.\n\nThe team also adopted Jetpack libraries to improve developer velocity, decrease\nthe need for boilerplate code maintenance, and reduce the necessary amount of\ncode. Jetpack libraries also helped make their code more testable, since there\nare clearer functional boundaries and APIs.\n\nResults\n-------\n\n*\"Efficacy and writing less code that does more is the 'speed' increase you can\nachieve with Kotlin.\" - Jared Burrows, Software Engineer on Google Home*\n\nSwitching to Kotlin resulted in a reduction in the amount of required code,\ncompared to the equivalent of existing Java code. One example is the use of data\nclasses and the Parcelize plugin: **a class which was 126 hand-written lines in\nJava can now be represented in just 23 lines in Kotlin---an 80% reduction.**\nAdditionally, equality and parcelizing methods can be automatically generated\nand kept up to date. Many nested loops and filtering checks were also simplified\nusing the functional methods available in Kotlin.\n\nBecause Kotlin can make nullability a part of the language, tricky situations\ncan be avoided, like when inconsistent usage of nullability annotations in Java\nmight lead to a missed bug. **Since the team started migrating to developing new\nfeatures with Kotlin, they saw a 33% decrease in NullPointerExceptions** .\nSince this is the [most common crash\ntype on Google Play Console](/topic/performance/vitals/crash#prevent-crashes-null-pointer),\nreducing them led to a dramatically improved user experience.\n\nWith a large, mature app like Google Home---which has over a million lines of\ncode---it's helpful to be able to gradually add Jetpack libraries. Incorporating\nthem allowed the team to consolidate and replace custom tailored solutions,\nsometimes even with a single library. Since Jetpack libraries can help engineers\nfollow best practices and be less verbose (for example, using\n[Room](/training/data-storage/room) or\n[ConstraintLayout](/training/constraint-layout)), readability was increased as\nwell. The team considers many of the newer Jetpack libraries 'must-haves,'\nincluding [ViewModel](/topic/libraries/architecture/viewmodel) and\n[LiveData](/topic/libraries/architecture/livedata), both of which are used\nextensively in the Google Home codebase.\n\nThe Google Home app team found the Jetpack KTX integrations with Kotlin\ncoroutines to be especially helpful. The team is now able to avoid tricky\nasynchronous programming bugs by associating coroutines with lifecycle-aware\ncomponents like [ViewModel](/topic/libraries/architecture/viewmodel).\n\n*Java is a registered trademark of Oracle and/or its affiliates.*\n\nGet Started\n-----------\n\nLearn more about [writing Android apps in Kotlin](/kotlin)\nand [using Android Jetpack libraries](/jetpack)."]]