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
430
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
2.2k
#Ubie 狂気の認知施策と選考設計
ntaro
13
13k
UbieにおけるサーバサイドKotlin活用事例
ntaro
1
1.1k
KotlinでSpring 完全理解ガイド #jsug
ntaro
6
3.3k
Kotlinでサーバサイドを始めよう!
ntaro
1
930
Androidからサーバーサイドまで!プログラミング言語 Kotlinの魅力 #devboost
ntaro
5
2.6k
Kotlin Contracts #m3kt
ntaro
4
3.9k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
440
Other Decks in Programming
See All in Programming
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
140
Beyond ORM
77web
7
960
Keeping it Ruby: Why Your Product Needs a Ruby SDK - RubyWorld 2024
envek
0
190
range over funcの使い道と非同期N+1リゾルバーの夢 / about a range over func
mackee
0
110
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
140
Recoilを剥がしている話
kirik
5
6.8k
Go の GC の不得意な部分を克服したい
taiyow
3
800
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
220
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
780
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
ある日突然あなたが管理しているサーバーにDDoSが来たらどうなるでしょう?知ってるようで何も知らなかったDDoS攻撃と対策 #phpcon.2024
akase244
1
150
Featured
See All Featured
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
Automating Front-end Workflow
addyosmani
1366
200k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
2
290
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
A Tale of Four Properties
chriscoyier
157
23k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Rails Girls Zürich Keynote
gr2m
94
13k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
0
98
Product Roadmaps are Hard
iamctodd
PRO
49
11k
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!