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
370
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.6k
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
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
280
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
9
3.3k
初めてDefinitelyTypedにPRを出した話
syumai
0
400
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
詳細解説! ArrayListの仕組みと実装
yujisoftware
0
570
Outline View in SwiftUI
1024jp
1
320
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
2
650
EventSourcingの理想と現実
wenas
6
2.3k
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
Jakarta EE meets AI
ivargrimstad
0
510
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
250
as(型アサーション)を書く前にできること
marokanatani
8
2.6k
Featured
See All Featured
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
720
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Designing the Hi-DPI Web
ddemaree
280
34k
How to Ace a Technical Interview
jacobian
276
23k
It's Worth the Effort
3n
183
27k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
BBQ
matthewcrist
85
9.3k
The Pragmatic Product Professional
lauravandoore
31
6.3k
Code Reviewing Like a Champion
maltzj
520
39k
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!