Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Kotlin導入 5つのステップ
Search
Koji Wakamiya
January 21, 2019
Programming
1
1.8k
Kotlin導入 5つのステップ
「集まれKotlin好き!Kotlin愛好会 vol7」の発表資料です。
Kotlinを既存プロダクトに導入した際、重要だなと思ったポイントを5点紹介します。
Koji Wakamiya
January 21, 2019
Tweet
Share
More Decks by Koji Wakamiya
See All by Koji Wakamiya
Flutterコントリビューションのススメ
d_r_1009
1
950
人気サービスをFlutter Webでリプレースするとどうなるのか
d_r_1009
0
1.1k
モバイルアプリケーション 開発組織の“学び”
d_r_1009
0
370
Add-to-appの戦い方
d_r_1009
0
1.7k
開発チーム主導で iOSの新機能に対応する 3つのポイント
d_r_1009
0
660
FlutterKaigi2021
d_r_1009
0
1.8k
Flutter?
d_r_1009
0
430
Room2.1 DatabaseView あるある
d_r_1009
0
700
AAC Paging & Kotlin化の紹介
d_r_1009
0
1.3k
Other Decks in Programming
See All in Programming
Combinatorial Interview Problems with Backtracking Solutions - From Imperative Procedural Programming to Declarative Functional Programming - Part 2
philipschwarz
PRO
0
130
Unicodeどうしてる? PHPから見たUnicode対応と他言語での対応についてのお伺い
youkidearitai
PRO
0
370
re:Invent 2025 トレンドからみる製品開発への AI Agent 活用
yoskoh
0
580
AIエージェントの設計で注意するべきポイント6選
har1101
6
3k
Patterns of Patterns
denyspoltorak
0
420
Graviton と Nitro と私
maroon1st
0
160
AIで開発はどれくらい加速したのか?AIエージェントによるコード生成を、現場の評価と研究開発の評価の両面からdeep diveしてみる
daisuketakeda
1
530
AI時代を生き抜く 新卒エンジニアの生きる道
coconala_engineer
1
520
Giselleで作るAI QAアシスタント 〜 Pull Requestレビューに継続的QAを
codenote
0
330
[AI Engineering Summit Tokyo 2025] LLMは計画業務のゲームチェンジャーか? 最適化業務における活⽤の可能性と限界
terryu16
2
260
AI前提で考えるiOSアプリのモダナイズ設計
yuukiw00w
0
210
Python札幌 LT資料
t3tra
7
1.1k
Featured
See All Featured
A Tale of Four Properties
chriscoyier
162
24k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
61
Darren the Foodie - Storyboard
khoart
PRO
1
2.1k
What does AI have to do with Human Rights?
axbom
PRO
0
1.9k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.8k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
41
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.2k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
420
New Earth Scene 8
popppiees
0
1.3k
A better future with KSS
kneath
240
18k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
110
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.3k
Transcript
Kotlin導入 5つのステップ
Koji Wakamiya Work : Studyplus, Inc. Github : @koji-1009 Twitter
: @D_R_1009 2
Kotlin std libを考える 環境から確認する 1
minSDK を見る 環境に合わせた適切なstdを選ぶ! ※minSDK21以上ならほとんど影響なし 4
SDK16 + Kotlin? • API16ならstdライブラリを利用すべき ◦ Android4系のテストは厳しい ◦ SDKはminSDKが利用アプリに影響する •
API19以上なら(現状は)std-jdk7で問題なさそう ◦ “AutoClosable”APIがAPI19からサポート ◦ “try-with-resource”は全てのAPiでサポート 5
Rx-Streamを置き換える 必要なものを選り分ける 2
Java7以下でListのStreamAPI • “for + if” を “filter” で • “flatMap”
で入れ子リストの操作 Rx-Sreamの 利用シーン サーバーAPIへのアクセス • HTTP GETをSingleで • HTTP 204をCompletableで 7
RxJavaは必要? • Observe.fromIterableをKotlinのList.forEachに • SingleによるGET/POST処理をasync/awaitに • RxAndroidによるUIスレッド指定(するなら)runBlockingに Rxが必要な箇所を洗い出すことで、 強力なRxの処理を十分に検討して利用できるように 8
Enum + Kotlin Extension 定数定義と定数定義を利用する処理を切り分ける 3
Enumの役割 - パターンの列挙 - When文との組み合わせによる 処理分岐 Enumを純粋な列挙 にする Enumで実行したい事柄 -
パターンに応じた文字列 - Enumの表現に合わせた 表示/非表示の組み合わせ 10
例:UserTypeを定義する enum UserType(val code: String) { FREE(“free”), PREMIUM(“premium”) } fun
UserType.stringResId() = when(this) { UserType.FREE -> R.string.plan_free // 無料プラン USerType.PREMIUM -> R.string.plan_premium // 課金プラン else -> R.string.other // その他 } 11
利用しやすいEnum • 要素の追加、削除がしやすいと利用しやすい ◦ 列挙項目の増減時に考えることが少ないこと ◦ アプリ内で1つの概念を1つのEnumで表現できること • フィールドに過不足がない ◦
「とりあえずnull」の必要がない ◦ “R.hoge.hugahuga”による改行が発生しない 12
2つのKotlin Data Class 通信処理用のクラスと内部処理用の2つのData Class 4
API変換用 • プリミティブ型で構成 • Proguardを考慮 2つのData Class を 使い分ける アプリロジック用
• アプリの内部クラスも利用 • Proguardは考慮しない 14
APIレスポンスから Data Classを作る 15 JSON Data Class
オブジェクトの目的 16 JSON Data Class Data Class Server API Response
サーバー処理の結果 DBからの取得結果 JSON Parsed Class APIレスポンスを JVM上で簡単に扱う In-App Data アプリのロジックや UI表示に対応
オブジェクトの特徴 17 JSON Data Class Data Class Server API Response
パラメータの追加/削除 に対応しやすい JSON Parsed Class APIレスポンスの更新に 対応しやすい In-App Data アプリロジックの更新に 対応しやすい
アプリメリット • equal判定が可能なため比較や並べ替えが容易 ◦ RecyclerView + ListAdapterによる差分更新 • 変換が容易 ◦
Data Classから別のData Classを作成して扱う ◦ Entity AnnotationをつけてRoomで扱う 18
Coroutines + Result Kotlin Coroutinesの結果をUIに伝播させる 5
通信状態をUIに反映 • ユーザーに次の動作を伝えるために必要 ◦ 通信中か通信中ではないか ◦ 通信は成功したか失敗したか 非同期処理の状態をActivity/Fragmentにどう引っ張ってくるか 20
Resource • status(Status), msg(String?), data(T?) • Statusの定義 ◦ success ◦
error ◦ loading AACサンプル NetworkState • status(Status), msg(String?) • Statusの定義 ◦ LOADED ◦ LOADING ◦ error 21 https://github.com/googlesamples/android-architecture-components/blob/master/GithubBrowserSample/app/src/main/java/com/android/example/github/vo/Resource.kt https://github.com/googlesamples/android-architecture-components/blob/master/PagingWithNetworkSample/app/src/main/java/com/android/example/paging/pagingwithnetwork/reddit/repository/NetworkState.kt
A discriminated union that encapsulates successful outcome with a value
of type T or a failure with an arbitrary Throwable exception. 22 kotlin-stdlib/ kotlin/Result
launch { state.postValue(State.LOADING) // 通信開始 runCaching { repository.user() // Coroutinesによる通信
}.fold( onSuccess = { user.post(it) // 通信結果 state.postValue(State.LOADED) // 通信成功 }, onFailure = { state.postValue(State.error(it)) // 通信失敗 } } ※StateクラスはNetworkStateクラスのThrowable?を保持する独自拡張クラス 23
+ LiveDataを推進 • ViewModel ◦ Model層から受け取るCoroutinesをResultで表現 ◦ Resultの結果をLiveDataに変換 • View
◦ ViewModel層からLiveDataでデータを取得 ◦ 正常/異常の状態に応じたUIを提供 24
“ Kotlinでもっと便利に! 25
26 Thanks! Any questions? You can find me at @koji-1009
& koji,
[email protected]
Credits Special thanks to all the people who made and
released these awesome resources for free: • Presentation template by SlidesCarnival • Photographs by Unsplash 27