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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Koji Wakamiya
January 21, 2019
Programming
1.8k
1
Share
Kotlin導入 5つのステップ
「集まれKotlin好き!Kotlin愛好会 vol7」の発表資料です。
Kotlinを既存プロダクトに導入した際、重要だなと思ったポイントを5点紹介します。
Koji Wakamiya
January 21, 2019
More Decks by Koji Wakamiya
See All by Koji Wakamiya
Flutterコントリビューションのススメ
d_r_1009
1
1.1k
人気サービスをFlutter Webでリプレースするとどうなるのか
d_r_1009
0
1.2k
モバイルアプリケーション 開発組織の“学び”
d_r_1009
0
390
Add-to-appの戦い方
d_r_1009
0
1.8k
開発チーム主導で iOSの新機能に対応する 3つのポイント
d_r_1009
0
690
FlutterKaigi2021
d_r_1009
0
1.9k
Flutter?
d_r_1009
0
440
Room2.1 DatabaseView あるある
d_r_1009
0
720
AAC Paging & Kotlin化の紹介
d_r_1009
0
1.3k
Other Decks in Programming
See All in Programming
GC言語のWasm化とComponent Modelサポートの実践と課題 - Scalaの場合
tanishiking
0
130
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
1.2k
L’IA au service des devs : Anatomie d'un assistant de Code Review
toham
0
160
へんな働き方
yusukebe
6
2.9k
Codexに役割を持たせる 他のAIエージェントと組み合わせる実務Tips
o8n
4
1.5k
ポーリング処理廃止によるイベント駆動アーキテクチャへの移行
seitarof
3
1.3k
最初からAWS CDKで技術検証してもいいんじゃない?
akihisaikeda
4
180
Goの型安全性で実現する複数プロダクトの権限管理
ishikawa_pro
2
1.4k
ロボットのための工場に灯りは要らない
watany
12
3.3k
野球解説AI Agentを開発してみた - 2026/02/27 LayerX社内LT会資料
shinyorke
PRO
0
380
2026-03-27 #terminalnight 変数展開とコマンド展開でターミナル作業をスマートにする方法
masasuzu
0
270
Ruby and LLM Ecosystem 2nd
koic
1
1.4k
Featured
See All Featured
Color Theory Basics | Prateek | Gurzu
gurzu
0
270
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.9k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
300
Designing for Timeless Needs
cassininazir
0
180
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
New Earth Scene 8
popppiees
2
2k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
500
Practical Orchestrator
shlominoach
191
11k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5.5k
How to Think Like a Performance Engineer
csswizardry
28
2.5k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
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