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
みんなでプロポーザルを書いてみた
yuriko1211
0
280
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
990
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.2k
Jakarta EE meets AI
ivargrimstad
0
690
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
340
Jakarta EE meets AI
ivargrimstad
0
620
CSC509 Lecture 09
javiergs
PRO
0
140
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
610
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
subpath importsで始めるモック生活
10tera
0
320
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
240
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
Featured
See All Featured
The Language of Interfaces
destraynor
154
24k
KATA
mclloyd
29
14k
Writing Fast Ruby
sferik
627
61k
It's Worth the Effort
3n
183
27k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
Producing Creativity
orderedlist
PRO
341
39k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
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!