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
100
kotlinx.datetime 使ってみた
takuji31
0
390
HiltのCustom Componentについて
takuji31
0
200
java.timeをAndroidで使う
takuji31
0
81
KSPを使ってコード生成
takuji31
0
330
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
1k
kotlinx.serialization
takuji31
0
560
kanmoba-returns-02.pdf
takuji31
0
190
AndroidXとKotlin Coroutines
takuji31
0
350
Other Decks in Technology
See All in Technology
【JAWS-UG大阪 reInvent reCap LT大会 サンバが始まったら強制終了】“1分”で初めてのソロ参戦reInventを数字で振り返りながら反省する
ttelltte
0
130
駆け出しリーダーとしての第一歩〜開発チームとの新しい関わり方〜 / Beginning Journey as Team Leader
kaonavi
0
120
Godot Engineについて調べてみた
unsoluble_sugar
0
360
カップ麺の待ち時間(3分)でわかるPartyRockアップデート
ryutakondo
0
130
2025年に挑戦したいこと
molmolken
0
150
Cloudflareで実現する AIエージェント ワークフロー基盤
kmd09
0
280
商品レコメンドでのexplicit negative feedbackの活用
alpicola
1
330
.NET AspireでAzure Functionsやクラウドリソースを統合する
tsubakimoto_s
0
180
なぜfreeeはハブ・アンド・スポーク型の データメッシュアーキテクチャにチャレンジするのか?
shinichiro_joya
2
160
re:Invent 2024のふりかえり
beli68
0
100
ゼロからわかる!!AWSの構成図を書いてみようワークショップ 問題&解答解説 #デッカイギ #羽田デッカイギおつ
_mossann_t
0
1.5k
SpiderPlus & Co. エンジニア向け会社紹介資料
spiderplus_cb
0
830
Featured
See All Featured
How to Ace a Technical Interview
jacobian
276
23k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Visualization
eitanlees
146
15k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Rails Girls Zürich Keynote
gr2m
94
13k
Product Roadmaps are Hard
iamctodd
PRO
50
11k
A better future with KSS
kneath
238
17k
Bash Introduction
62gerente
610
210k
The Cost Of JavaScript in 2023
addyosmani
46
7.2k
Being A Developer After 40
akosma
89
590k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.4k
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