Slide 1

Slide 1 text

Kotlin in Google I/O 2017 Final Edition 2017.06.15 CA.kt #1 @satorufujiwara

Slide 2

Slide 2 text

About me Satoru Fujiwara (@satorufujiwara) Android Engineer @CyberAgent ,Inc Shibuya.apk

Slide 3

Slide 3 text

Kotlin & Me FRESH! ΛKotlinͰ։ൃ (2015೥3݄ʙ) Kotlin Advent Calendar (2015/2016) -ʮ2016೥ɺKotlinͰAndroid։ൃ͢Δํ΁ʯ @Qiita ( goo.gl/0medax ) Kotlinೖ໳·Ͱͷॿ૸ಡຊ ( goo.gl/5vUT7o )

Slide 4

Slide 4 text

Kotlin in Google I/O 2017

Slide 5

Slide 5 text

Google Keynote

Slide 6

Slide 6 text

Kotlin in Keynote • KotlinΛAndroidʹ͓͚Δެࣜݴޠͱͯ͠αϙʔτ • JetBrainsࣾΛύʔτʔφʔͱͯ͠Kotlinࡒஂͷઃཱ • طଘͷίʔυ(Java)ʹ΋͞ΒͳΔ౤ࢿΛ͢Δ

Slide 7

Slide 7 text

Why Kotlin? • Kotlin͸Androidͷ։ൃऀίϛϡχςΟ͕طʹٻΊ͍ͯͨݴޠͰɺ։ൃ ऀͷੜ࢈ੑΛߴΊΔ͜ͱ͕ग़དྷΔ • AndroidͷϥϯλΠϜͱͷ׬શͳޓ׵ੑ͕͋Γɺطଘͷίʔυͱͷ૬ޓ ӡ༻͕ՄೳͰ͋Δ • ૉ੖Β͍͠IDEʹΑΔαϙʔτ • ݴޠ͕੒ख़͠ɺ੡඼൛ͱͯ͠ϦϦʔεՄೳͳঢ়ଶͰ͋Δ

Slide 8

Slide 8 text

Developer Keynote

Slide 9

Slide 9 text

4 big themes in Android • Kotlin • Android Studio & Libraries • App Quality & Success • Android Instant App

Slide 10

Slide 10 text

Kotlin in Developer Keynote • First-class Language ʹ • γϯλοΫεϊΠζͷগͳ͍ඒ͍͠ݴޠ • Android Studio 3.0 ʹ૊Έࠐ·ΕΔ • Java8 ͷ͞ΒͳΔαϙʔτ(Android O)

Slide 11

Slide 11 text

Android Studio 3.0

Slide 12

Slide 12 text

