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プログラミング #DroidKaigi
Search
Taro Nagasawa
April 25, 2015
Programming
16
20k
新言語KotlinでAndroidプログラミング #DroidKaigi
DroidKaigiでの発表で使用したスライドです。
Taro Nagasawa
April 25, 2015
Tweet
Share
More Decks by Taro Nagasawa
See All by Taro Nagasawa
Android開発者のための Kotlin Multiplatform入門
ntaro
0
340
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
2.1k
#Ubie 狂気の認知施策と選考設計
ntaro
13
12k
UbieにおけるサーバサイドKotlin活用事例
ntaro
1
1.1k
KotlinでSpring 完全理解ガイド #jsug
ntaro
6
3.3k
Kotlinでサーバサイドを始めよう!
ntaro
1
920
Androidからサーバーサイドまで!プログラミング言語 Kotlinの魅力 #devboost
ntaro
5
2.5k
Kotlin Contracts #m3kt
ntaro
4
3.8k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
430
Other Decks in Programming
See All in Programming
Universal Linksの実装方法と陥りがちな罠
kaitokudou
1
220
JaSST 24 九州:ワークショップ(は除く)実践!マインドマップを活用したソフトウェアテスト+活用事例
satohiroyuki
0
260
Piniaの現状と今後
waka292
5
1.5k
OpenTelemetryでRailsのパフォーマンス分析を始めてみよう(KoR2024)
ymtdzzz
4
1.6k
色々なIaCツールを実際に触って比較してみる
iriikeita
0
270
レガシーな Android アプリのリアーキテクチャ戦略
oidy
1
170
Tuning GraphQL on Rails
pyama86
2
1k
Kaigi on Rails 2024 - Rails APIモードのためのシンプルで効果的なCSRF対策 / kaigionrails-2024-csrf
corocn
5
3.4k
PLoP 2024: The evolution of the microservice architecture pattern language
cer
PRO
0
1.7k
シールドクラスをはじめよう / Getting Started with Sealed Classes
mackey0225
3
400
Hotwire or React? ~Reactの録画機能をHotwireに置き換えて得られた知見~ / hotwire_or_react
harunatsujita
9
4.1k
カスタムしながら理解するGraphQL Connection
yanagii
1
1.2k
Featured
See All Featured
Product Roadmaps are Hard
iamctodd
PRO
48
10k
The Language of Interfaces
destraynor
154
24k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
14
1.9k
A Tale of Four Properties
chriscoyier
156
23k
StorybookのUI Testing Handbookを読んだ
zakiyama
26
5.2k
Typedesign – Prime Four
hannesfritz
39
2.4k
Designing the Hi-DPI Web
ddemaree
280
34k
Into the Great Unknown - MozCon
thekraken
31
1.5k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Practical Orchestrator
shlominoach
186
10k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Transcript
৽ݴޠ KotlinͰ Androidϓϩάϥϛϯά %SPJE,BJHJ ! ຊ,PUMJOϢʔβάϧʔϓ ᖒଠ!OHTX@UBSP ͜ͱΓΜ
ࣗݾհ
͡Ί·ͯ͠ʂ w ᖒଠ!OHTX@UBSP w ࣄͰ+BWB "OESPJEɺͱ͖Ͳ͖4DBMB w ࡀɺֈ࠲ɺͱ͕େ͖
Θͨ͠ͱAndroid w ग़ձ͍ɻ)5"Ͱ༡ΜͰΈͨͷ͕࠷ॳ w ݸਓͰʮເͱຐ๏ͷͪ࣌ؒʯͱ͍͏ΞϓϦΛ։ ൃɾެ։͍ͯ͠·͢ ສ%- w ॻ੶ٕज़ܥಉਓࢽͳͲʹܞΘΒ͍͍ͤͯͨͩͨ
ܦݧ͕͋Γ·͢
Θͨ͠ͱKotlin w ເதʹͳΓ࢝Ίͨͷ w ҎདྷɺϒϩάษڧձͰใൃ৴ w 4PGUXBSF%FTJHO ٕධ Ͱ,PUMJOهࣄΛ࿈ࡌத
͘͡ ,PUMJOͱ +BWBͱൺΔ ,PUMJOͰ"OESPJEϓϩάϥϛϯά
1. Kotlinͱ
࣭ Kotlinͱ͍͏ϓϩάϥϛϯάݴޠ Λฉ͍ͨ͜ͱ͕͋Γ·͔͢ʁ
w !+BLF8IBSUPO͞Μ w ʮ6TJOH1SPKFDU,PUMJOGPS"OESPJEʯ w !LJSJNJO͞Μ w ʮͷ"OESPJE։ൃ,PUMJOͰܾ·Γ͔ ͠Εͳ͍ʯ Androidք۾Ͱόζͬͨʁ
Kotlinͱ
Kotlinͱ Better Java
w Նʹ+FU#SBJOTʹΑΓൃද w Φʔϓϯιʔε "QBDIF-JDFOTF w +7.ݴޠɺBMU+4ɺ"OESPJEαϙʔτ w ੩తܕ͚ΦϒδΣΫτࢦݴޠ
Kotlinͱ (ৄ͘͠)
ଞͷJVMݴޠͱͷҧ͍
ଞͷJVMݴޠͱͷҧ͍ ϚΠϧυ ݱ࣮ݟͯΔײ
Kotlinͷಛ ؆୯ ҆શ
2. JavaͱൺΔ
Hello World (Java) class HelloWorld { public static void main(String[]
args) { System.out.println("Hello, world!"); } }
Hello World (Kotlin) fun main(args: Array<String>) { println("Hello, world!") }
Hello World (Kotlin) fun main(args: Array<String>) { println("Hello, world!") }
τοϓϨϕϧʹ ؔΛஔ͚Δ ྻʹ δΣωϦΫε ηϛίϩϯ ෆཁ
ม (Java) final int a = 0;
ม (Kotlin) val a = 0
ม (Kotlin) val a = 0 ܕਪ Մมͳมʹ WBSΩʔϫʔυΛ
Ϋϥε (Java) class User { private final long id; private
final String name; public User(long id, String name) { this.id = id; this.name = Objects.requireNonNull(name); } public long getId() { return id; } public String getName() { return name; } / / toString, equals, hashCodeͱ͔ }
Ϋϥε (Kotlin) class User(val id: Long, val name: String) {
/ / toString, equals, hashCodeͱ͔ }
Ϋϥε (Kotlin) class User(val id: Long, val name: String) {
/ / toString, equals, hashCodeͱ͔ } ίϯετϥΫλҾ͕ ͦͷ··ϓϩύςΟʹ
dataΞϊςʔγϣϯ data class User(val id: Long, val name: String)
σʔλΫϥε val taro = User(123, "Taro") println(taro.id) / / =>
123 println(taro.name) / / => Taro println(taro) / / => User(id=123, name=Taro)
ؔܕΠϯλϑΣʔε (Java) / / anonymous class button.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) { toast("Clicked!"); } }); ! / / lambda button.setOnClickListener(v -> toast("Clicked!"));
ؔܕΠϯλϑΣʔε (Kotlin) var count = 0 button.setOnClickListener { toast("${count++}") }
ؔܕΠϯλϑΣʔε (Kotlin) var count = 0 button.setOnClickListener { toast("${count++}") }
Ϋϩʔδϟ
ߴ֊ؔͰίϨΫγϣϯૢ࡞ (1..999) .filter { it % 3 == 0 ||
it % 5 == 0 } .reduce { a, b -> a + b }
ߴ֊ؔͰίϨΫγϣϯૢ࡞ (1..999) .filter { it % 3 == 0 ||
it % 5 == 0 } .reduce { a, b -> a + b } ʮΠϯϥΠϯؔʯ ؔϦςϥϧ͕ల։͞Ε ΦϒδΣΫτΛੜ͠ͳ͍
ಠࣗศརϝιου (Java) / / ఆٛ static void toast(Context c, String
s) {...} ! / / ݺͼग़͠ MyUtils.toast(context, "Hello");
ಠࣗศརϝιου (Kotlin) / / ఆٛ fun Context.toast(s: String) {...} !
/ / ݺͼग़͠ context.toast("Hello") ! / / ݺͼग़͠(ActivityͳͲͷத) toast("Hello")
ಠࣗศརϝιου (Kotlin) / / ఆٛ fun Context.toast(s: String) {...} !
/ / ݺͼग़͠ context.toast("Hello") ! / / ݺͼग़͠(ActivityͳͲͷத) toast("Hello") ϝιουΛੜͤΔ ʮ֦ுؔʯ ੩తʹղܾ͞ΕΔʂ
Optional (Java) String a = "hoge"; Optional<String> b = Optional.of("fuga");
Optional<String> c = null;
Optional (Java) String a = "hoge"; Optional<String> b = Optional.of("fuga");
Optional<String> c = null; ΦϒδΣΫτੜ ୭ʹOVMMࢭΊΒΕͳ͍ʂ
Optional (Kotlin) val a: String = "hoge" val b: String
= null / / NG ! val c: String? = "hoge" val d: String? = null / / OK ! d.toUpperCase() / / NG
NULL҆શ val a: String? = "hoge" val b: String? =
null ! if (a != null) a.toUpperCase() else null / / => HOGE if (b != null) b.toUpperCase() else null / / => null ! a?.toUpperCase() / / => HOGE b?.toUpperCase() / / => null
NULL҆શ val a: String? = "hoge" val b: String? =
null ! if (a != null) a.toUpperCase() else null / / => HOGE if (b != null) b.toUpperCase() else null / / => null ! a?.toUpperCase() / / => HOGE b?.toUpperCase() / / => null OVMMͰͳ͍͜ͱ͕อূ͞Ε͍ͯΔ ॴͰ/PU/VMMܕͱͯ͠ѻ͑Δ
3. KotlinͰ Androidϓϩάϥϛϯά
Android StudioͰ͏ ී௨ʹϓϩδΣΫτΛ࡞ +BWB,PUMJOม Φϓγϣϯ CVJMEHSBEMFͷઃఆ ָ͍͠,PUMJOϥΠϑͷ࢝·Γʂ
KotterKnife w,PUMJO൛#VUUFS,OJGF w࡞ऀ+BLF8IBSUPO͞Μ wಋೖΈղઆͪ͜ΒΛࢀর IUUQRJJUBDPNOHTX@UBSPJUFNTDEDEE
KotterKnife ༻લ var button: Button? = null ! button =
findViewById(R.id.button) as Button ! button?.setOnClickListener {...}
KotterKnife ༻લ var button: Button? = null ! button =
findViewById(R.id.button) as Button ! button?.setOnClickListener {...} "DUJWJUZͱ͔ͷϓϩύςΟ ຖͷpOE7JFX#Z*E ͦͯ͠Ωϟετʜ
KotterKnife ༻ޙ var button: Button by bindView(R.id.button) ! button.setOnClickListener {...}
KotterKnife ༻ޙ var button: Button by bindView(R.id.button) ! button.setOnClickListener {...}
ϓϩύςΟ ಉ͡ CZΩʔϫʔυʹଓ͚ͯ 3JECVUUPOΛόΠϯυ pOE7JFX#Z*EෆཁͰ ී௨ʹ͑ΔΑ͏ʹͳͬͯΔ
Kotlin Android Extensions w"4 ͱ*OUFMMJ+*%&" ͷϓϥάΠϯ wpOE7JFX#Z*EΛۦஞ͢Δͭ wνϡʔτϦΞϧͪ͜ΒΛࢀর IUUQLPUMJOMBOHPSHEPDTUVUPSJBMTBOESPJEQMVHJOIUNM
༻ྫ import kotlinx.android.synthetic.activity_main.button ! public class MainActivity: Activity() { override
fun onCreate(savedInstaceState: Bundle?) { super.onCreate(savedInstaceState) setContentView(R.layout.activity_main) button.setOnClickListener {...}
Anko w͍ΖΜͳ%4- wओʹϨΠΞτΛΉ%4- wܕ҆શɺ؆ܿɾϦʔμϒϧɺ෦Խɾ ࠶ར༻ੑ wৄࡉͪ͜Β IUUQTHJUIVCDPN+FU#SBJOTBOLP
None
·ͱΊ w ,PUMJOϓϩάϥϚʹ༏͍͠ʮݱ࣮ࢦʯݴޠ w "OESPJEͰ͏ͱͤʹͳΕΔ͔
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ Let’s enjoy Kotlin!