Google Home でクラッシュの最大の原因を 33% 削減
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。

Google Home アプリは、Google Home デバイス、Google Nest デバイス、Chromecast デバイスをはじめ、さまざまなスマートホーム製品(照明、カメラ、エアコンなど)のセットアップ、管理、操作に役立ちます。
Google Home アプリを支えるエンジニアリング チームは、Kotlin と Android Jetpack ライブラリを使用して、エンジニアリングの生産性とデベロッパーの満足度を高めています。
Google Home の取り組み
Google Home チームは、Kotlin をコードベースに組み込むことで、プログラミングを効率化するとともに、var / val、スマート キャスト、コルーチンなど最新の言語機能を使用できるようにしました。2020 年 6 月現在、コードベースの約 30% が Kotlin で記述されており、新機能についてはすべて Kotlin での開発が推奨されています。
また、Jetpack ライブラリを採用することで、デベロッパーの開発速度向上、ボイラープレート コード管理の必要性の低減、必要なコード量の削減を図りました。Jetpack ライブラリは機能や API の境界が明確なため、コードのテストも容易になりました。
結果
「効率的であることと、少ないコードで多くを実行できることが、Kotlin でスピードアップを実現できる理由です」- Google Home ソフトウェア エンジニア、Jared Burrows
Kotlin に切り替えることで、既存の同等の Java コードに比べ、必要なコードの量を削減できました。データクラスと Percelize プラグインを使用した場合を例に取ると、クラスを作成する際に、Java では 126 行の手動による記述が必要だったのに対し、Kotlin ではわずか 23 行の記述で済みました。実に 80% の削減です。
さらに、等価性と Percelize のメソッドを自動的に生成し、最新の状態に維持できます。ループのネストやフィルタ チェックが多数ある場合も、Kotlin に用意された機能的メソッドを使用すれば簡素化できます。
Kotlin は、言語として null 可能性をサポートしているため、null 可能性アノテーションの使用に一貫性がないためバグが見逃されるといった、Java では陥りがちだった状況を回避できます。Kotlin を使用した新機能の開発に移行して以来、NullPointerExceptions が 33% 減少しました。これは Google Play Console がクラッシュする最大の原因であるため、その低減によりユーザー エクスペリエンスが大幅に向上しました。
Google Home のような大規模(100 万行を超えるコードが含まれます)で成熟したアプリにとって、段階的に追加していける 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 UTC。
[null,null,["最終更新日 2020-07-13 UTC。"],[],[],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)."]]