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
64
kotlinx.datetime 使ってみた
takuji31
0
260
HiltのCustom Componentについて
takuji31
0
150
java.timeをAndroidで使う
takuji31
0
59
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
ドメインの本質を掴む / Get the essence of the domain
sinsoku
2
150
Terraform CI/CD パイプラインにおける AWS CodeCommit の代替手段
hiyanger
1
240
Exadata Database Service on Dedicated Infrastructure(ExaDB-D) UI スクリーン・キャプチャ集
oracle4engineer
PRO
2
3.2k
【令和最新版】AWS Direct Connectと愉快なGWたちのおさらい
minorun365
PRO
5
750
透過型SMTPプロキシによる送信メールの可観測性向上: Update Edition / Improved observability of outgoing emails with transparent smtp proxy: Update edition
linyows
2
210
[CV勉強会@関東 ECCV2024 読み会] オンラインマッピング x トラッキング MapTracker: Tracking with Strided Memory Fusion for Consistent Vector HD Mapping (Chen+, ECCV24)
abemii
0
220
信頼性に挑む中で拡張できる・得られる1人のスキルセットとは?
ken5scal
2
530
リンクアンドモチベーション ソフトウェアエンジニア向け紹介資料 / Introduction to Link and Motivation for Software Engineers
lmi
4
300k
Lambda10周年!Lambdaは何をもたらしたか
smt7174
2
110
Amazon Personalizeのレコメンドシステム構築、実際何するの?〜大体10分で具体的なイメージをつかむ〜
kniino
1
100
AWS Lambdaと歩んだ“サーバーレス”と今後 #lambda_10years
yoshidashingo
1
170
Engineer Career Talk
lycorp_recruit_jp
0
130
Featured
See All Featured
4 Signs Your Business is Dying
shpigford
180
21k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
How to Ace a Technical Interview
jacobian
276
23k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Faster Mobile Websites
deanohume
305
30k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
Building Applications with DynamoDB
mza
90
6.1k
For a Future-Friendly Web
brad_frost
175
9.4k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Typedesign – Prime Four
hannesfritz
40
2.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