Google Home 应用团队将因首要原因导致的崩溃次数减少了 33%
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。

Google Home 应用可帮助您设置、管理和控制 Google Home、Google Nest 和 Chromecast 设备,以及数千种智能互联家居产品(如灯具、摄像头、恒温器等)。
Google Home 应用背后的工程团队使用 Kotlin 和 Android Jetpack 库来提升工程生产力和开发者满意度,这让他们受益良多。
他们做了什么
Google Home 团队决定将 Kotlin 整合到代码库中,以提升编程效率,并使用现代语言功能(如 var/val、智能投射、协程等)。截至 2020 年 6 月,30% 左右的代码库是使用 Kotlin 编写的,我们建议使用 Kotlin 来开发所有新功能。
该团队还采用 Jetpack 库来提高开发者的速度、降低对样板代码维护的需求并减少必要的代码量。Jetpack 库还有助于提高代码的可测试性,因为有更清晰的功能边界和 API。
成果
“Kotlin 可助您提高工作效率,让您编写更少的代码来实现更多的功能,这就是您可实现的‘速度’提升。”- Jared Burrows,Google Home 的软件工程师
与等效的现有 Java 代码相比,改用 Kotlin 可以减少所需的代码量。例如,使用数据类和 Parcelize 插件:一个在使用 Java 时需要手动编写 126 行代码的类,现在使用 Kotlin 只需编写 23 行,代码量减少了 80%。此外,还可以自动生成并及时更新均衡和解析方法。使用 Kotlin 中提供的实用方法,还可以简化许多嵌套循环和过滤检查。
由于 Kotlin 可以将是否可为 null 作为语言的一部分,因此可以避免棘手的情况,例如在 Java 中不一致地使用是否可为 null 注释可能会导致遗漏错误。该团队开始使用 Kotlin 开发新功能后,NullPointerException 减少了 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 和/或其关联公司的注册商标。
最后更新时间 (UTC):2020-07-13。
[null,null,["最后更新时间 (UTC):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)."]]