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.2k
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
66
kotlinx.datetime 使ってみた
takuji31
0
260
HiltのCustom Componentについて
takuji31
0
150
java.timeをAndroidで使う
takuji31
0
60
KSPを使ってコード生成
takuji31
0
290
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
960
kotlinx.serialization
takuji31
0
540
kanmoba-returns-02.pdf
takuji31
0
180
AndroidXとKotlin Coroutines
takuji31
0
330
Other Decks in Technology
See All in Technology
[CV勉強会@関東 ECCV2024 読み会] オンラインマッピング x トラッキング MapTracker: Tracking with Strided Memory Fusion for Consistent Vector HD Mapping (Chen+, ECCV24)
abemii
0
220
Adopting Jetpack Compose in Your Existing Project - GDG DevFest Bangkok 2024
akexorcist
0
110
iOS/Androidで同じUI体験をネ イティブで作成する際に気をつ けたい落とし穴
fumiyasac0921
1
110
Platform Engineering for Software Developers and Architects
syntasso
1
520
【Pycon mini 東海 2024】Google Colaboratoryで試すVLM
kazuhitotakahashi
2
540
生成AIが変えるデータ分析の全体像
ishikawa_satoru
0
170
B2B SaaSから見た最近のC#/.NETの進化
sansantech
PRO
0
880
AIチャットボット開発への生成AI活用
ryomrt
0
170
マルチプロダクトな開発組織で 「開発生産性」に向き合うために試みたこと / Improving Multi-Product Dev Productivity
sugamasao
1
310
Shopifyアプリ開発における Shopifyの機能活用
sonatard
4
250
AWS Media Services 最新サービスアップデート 2024
eijikominami
0
200
Terraform Stacks入門 #HashiTalks
msato
0
360
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
The Language of Interfaces
destraynor
154
24k
Building Adaptive Systems
keathley
38
2.3k
Bash Introduction
62gerente
608
210k
Why Our Code Smells
bkeepers
PRO
334
57k
Adopting Sorbet at Scale
ufuk
73
9.1k
The Cost Of JavaScript in 2023
addyosmani
45
6.8k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Into the Great Unknown - MozCon
thekraken
32
1.5k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
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