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
580
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
2.2k
#Ubie 狂気の認知施策と選考設計
ntaro
13
13k
UbieにおけるサーバサイドKotlin活用事例
ntaro
1
1.1k
KotlinでSpring 完全理解ガイド #jsug
ntaro
6
3.4k
Kotlinでサーバサイドを始めよう!
ntaro
1
960
Androidからサーバーサイドまで!プログラミング言語 Kotlinの魅力 #devboost
ntaro
5
2.7k
Kotlin Contracts #m3kt
ntaro
4
4k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
480
Other Decks in Programming
See All in Programming
Go1.24 go vetとtestsアナライザ
kuro_kurorrr
2
480
SideKiqでジョブが二重起動した事象を深堀りしました
t_hatachi
0
240
自分のために作ったアプリが、グローバルに使われるまで / Indie App Development Lunch LT
pixyzehn
1
130
Devin入門と最近のアップデートから見るDevinの進化 / Introduction to Devin and the Evolution of Devin as Seen in Recent Update
rkaga
7
3.9k
本当だってば!俺もTRICK 2022に入賞してたんだってば!
jinroq
0
260
Firebase Dynamic Linksの代替手段を自作する / Create your own Firebase Dynamic Links alternative
kubode
0
180
RailsでCQRS/ESをやってみたきづき
suzukimar
2
1.5k
新卒から4年間、20年もののWebサービスと 向き合って学んだソフトウェア考古学
oguri
8
6.9k
eBPF Updates (March 2025)
kentatada
0
130
Preact、HooksとSignalsの両立 / Preact: Harmonizing Hooks and Signals
ssssota
1
780
Denoでフロントエンド開発 2025年春版 / Frontend Development with Deno (Spring 2025)
petamoriken
1
1.3k
ベクトル検索システムの気持ち
monochromegane
30
9.1k
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
30
1.1k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
Mobile First: as difficult as doing things right
swwweet
223
9.5k
The Language of Interfaces
destraynor
157
24k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
102
18k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.8k
KATA
mclloyd
29
14k
Optimising Largest Contentful Paint
csswizardry
35
3.2k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
4
500
Gamification - CAS2011
davidbonilla
81
5.2k
Docker and Python
trallard
44
3.3k
RailsConf 2023
tenderlove
29
1k
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!