Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Kotlin導入 5つのステップ
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Koji Wakamiya
January 21, 2019
Programming
1.9k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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.4k
人気サービスをFlutter Webでリプレースするとどうなるのか
d_r_1009
0
1.3k
モバイルアプリケーション 開発組織の“学び”
d_r_1009
0
430
Add-to-appの戦い方
d_r_1009
0
2k
開発チーム主導で iOSの新機能に対応する 3つのポイント
d_r_1009
0
740
FlutterKaigi2021
d_r_1009
0
2k
Flutter?
d_r_1009
0
480
Room2.1 DatabaseView あるある
d_r_1009
0
770
AAC Paging & Kotlin化の紹介
d_r_1009
0
1.4k
Other Decks in Programming
See All in Programming
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
650
[2026-09-26]空論ジェネリックプロセス~テスト資産とAIで紡ぐ、再現可能なパフォーマンスチューニングの話~
tosite
0
260
更なる可用性を求めて、5年間運用したKotlinのアプリケーションをGoでリプレイスする話
ken_tunc
0
470
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
370
Java 27新機能 / Java 27 new features
kishida
2
210
Compiling a real-world CLI tool to native with Static Hermes
tmikov2023
0
100
KiroのSpecで「五目並べ」を作ってみる
satoshi256kbyte
1
360
Turning Architecture into Unit Tests in the AI Era (NSSpain XIV)
steliosf
PRO
1
140
最近”作らせてる” ローカルAIアプリの話
akit37
0
430
カツオ、ご期待ください
suneo3476
0
160
AWSに止められる覚悟してますか?
morizo_1984
2
520
mrbgem 三角測量 開発
ogom
0
220
Featured
See All Featured
Un-Boring Meetings
codingconduct
0
450
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
What the history of the web can teach us about the future of AI
inesmontani
PRO
2
730
KATA
mclloyd
PRO
35
16k
Designing for humans not robots
tammielis
254
26k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.6k
Automating Front-end Workflow
addyosmani
1369
210k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.4k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
1k
Amusing Abliteration
ianozsvald
1
330
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
Writing Fast Ruby
sferik
630
63k
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