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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
Simple Storage Service(S3) is not simple
iwatsukayura
0
230
re:Inventに行く前に知っておきたい現地参加のノウハウ
nokomoro3
0
390
複雑なUIコンポーネントとの ⻑い付き合い⽅:波及するバグからテスト戦略を経て、安全な再設計へ
mixplace
0
260
golang-bob.pdf
iwatsukayura
0
110
なぜCTOを降りてFDEを選んだのか?〜なぜプロダクト企業がFDEで顧客の現場に踏み込むのか〜
gonta
1
170
パーソナライズPodcastを作った話
repunit11
0
130
KiroのSpecで「五目並べ」を作ってみる
satoshi256kbyte
1
360
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
640
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
120
自サークルのIdPからログアウトできるようになるまで
taiseiue
1
130
JRuby: Past, Present, and Future
headius
0
230
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
470
Featured
See All Featured
BBQ
matthewcrist
90
10k
Are puppies a ranking factor?
jonoalderson
2
4k
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
520
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
5
780
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
50
10k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
sira's awesome portfolio website redesign presentation
elsirapls
0
450
WENDY [Excerpt]
tessaabrams
14
40k
So, you think you're a good person
axbom
PRO
2
2.2k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
2
350
The Cost Of JavaScript in 2023
addyosmani
55
10k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
560
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