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の言語機能をフル活用したAndroidアプリの開発
Search
Takuji Nishibayashi
October 30, 2017
Technology
6
8.3k
Kotlinの言語機能をフル活用したAndroidアプリの開発
Hatena Engineer Seminar #9 で発表した内容です
Takuji Nishibayashi
October 30, 2017
Tweet
Share
More Decks by Takuji Nishibayashi
See All by Takuji Nishibayashi
CameraX使ってみた
takuji31
0
86
kotlinx.datetime 使ってみた
takuji31
0
330
HiltのCustom Componentについて
takuji31
0
190
java.timeをAndroidで使う
takuji31
0
73
KSPを使ってコード生成
takuji31
0
310
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
990
kotlinx.serialization
takuji31
0
550
kanmoba-returns-02.pdf
takuji31
0
180
AndroidXとKotlin Coroutines
takuji31
0
340
Other Decks in Technology
See All in Technology
podman_update_2024-12
orimanabu
1
260
日本版とグローバル版のモバイルアプリ統合の開発の裏側と今後の展望
miichan
1
130
アップデート紹介:AWS Data Transfer Terminal
stknohg
PRO
0
180
LINE Developersプロダクト(LIFF/LINE Login)におけるフロントエンド開発
lycorptech_jp
PRO
0
120
マイクロサービスにおける容易なトランザクション管理に向けて
scalar
0
110
Snykで始めるセキュリティ担当者とSREと開発者が楽になる脆弱性対応 / Getting started with Snyk Vulnerability Response
yamaguchitk333
2
180
Amazon VPC Lattice 最新アップデート紹介 - PrivateLink も似たようなアップデートあったけど違いとは
bigmuramura
0
190
非機能品質を作り込むための実践アーキテクチャ
knih
3
900
なぜCodeceptJSを選んだか
goataka
0
160
re:Invent をおうちで楽しんでみた ~CloudWatch のオブザーバビリティ機能がスゴい!/ Enjoyed AWS re:Invent from Home and CloudWatch Observability Feature is Amazing!
yuj1osm
0
120
5分でわかるDuckDB
chanyou0311
10
3.2k
How to be an AWS Community Builder | 君もAWS Community Builderになろう!〜2024 冬 CB募集直前対策編?!〜
coosuke
PRO
2
2.8k
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Designing for humans not robots
tammielis
250
25k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.2k
Mobile First: as difficult as doing things right
swwweet
222
9k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Raft: Consensus for Rubyists
vanstee
137
6.7k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.4k
How STYLIGHT went responsive
nonsquared
95
5.2k
4 Signs Your Business is Dying
shpigford
181
21k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
Transcript
KotlinͷݴޠػೳΛ ϑϧ׆༻ͨ͠ AndroidΞϓϦͷ։ൃ id:takuji31 Hatena Engineer Seminar #9
ҙࣄ߲ • AndroidΞϓϦ։ൃͷલఏࣝͱͳΔΑ͏ͳ ෦ͷղઆ͠·ͤΜ • ͜ͷεϥΠυʹॻ͔Ε͍ͯΔKotlinͷίʔυ ༷όʔδϣϯ1.1.51࣌ͷͷͰ͢
ࣗݾհ • ྛ ࢤ (ʹ͠͠ ͨ͘͡) • (hatena | twitter
| github) takuji31 • גࣜձࣾͯͳ ΞϓϦέʔγϣϯΤϯδχΞ • ϒοΫϚʔΫΞϓϦνʔϜॴଐ
ࣗݾհ • Android։ൃ 20102݄ʙ • Kotlin 201412݄ʙ • iOSWebͷ։ൃᅂΉ
׆ಈ • Koreference • KotlinͰSharedPreferencesͷϞσϧΛ࡞Δ ϥΠϒϥϦʔ • github.com/takuji31/Koreference • Kansai.kt
ӡӦ
ϓϥΠϕʔτ • Χϝϥ(not Ϩϯζ)পॅਓ • ϚΠϘϥʔ (AVG175) • ͏͗͞ •
ΞΠίϯ͏ͪͷࢠͰ͢
ͱ͜ΖͰ
Έͳ͞ΜAndroidΞϓϦ ։ൃͯ͠·͔͢ʁ
Kotlinॻ͍ͯ·͔͢ʁ
Kotlinͷݴޠػೳ׆༻ ͍ͯ͠·͔͢ʁ
Null҆શ͚ͩͰຬ͍ͯ͠· ͤΜ͔ʁ
ࠓͯͳϒοΫϚʔΫ AndroidΞϓϦͰͷKotlin ׆༻ࣄྫΛհ͠·͢
AGENDA • Kotlinಋೖ·ͰͷಓͷΓ • KotlinͷݴޠػೳΛ׆༻͢Δ • data class • Extension
method / property • Delegated property
Kotlin ಋೖ·ͰͷಓͷΓ
ͯͳͰͷ৽ٕज़ (ݴޠϛυϧΣΞ) ಋೖ
ಋೖ·Ͱ • ΤϯδχΞ͕ىҊ • ΤϯδχΞνʔϑձͰಋೖͷੋඇΛٞ • CTO͕ঝೝ
Kotlinͷ߹ • I/O Keynote͕ऴΘͬͨޙͷேɺKotlinಋೖ ىҊ (5/18) • ΤϯδχΞνʔϑձ • ঝೝ
(5/19) • ͜͜·Ͱ͓Αͦ1
Kotlin+ͯͳϒοΫϚʔΫͷ߹ • (ि໌͚ͯ) 22νʔϜͷσΟϨΫλʔ͕ ෆࡏͩͬͨͷͰ1ճٳΈ • 23ேʹσΟϨΫλʔͱձͯ͠ಋೖܾఆ • ଈಋೖ
None
None
Kotlinͷ ݴޠػೳΛ ׆༻͢Δ
data class
ԿΒ͔ͷσʔλΛอ࣋͢Δͨ ΊͷΫϥε
ModelDTOΛ࡞ΔͨΊͷ ศརػೳຬࡌ
ྫ) طଘͷmodelΛKotlinʹ ม͍ͨ͠
มConvert Java file to Kotlin fileͰ
୯७ͳม͚ͩͰΑ͍ͷ͔ʁ
୯७ͳม͚ͩͰΑ͍ͷ͔ʁ • Nullableͳͷ͔NonNullͳͷ͔ࢦఆ͍ͨ͠ • equals/hashCode/toStringΛ࣮͍ͨ͠
ྫʣϢʔβʔͷmodelΛఆٛ • id • ໊લ • • ໊ •
ϓϩϑΟʔϧ • ొঢ়ଶ
Javaͷmodel(Ұ෦) public class JavaUser { private final long id; private
String firstName; private String lastName; private String profile; private boolean registered; public JavaUser(long id, String firstName, String lastName, String profile, boolean registered) { this.id = id; this.firstName = firstName; this.lastName = lastName; this.profile = profile; this.registered = registered; } public long getId() { return id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getProfile() { return profile; } public void setProfile(String profile) { this.profile = profile; } public boolean isRegistered() { return registered; } public void setRegistered(boolean registered) { this.registered = registered; } public String getFullName() { return firstName + " " + lastName; } }
ಡΊͳ͍
Kotlinͷmodel data class JavaUser( val id: Long, var firstName: String,
var lastName: String, var profile: String?, var isRegistered: Boolean = false ) { val fullName: String get() = firstName + " " + lastName } ಄ʹEBUB Λ͚ͭΔ શͯͷ CBDLJOHpFME ϓϩύςΟʔΛ DPOTUSVDUPSͰఆٛ
equals/hashCode/toString →ࣗಈੜ
boilerplate codeΛ ݮ͖ͯͬ͢͠Γ
Extension
֦ுؔ/֦ுϓϩύςΟʔ
طଘͷΫϥεʹ ؔϓϩύςΟʔΛੜ͢ (Α͏ʹݟͤΔ)Έ
FragmentΛ֦ுϓϩύςΟʔͰ ֦ு͢Δ val Fragment.baseActivity: BaseActivity? get() = activity as BaseActivity?
val Fragment.actionBar: ActionBar? get() = baseActivity?.supportActionBar ֦ு͍ͨ͠ܕ Λॻ͘ ͦͷଞ௨ৗͷ ϓϩύςΟʔͱಉ͡
RealmͷProviderΛ֦ுؔͰ ֦ு͢Δ fun Provider<Realm>.getAutoCloseObservable(): Observable<Realm> { val realm = get()
realm.use { return Observable.create { emitter -> val instance = get() emitter.setCancellable { instance.close() } emitter.onNext(instance) } } } ֦ு͍ͨ͠ܕ Λॻ͘ ͦͷଞ௨ৗͷ ؔͱಉ͡ (FOFSJDTͷ ܕύϥϝʔλʔࢦఆՄ ϓϩύςΟʔಉ༷
طଘͷJavaΫϥε ֦ுͰ͖Δ
(Javaଆ͔Β) ୈ1ҾΛϨγʔόʔʹͨ͠ staticϝιουʹݟ͑Δ
→ طଘΫϥεͷҰ෦Λ KotlinԽͰ͖Δ
ػೳͷଟ͍ը໘Λ KotlinԽ
KotlinԽͷखॱ • ֦ுؔΛఆٛ͢ΔϑΝΠϧΛ࡞Δ • JavaϑΝΠϧͷϝιουΛ֦ுؔʹͯ͠Kotlinଆʹఆ ٛ • Javaͷݩϝιου͔Β֦ுؔΛݺͼग़͢ • ͋ΔఔݩϑΝΠϧ͕૫ͤͨΒKotlinʹม
• ֦ுؔͷதΛݩϝιουʹ͍ͯ͘͠
pros • 1ͭͣͭdiff͕খ͘͞ͳΔͷͰϨϏϡʔ͢͠ ͍ • Ұ෦͚ͩKotlinԽ͢Δ͜ͱ͕Ͱ͖ͯؾܰʹΕ Δ
cons • gitͷྺ࢙͕ফ໓ • ϑΝΠϧ͕͔ΕͯίʔυΛ͏ͷ͕গ͠େ มʹ
֦ு͗͢͠Δͱ Θ͚͕Θ͔Βͳ͘ͳΔ
͝ར༻ܭըతʹ
Delegated property
ҕৡϓϩύςΟʔ
propertyͷॲཧΛ ผͷΦϒδΣΫτʹ ҕৡͰ͖Δ
Data Binding Libraryͷ BaseObservableͷ มߋ௨ΛࣗಈԽ
Delegated propertyͷఆٛ open class BaseObservableProperty<T : Any?>( vararg val propertyId:
Int, var value: T ) : ReadWriteProperty<BaseObservable, T> { override fun getValue( thisRef: BaseObservable, property: KProperty<*>): T { return value } override fun setValue( thisRef: BaseObservable, property: KProperty<*>, value: T) { this.value = value propertyId.forEach(thisRef::notifyPropertyChanged) } } 3FBE8SJUF1SPQFSUZ ΠϯλʔϑΣΠεΛ࣮ HFU7BMVFTFU7BMVFΛ ఆٛ UIJTͷࢀরͱQSPQFSUZͷ ใ͕͞ΕΔ
Delegated propertyͷఆٛ class NonNullBaseObservableProperty<T : Any>(vararg propertyId: Int, value: T)
: BaseObservableProperty<T>(propertyId = *propertyId, value = value) fun <T : Any?> BaseObservable.property(vararg propertyId: Int, value: T? = null) = BaseObservableProperty(propertyId = *propertyId, value = value) fun <T : Any> BaseObservable.nonNullProperty(vararg propertyId: Int, value: T) = NonNullBaseObservableProperty(propertyId = *propertyId, value = value) ܕ͕/PO/VMMͳͷ༻ͷఆٛ ֦ுؔʹ͓ͯ͘͠ͱ ͏࣌ʹศར
ར༻͢Δ class ViewModel: BaseObservable() { @get:Bindable var users: List<User> by
nonNullProperty(BR.users, value = emptyList()) fun requestUsers(): Disposable { return getUser .execute() .subscribeBy( onSuccess = { user -> this.users = users }, ) } } CZͷޙʹΦϒδΣΫτΛ͢ TFU͢Δ͜ͱͰࣗಈతʹ OPUJGZ1SPQFSUZ$IBOHFE͕ ݺΕΔ
ศརͦ͏͚ͩͲ Ͳ͏͍͏༻్Ͱ͑Δ͔ Θ͔Βͳ͍ʁ
SharedPreferencesͷૢ࡞
͜ΕΛ͍·͠ΐ͏ˠ https://github.com/ takuji31/Koreference
ͯͳϒοΫϚʔΫͰ ͍ͬͯ·͢
σϑΥϧτͷͷ ༻ҙ͞Ε͍ͯ·͢
σϑΥϧτͷ Delegated property(Ұ෦) • Delegates.notNull • not-null͕ͩॳظԽ࣌ʹॳظԽͰ͖ͳ͍ϓϩύςΟʔ • nullͷ··ΞΫηεͨ͠Βྫ֎ •
lazy • ԆॳظԽ • ࠷ॳͷΞΫηε࣌ʹҾͰͨ͠ϒϩοΫͷΓΛฦͯ͠ Ωϟογϡ͢Δ
KotlinϞμϯͳݴޠͳͷͰ ॻ͘ίʔυϞμϯʹ ͍͖ͯ͠·͠ΐ͏ :kotlin:
Enjoy Kotlin Life