buid.gradle buildscript { ext.kotlin_version = '1.1.2-4' dependencies { classpath 'com.android.tools.build:gradle:3.0.0-alpha1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } app/build.gradle apply plugin: ‘kotlin-android' dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" }

Slide 13

Slide 13 text

Why Kotlin? in Developer Keynote • Kotlin͸Javaͱͷ׬શͳ૬ޓӡ༻͕ग़དྷɺ1Ϋϥε͔ΒKotlinԽͰ͖Δ • 100% interoperable with Java and Android • ։ൃ͔Β5೥ܦͪɺݴޠ͕੒ख़͠ɺ੡඼൛ͱͯ͠ϦϦʔεՄೳͳঢ়ଶͰ͋Γɺ࣮ࡍʹ࢖ΘΕ͍ͯΔ • Flipboard,Pinterest ,Square Cash, Expedia • ݴޠ͕ૉ੖Β͍͠ͷʹՃ͑ɺIDEαϙʔτ͕ૉ੖Β͍͠ • Android Studioͷϕʔεͱͳ͍ͬͯΔIntelliJ IDEAͱಉ͡νʔϜ • Kotlin͸طʹApache2ϥΠηϯεͷΦʔϓϯιʔε͕ͩɺ͜Ε͔Β΋ΦʔϓϯιʔεϓϩδΣΫτ ͱͯ͠ඇӦརࡒஂ͕։ൃ͍ͯ͘͠

Slide 14

Slide 14 text

Sessions about Kotlin • Introduction to Kotlin • Life is Great and Everything Will Be Ok, Kotlin is Here

Slide 15

Slide 15 text

Introduction to Kotlin Google I/O 2017 : Introduction to Kotlin (࿨༁/ཁ໿) @Qiita goo.gl/U24xkw

Slide 16

Slide 16 text

Life is Great and Everything Will Be Ok, Kotlin is Here • KotlinͰAndroidΛ։ൃ͢Δ্ͰͷTips • by Jake Wharton (Square) @JakeWharton • KotlinΛ͍͔ʹͯ͠ಋೖ͔ͨ͠ʁ • by Christina Lee (Pinterest) @RunChristinaRun • https://speakerdeck.com/jakewharton/life-is-great-and-everything-will-be-ok- kotlin-is-here-google-io-2017 • https://www.youtube.com/watch?v=fPzxfeDJDzY

Slide 17

Slide 17 text

FRESH! ͰͷKotlin։ൃ • 2015೥3݄ Kotlinݕূ։࢝ɺ4݄͔Βຊ֨։ൃ (Kotlin M11) • 2016೥1݄21೔ αʔϏεϩϯν (Kotlin 1.0-beta3) • ʮ2016೥ɺKotlinͰAndroid։ൃ͢Δํ΁ʯ @Qiita ( goo.gl/0medax )

Slide 18

Slide 18 text

FRESH! ͰKotlinΛ࠾༻ͨ͠ཧ༝ • ݴޠ࢓༷ (Null Safety, Lambda, Collections) • JetBrains • 100% interoperable with Java

Slide 19

Slide 19 text

FRESH! ͰKotlinΛ࠾༻ͨ͠ཧ༝ • ݴޠ࢓༷ (Null Safety, Lambda, Collections) • JetBrains • 100% interoperable with Java -> ʮԿ͔͋ͬͨΒJavaʹ໭ΔΜͰʯΛޱบʹͯ͠։ൃ

Slide 20

Slide 20 text

FRESH! ͰKotlinΛ࠾༻ͨ͠ཧ༝ • ʰUsing Project Kotlin for Androidʱby Jake Wharton • goo.gl/1MyRCk

Slide 21

Slide 21 text

Using Project Kotlin in Android

Slide 22

Slide 22 text

Kotlin or Java • 2016೥2݄(Kotlin 1.0.0 Release)࣌఺Ͱͷબ୒ࢶ͸ • Java8 + Retrolambda • Kotlin • (Jack&Jill)

Slide 23

Slide 23 text

Chosen Kotlin goo.gl/Iopsz0

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

goo.gl/xxLvWE

Slide 26

Slide 26 text

Kotlin on Android. Now official

Slide 27

Slide 27 text

Kotlin͕ެࣜݴޠʹͳͬͯ • Android։ൃͰʮKotlinΛબͿ͜ͱʯ͕ग़དྷΔ • Ҿ͖ଓ͖Javaͷ஌ࣝ͸ඞཁ • Swiftͱ͸ࣅͯඇͳΔݴޠɺڞ௨Խ͸ग़དྷͳ͍ • Kotlin͸ࣗ༝౓͕ߴ͘ɺ͜Ε·ͰҎ্ʹֶशɾνʔϜͰͷٞ࿦͕ ඞཁ

Slide 28

Slide 28 text

Kotlin͸ࣗ༝౓͕ߴ͍

Slide 29

Slide 29 text

ྫ1: lambda

Slide 30

Slide 30 text

fun findEmails(users: List, predicate: (String) -> (Boolean)) : List { //… } findEmails(users, { value -> value.endsWith(".com") })

Slide 31

Slide 31 text

fun findEmails(users: List, predicate: (String) -> (Boolean)) : List { //… } findEmails(users, { value -> value.endsWith(".com") }) findEmails(users, { it.endsWith(".com") })

Slide 32

Slide 32 text

fun findEmails(users: List, predicate: (String) -> (Boolean)) : List { //… } findEmails(users, { value -> value.endsWith(".com") }) findEmails(users, { it.endsWith(".com") }) findEmails(users) { it.endsWith(".com") }

Slide 33

Slide 33 text

ྫ2: nullable

Slide 34

Slide 34 text

data class Money(val amount: Int, val currency: String) fun sumUp(money : Money?) { // money is nullable }

Slide 35

Slide 35 text

data class Money(val amount: Int, val currency: String) fun sumUp(money : Money?) { sumOfAmount += money!!.amount }

Slide 36

Slide 36 text

data class Money(val amount: Int, val currency: String) fun sumUp(money : Money?) { sumOfAmount += money!!.amount }

Slide 37

Slide 37 text

data class Money(val amount: Int, val currency: String) fun sumUp(money : Money?) { if (money != null) { sumOfAmount += money.amount } }

Slide 38

Slide 38 text

data class Money(val amount: Int, val currency: String) fun sumUp(money : Money?) { if (money != null) { sumOfAmount += money.amount } } fun sumUp(money : Money?) { money ?: return sumOfAmount += money.amount }

Slide 39

Slide 39 text

data class Money(val amount: Int, val currency: String) fun sumUp(money : Money?) { if (money != null) { sumOfAmount += money.amount } } fun sumUp(money : Money?) { money ?: return sumOfAmount += money.amount } fun sumUp(money : Money?) { sumOfAmount += money?.amount ?: 0 }

Slide 40

Slide 40 text

data class Money(val amount: Int, val currency: String) fun sumUp(money : Money?) { if (money != null) { sumOfAmount += money.amount } } fun sumUp(money : Money?) { money ?: return sumOfAmount += money.amount } fun sumUp(money : Money?) { sumOfAmount += money?.amount ?: 0 } fun sumUp(money : Money?) { money?.run { sumOfAmount += amount } }

Slide 41

Slide 41 text

ྫ3: είʔϓؔ਺

Slide 42

Slide 42 text

companion object { private const val KEY_USER = “user” fun newInstance(user: User): UserFragment { val fragment = UserFragment() val args = Bundle() args.putParcelable(KEY_USER, user) fragment.argument = args return fragment } }

Slide 43

Slide 43 text

companion object { private const val KEY_USER = “user” fun newInstance(user: User): UserFragment { val fragment = UserFragment() val args = Bundle() args.putParcelable(KEY_USER, user) fragment.argument = args return fragment } fun newInstance(user: User): UserFragment { val fragment = UserFragment() fragment.argument = Bundle().apply { putParcelable(KEY_USER, user) } return fragment } }

Slide 44

Slide 44 text

companion object { private const val KEY_USER = “user” fun newInstance(user: User): UserFragment { val fragment = UserFragment() val args = Bundle() args.putParcelable(KEY_USER, user) fragment.argument = args return fragment } fun newInstance(user: User): UserFragment { val fragment = UserFragment() fragment.argument = Bundle().apply { putParcelable(KEY_USER, user) } return fragment } fun newInstance(user: User) = UserFragment().apply { arguments = Bundle().apply { putParcelable(KEY_USER, user) } } }

Slide 45

Slide 45 text

είʔϓؔ਺ • let / with / run /apply / also • Kotlin είʔϓؔ਺ ༻్·ͱΊ@Qiita • http://qiita.com/ngsw_taro/items/d29e3080d9fc8a38691e • inline prefix public inline fun T.run(block: T.() -> R): R = block()

Slide 46

Slide 46 text

Kotlin͸ࣗ༝౓͕ߴ͍

Slide 47

Slide 47 text

ٞ࿦͠ͳ͕ΒKotlinΛॻ͘ͷ͕ۙಓ

Slide 48

Slide 48 text

νʔϜͰͷKotlinֶशͷྫ • 4ਓͷAndroidνʔϜͰKotlinܦݧऀ͸ࢲ͚ͩ • PRͰϨϏϡʔ • ि1ͰKotlinษڧձ • Kotlin Koans࢖ͬͯɺͳͥͦͷ౴͑ʹͳΔͷ͔ͷղઆ • ผղ΍࣮ࡍͷϓϩμΫτͷࣄྫΛࣔ͠ͳ͕Β͞Βʹৄ͘͠ղઆ

Slide 49

Slide 49 text

͓·͚

Slide 50

Slide 50 text

͓·͚ • Google I/O 2017 ͱ͍͑͹ɺʰArchitecture Componentʱ • github.com/googlesamples/android-architecture-components • ্هʹαϯϓϧ͕ެ։͞Ε͍ͯΔ • GithubBrowserSample - An advanced sample that uses the Architecture components, Dagger and the Github API. Requires Android Studio 3.0 canary 1

Slide 51

Slide 51 text

͓·͚ • ʰKotlin + Architecture Component + Dagger2ʹΑΔAndroidΞϓϦઃܭʱ@Qiita • goo.gl/z1p8zP • Ұ൪ϋϚͬͨͷ͸Dagger2ͷMultibindingػೳΛ࢖͏͜ͱ

Slide 52

Slide 52 text

//Kotlin class ViewModelFactory @Inject constructor(private val creators: Map, Provider>)

Slide 53

Slide 53 text

//Kotlin class ViewModelFactory @Inject constructor(private val creators: Map, Provider>) //Java expected Map, Provider>

Slide 54

Slide 54 text

//Kotlin class ViewModelFactory @Inject constructor(private val creators: Map, Provider>) //Java expected Map, Provider> //Java actual Map,? extends Provider> //Java in java.inject package public interface Provider

Slide 55

Slide 55 text

//Kotlin class ViewModelFactory @Inject constructor(private val creators: Map, @JvmSuppressWildcards Provider>)

Slide 56

Slide 56 text

//Kotlin class ViewModelFactory @Inject constructor(private val creators: Map, @JvmSuppressWildcards Provider>) //Java expected Map, Provider> //Java actual Map, Provider>

Slide 57

Slide 57 text

For ‘100% interoperable with Java’ • @JvmSuppressWildcards • @JvmMultifileClass • @JvmField • @JvmStatic • @JvmOverloads

Slide 58

Slide 58 text

JavaͰͲ͏ͳ͍ͬͯΔͷ͔Λ૝૾͠ͳ͕Β KotlinΛॻ͘

Slide 59

Slide 59 text

How to start Kotlin • Official Site : kotl.in/ , kotl.in/android • Kotlin is awesome! (kotlin.link/) • Kotlin ελʔτϒοΫ(੺΂͜ຊ) • Kotlin Advent Calendar (2015/2016) • Kotlinೖ໳·Ͱͷॿ૸ಡຊ( goo.gl/5vUT7o )

Slide 60

Slide 60 text

Thank you ! Let’s enjoy Kotlin