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
74
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
PHPからGoへのマイグレーション for DMMアフィリエイト
yabakokobayashi
1
170
DevFest 2024 Incheon / Songdo - Compose UI 조합 심화
wisemuji
0
110
Snykで始めるセキュリティ担当者とSREと開発者が楽になる脆弱性対応 / Getting started with Snyk Vulnerability Response
yamaguchitk333
2
190
Microsoft Azure全冠になってみた ~アレを使い倒した者が試験を制す!?~/Obtained all Microsoft Azure certifications Those who use "that" to the full will win the exam! ?
yuj1osm
2
110
大幅アップデートされたRagas v0.2をキャッチアップ
os1ma
2
540
NilAway による静的解析で「10 億ドル」を節約する #kyotogo / Kyoto Go 56th
ytaka23
3
380
Oracle Cloud Infrastructure:2024年12月度サービス・アップデート
oracle4engineer
PRO
0
200
[Ruby] Develop a Morse Code Learning Gem & Beep from Strings
oguressive
1
170
成果を出しながら成長する、アウトプット駆動のキャッチアップ術 / Output-driven catch-up techniques to grow while producing results
aiandrox
0
350
DUSt3R, MASt3R, MASt3R-SfM にみる3D基盤モデル
spatial_ai_network
2
180
非機能品質を作り込むための実践アーキテクチャ
knih
5
1.5k
継続的にアウトカムを生み出し ビジネスにつなげる、 戦略と運営に対するタイミーのQUEST(探求)
zigorou
0
590
Featured
See All Featured
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Building Your Own Lightsaber
phodgson
103
6.1k
The Cult of Friendly URLs
andyhume
78
6.1k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
95
17k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Speed Design
sergeychernyshev
25
670
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
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