Slide 1

Slide 1 text

KotlinͱϞμϯͳϥΠϒϥϦʔ ͰAndroidΞϓϦΛ࡞Δͬ ͨ͘͡ (@takuji31)

Slide 2

Slide 2 text

@takuji31 (Takuji Nishibayashi) Application Engineer at Hatena

Slide 3

Slide 3 text

Loves Kotlin

Slide 4

Slide 4 text

KotlinͰΞϓϦ։ൃத (ϓϥΠϕʔτͰ)

Slide 5

Slide 5 text

AGENDA • KotlinͰAndroidΞϓϦΛ࡞Δ • KotlinͰJavaͷϥΠϒϥϦʔΛ࢖͏ • KotlinͷϥΠϒϥϦʔΛ࢖͏

Slide 6

Slide 6 text

Kotlin 1.0.0 released

Slide 7

Slide 7 text

Kotlin 1.0.1-2 released

Slide 8

Slide 8 text

KotlinͰ AndroidΞϓ ϦΛ࡞Δ

Slide 9

Slide 9 text

Android Java = Java6 + α

Slide 10

Slide 10 text

+ α

Slide 11

Slide 11 text

Diamond operator

Slide 12

Slide 12 text

Multi catch

Slide 13

Slide 13 text

try-with-resources (4.4~)

Slide 14

Slide 14 text

ͭΒ͍

Slide 15

Slide 15 text

Kotlin

Slide 16

Slide 16 text

Null-safe

Slide 17

Slide 17 text

Property

Slide 18

Slide 18 text

Delegate

Slide 19

Slide 19 text

Lambda expression

Slide 20

Slide 20 text

Extension

Slide 21

Slide 21 text

Smart cast

Slide 22

Slide 22 text

ߴ͍Javaͱͷ ૬ޓӡ༻ੑ

Slide 23

Slide 23 text

JetBrainsࣗମ͕ Androidαϙʔτʹྗ ΛೖΕ͍ͯΔ

Slide 24

Slide 24 text

࠷ߴ

Slide 25

Slide 25 text

KotlinͰJava ͷϥΠϒϥϦʔ Λ࢖͏

Slide 26

Slide 26 text

Dagger2

Slide 27

Slide 27 text

ίϯύΠϧλΠϜʹ ґଘΛղܾ͢Δ DIίϯςφ

Slide 28

Slide 28 text

Component/Module

Slide 29

Slide 29 text

Module͕ ґଘΛఏڙ

Slide 30

Slide 30 text

Component͕ ґଘΛ஫ೖ

Slide 31

Slide 31 text

KotlinͰͷ࢖͍ํ

Slide 32

Slide 32 text

build.gradle dependencies { compile 'com.google.dagger:dagger:2.1'
 kapt "com.google.dagger:dagger-compiler:2.1"
 compile 'javax.annotation:javax.annotation-api:1.2'
 } kapt {
 generateStubs = true
 }


Slide 33

Slide 33 text

build.gradle dependencies { compile 'com.google.dagger:dagger:2.1'
 kapt "com.google.dagger:dagger-compiler:2.1"
 compile 'javax.annotation:javax.annotation-api:1.2'
 } kapt {
 generateStubs = true
 }
 LBQUͰ,PUMJOͷΫϥεʹ ରͯ͠"15Λ࣮ߦͰ͖ΔΑ͏ʹ͢Δ

Slide 34

Slide 34 text

build.gradle dependencies { compile 'com.google.dagger:dagger:2.1'
 kapt "com.google.dagger:dagger-compiler:2.1"
 compile 'javax.annotation:javax.annotation-api:1.2'
 } kapt {
 generateStubs = true
 }
 LBQUͰ,PUMJOͷΫϥεʹ ରͯ͠"15Λ࣮ߦͰ͖ΔΑ͏ʹ͢Δ ίϯύΠϧ࣌ʹ+BWBͰTUVCϑΝΠϧΛੜ੒ ͢Δ͜ͱͰɺ"15Ͱੜ੒͞ΕͨΫϥεΛ ,PUMJOͰ΋ѻ͑ΔΑ͏ʹ͢Δ

Slide 35

Slide 35 text

Module @Module
 class AppModule(@get:Provides val context: Context) {
 @Provides fun notificationManager(context: Context) : NotificationManager = context.getSystemService( Context.NOTIFICATION_SERVICE) as NotificationManager
 }

Slide 36

Slide 36 text

Module @Module
 class AppModule(@get:Provides val context: Context) {
 @Provides fun notificationManager(context: Context) : NotificationManager = context.getSystemService( Context.NOTIFICATION_SERVICE) as NotificationManager
 } (FUUFSʹ1SPWJEFTBOOPUBUJPOΛ͚ͭΔ

Slide 37

Slide 37 text

Module @Module
 class AppModule(@get:Provides val context: Context) {
 @Provides fun notificationManager(context: Context) : NotificationManager = context.getSystemService( Context.NOTIFICATION_SERVICE) as NotificationManager
 } (FUUFSʹ1SPWJEFTBOOPUBUJPOΛ͚ͭΔ ґଘΛఏڙ͢Δϝιουʹ 1SPWJEFTBOOPUBUJPOΛ͚ͭΔ

Slide 38

Slide 38 text

Component @Component(modules = arrayOf(AppModule::class))
 interface AppComponent {
 val context: Context
 val notificationManager: NotificationManager
 
 fun inject(mainActivity: MainActivity)
 }

Slide 39

Slide 39 text

Component @Component(modules = arrayOf(AppModule::class))
 interface AppComponent {
 val context: Context
 val notificationManager: NotificationManager
 
 fun inject(mainActivity: MainActivity)
 } ௚઀औಘ͢ΔͨΊʹϓϩύςΟʔ ϝιουͰ΋Α͍ ΛఆٛͰ͖Δ

Slide 40

Slide 40 text

Component @Component(modules = arrayOf(AppModule::class))
 interface AppComponent {
 val context: Context
 val notificationManager: NotificationManager
 
 fun inject(mainActivity: MainActivity)
 } ௚઀औಘ͢ΔͨΊʹϓϩύςΟʔ ϝιουͰ΋Α͍ ΛఆٛͰ͖Δ ஫ೖ͢ΔͨΊͷϝιουΛ ఆٛ͢Δ

Slide 41

Slide 41 text

Application class class App : Application() {
 val component : AppComponent by lazy {
 DaggerAppComponent .builder().appModule(AppModule(this)).build()
 }
 override fun onCreate() {
 super.onCreate()
 }
 }

Slide 42

Slide 42 text

Application class class App : Application() {
 val component : AppComponent by lazy {
 DaggerAppComponent .builder().appModule(AppModule(this)).build()
 }
 override fun onCreate() {
 super.onCreate()
 }
 } $PNQPOFOUΛอ࣋͢ΔͨΊͷ ϓϩύςΟʔΛ࡞Δ

Slide 43

Slide 43 text

Application class class App : Application() {
 val component : AppComponent by lazy {
 DaggerAppComponent .builder().appModule(AppModule(this)).build()
 }
 override fun onCreate() {
 super.onCreate()
 }
 } $PNQPOFOUΛอ࣋͢ΔͨΊͷ ϓϩύςΟʔΛ࡞Δ MB[ZΛ࢖͏͜ͱͰ ॳճΞΫηε࣌ʹॳظԽ

Slide 44

Slide 44 text

Application class class App : Application() {
 val component : AppComponent by lazy {
 DaggerAppComponent .builder().appModule(AppModule(this)).build()
 }
 override fun onCreate() {
 super.onCreate()
 }
 } $PNQPOFOUΛอ࣋͢ΔͨΊͷ ϓϩύςΟʔΛ࡞Δ MB[ZΛ࢖͏͜ͱͰ ॳճΞΫηε࣌ʹॳظԽ "15Ͱੜ੒͞ΕͨΫϥεΛ ࡞ͬͯฦ͢

Slide 45

Slide 45 text

Target class MainActivity : AppCompatActivity() {
 @Inject
 lateinit var context : Context
 
 @Inject
 lateinit var notificationManager : NotificationManager
 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 setContentView(R.layout.activity_main)
 app.component.inject(mainActivity = this)
 }
 }

Slide 46

Slide 46 text

Target class MainActivity : AppCompatActivity() {
 @Inject
 lateinit var context : Context
 
 @Inject
 lateinit var notificationManager : NotificationManager
 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 setContentView(R.layout.activity_main)
 app.component.inject(mainActivity = this)
 }
 } *OKFDUBOOPUBUJPOΛ͚ͭͨ QSPQFSUZ΍ϝιουʹґଘ஫ೖ͞ΕΔ

Slide 47

Slide 47 text

Target class MainActivity : AppCompatActivity() {
 @Inject
 lateinit var context : Context
 
 @Inject
 lateinit var notificationManager : NotificationManager
 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 setContentView(R.layout.activity_main)
 app.component.inject(mainActivity = this)
 }
 } *OKFDUBOOPUBUJPOΛ͚ͭͨ QSPQFSUZ΍ϝιουʹґଘ஫ೖ͞ΕΔ MBUFJOJUΛ͚ͭΔ͜ͱͰॳظԽ͕ෆཁʹͳΓɺ ͔ͭϑΟʔϧυʹ+BWB͔Β௚઀୅ೖͰ͖ΔΑ͏ʹ

Slide 48

Slide 48 text

Target class MainActivity : AppCompatActivity() {
 @Inject
 lateinit var context : Context
 
 @Inject
 lateinit var notificationManager : NotificationManager
 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 setContentView(R.layout.activity_main)
 app.component.inject(mainActivity = this)
 }
 } *OKFDUBOOPUBUJPOΛ͚ͭͨ QSPQFSUZ΍ϝιουʹґଘ஫ೖ͞ΕΔ MBUFJOJUΛ͚ͭΔ͜ͱͰॳظԽ͕ෆཁʹͳΓɺ ͔ͭϑΟʔϧυʹ+BWB͔Β௚઀୅ೖͰ͖ΔΑ͏ʹ $PNQPOFOUͷJOKFDUϝιουʹର৅ͷΠϯελϯε Λ౉͢ BQQ͸"QQΛऔಘ͢Δ֦ுϓϩύςΟʔ

Slide 49

Slide 49 text

Data Binding Library

Slide 50

Slide 50 text

Androidެࣜͷσʔ λʔόΠϯσΟϯά ϥΠϒϥϦʔ

Slide 51

Slide 51 text

Android Studio ʹΑΔαϙʔτ (ίʔυิ׬ɺγϯ λοΫεϋΠϥΠτ)

Slide 52

Slide 52 text

Ұํ޲/૒ํ޲ͷόΠ ϯσΟϯάαϙʔτ

Slide 53

Slide 53 text

KotlinͰ΋ ࢖͑Δ

Slide 54

Slide 54 text

build.gradle android {
 dataBinding {
 enabled = true
 }
 }
 
 dependencies {
 kapt 'com.android.databinding:compiler:2.1.0-alpha5'
 }
 kapt {
 generateStubs = true
 }

Slide 55

Slide 55 text

όΠϯυ͍ͨ͠ΫϥεΛ࡞Δ data class User(val name: String, val birthDay: String?)

Slide 56

Slide 56 text

ϨΠΞ΢τͷXMLͰ όΠϯσΟϯάΛఆٛ

Slide 57

Slide 57 text

ϨΠΞ΢τͷXML(Ұ෦ུ) 
 
 
 
 
 
 
 
 
 
 
 


Slide 58

Slide 58 text

ϨΠΞ΢τͷXML(Ұ෦ུ) 
 
 
 
 
 
 
 
 
 
 
 
 ϧʔτཁૉ͸MBZPVU

Slide 59

Slide 59 text

ϨΠΞ΢τͷXML(Ұ෦ུ) 
 
 
 
 
 
 
 
 
 
 
 
 ϧʔτཁૉ͸MBZPVU όΠϯσΟϯάΫϥεͷ ϓϩύςΟʔΛఆٛ

Slide 60

Slide 60 text

ϨΠΞ΢τͷXML(Ұ෦ུ) 
 
 
 
 
 
 
 
 
 
 
 
 ϧʔτཁૉ͸MBZPVU όΠϯσΟϯάΫϥεͷ ϓϩύςΟʔΛఆٛ ఆٛͨ͠ϓϩύςΟʔ΍ ͦͷϓϩύςΟʔΛόΠ ϯσΟϯά

Slide 61

Slide 61 text

ϨΠΞ΢τͷXML(Ұ෦ུ) 
 
 
 
 
 
 
 
 
 
 
 
 ϧʔτཁૉ͸MBZPVU όΠϯσΟϯάΫϥεͷ ϓϩύςΟʔΛఆٛ +BWBಉ༷ʹJNQPSUՄೳ ఆٛͨ͠ϓϩύςΟʔ΍ ͦͷϓϩύςΟʔΛόΠ ϯσΟϯά

Slide 62

Slide 62 text

ϨΠΞ΢τͷXML(Ұ෦ུ) 
 
 
 
 
 
 
 
 
 
 
 
 ϧʔτཁૉ͸MBZPVU όΠϯσΟϯάΫϥεͷ ϓϩύςΟʔΛఆٛ +BWBಉ༷ʹJNQPSUՄೳ ఆٛͨ͠ϓϩύςΟʔ΍ ͦͷϓϩύςΟʔΛόΠ ϯσΟϯά Ұ෦ͷԋࢉࢠ΋࢖͑Δ

Slide 63

Slide 63 text

ϨΠΞ΢τͷXML(Ұ෦ུ) 
 
 
 
 
 
 
 
 
 
 
 
 ϧʔτཁૉ͸MBZPVU όΠϯσΟϯάΫϥεͷ ϓϩύςΟʔΛఆٛ +BWBಉ༷ʹJNQPSUՄೳ ఆٛͨ͠ϓϩύςΟʔ΍ ͦͷϓϩύςΟʔΛόΠ ϯσΟϯά Ұ෦ͷԋࢉࢠ΋࢖͑Δ 0O$MJDL-JTUFOFS΋ઃఆՄ

Slide 64

Slide 64 text

ఆٛΛݩʹAPTͰ ΫϥεΛࣗಈੜ੒

Slide 65

Slide 65 text

όΠϯυ͢Δ class DataClassActivity : AppCompatActivity() {
 var users = listOf(
 User(name = "takuji31", birthDay = "1987/03/01"),
 User(name = "takuji32", birthDay = "1987/03/02"),
 User(name = "takuji33", birthDay = "1987/03/03"),
 User(name = "takuji24884", birthDay = null)
 )
 val binding: ActivityDataClassBinding by lazy {
 DataBindingUtil.setContentView(this, R.layout.activity_data_class)
 }
 override fun onCreate(savedInstanceState: Bundle?) {
 binding.user = users.last()
 binding.randomButtonClickListener = View.OnClickListener {
 binding.user = users[0]
 users = users.drop(1) + binding.user
 }
 }
 }

Slide 66

Slide 66 text

όΠϯυ͢Δ class DataClassActivity : AppCompatActivity() {
 var users = listOf(
 User(name = "takuji31", birthDay = "1987/03/01"),
 User(name = "takuji32", birthDay = "1987/03/02"),
 User(name = "takuji33", birthDay = "1987/03/03"),
 User(name = "takuji24884", birthDay = null)
 )
 val binding: ActivityDataClassBinding by lazy {
 DataBindingUtil.setContentView(this, R.layout.activity_data_class)
 }
 override fun onCreate(savedInstanceState: Bundle?) {
 binding.user = users.last()
 binding.randomButtonClickListener = View.OnClickListener {
 binding.user = users[0]
 users = users.drop(1) + binding.user
 }
 }
 } MB[Z΍OPU/VMMͷEFMFHBUFΛ࢖ͬͯ όΠϯσΟϯάΫϥεͷ ϓϩύςΟʔΛ༻ҙ͢ΔͱΑ͍

Slide 67

Slide 67 text

όΠϯυ͢Δ class DataClassActivity : AppCompatActivity() {
 var users = listOf(
 User(name = "takuji31", birthDay = "1987/03/01"),
 User(name = "takuji32", birthDay = "1987/03/02"),
 User(name = "takuji33", birthDay = "1987/03/03"),
 User(name = "takuji24884", birthDay = null)
 )
 val binding: ActivityDataClassBinding by lazy {
 DataBindingUtil.setContentView(this, R.layout.activity_data_class)
 }
 override fun onCreate(savedInstanceState: Bundle?) {
 binding.user = users.last()
 binding.randomButtonClickListener = View.OnClickListener {
 binding.user = users[0]
 users = users.drop(1) + binding.user
 }
 }
 } MB[Z΍OPU/VMMͷEFMFHBUFΛ࢖ͬͯ όΠϯσΟϯάΫϥεͷ ϓϩύςΟʔΛ༻ҙ͢ΔͱΑ͍ ࣗಈੜ੒͞ΕΔΫϥεʹ͸ఆ໊ٛͨ͠લͱಉ ͡ϓϩύςΟʔ ਖ਼֬ʹ͸TFUUFSHFUUFS ͕ ੜ੒͞ΕΔͷͰɺηοτ͢Δͱ஋͕൓ө͞Ε Δ

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

(ViewClass)findById(R.id.vi ewId)͸΋͏ෆཁͰ͢

Slide 71

Slide 71 text

Define binding 
 
 


Slide 72

Slide 72 text

Define binding 
 
 
 JEΛTOBLF@DBTF͔ MPXFS$BNFM$BTFͰఆٛ

Slide 73

Slide 73 text

Field access class MainActivity : AppCompatActivity() {
 val binding : ActivityMainBinding by lazy {DataBindingUtil.setContentView(this, R.layout.activity_main)} 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 binding.recyclerView.layoutManager = LinearLayoutManager(this)
 }
 }

Slide 74

Slide 74 text

Field access class MainActivity : AppCompatActivity() {
 val binding : ActivityMainBinding by lazy {DataBindingUtil.setContentView(this, R.layout.activity_main)} 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 binding.recyclerView.layoutManager = LinearLayoutManager(this)
 }
 } MPXFS$BNFM$BTFͰϑΟʔϧ υ͕ੜ੒͞ΕΔ

Slide 75

Slide 75 text

Property observer

Slide 76

Slide 76 text

มߋ؂ࢹՄೳͳσʔλʔΫϥε class Counter : BaseObservable() {
 @get:Bindable
 var count : Int = 0
 set(value) {
 field = value
 notifyPropertyChanged(BR.count)
 }
 }


Slide 77

Slide 77 text

มߋ؂ࢹՄೳͳσʔλʔΫϥε class Counter : BaseObservable() {
 @get:Bindable
 var count : Int = 0
 set(value) {
 field = value
 notifyPropertyChanged(BR.count)
 }
 }
 มߋ؂ࢹΛ͍ͨ͠৔߹͸ #BTF0CTFSWBCMFΛܧঝ

Slide 78

Slide 78 text

มߋ؂ࢹՄೳͳσʔλʔΫϥε class Counter : BaseObservable() {
 @get:Bindable
 var count : Int = 0
 set(value) {
 field = value
 notifyPropertyChanged(BR.count)
 }
 }
 มߋ؂ࢹΛ͍ͨ͠৔߹͸ #BTF0CTFSWBCMFΛܧঝ ؂ࢹ͍ͨ͠ϓϩύςΟʔͷ HFUUFSʹ#JOEBCMF BOOPUBUJPOΛ͚ͭΔ

Slide 79

Slide 79 text

มߋ؂ࢹՄೳͳσʔλʔΫϥε class Counter : BaseObservable() {
 @get:Bindable
 var count : Int = 0
 set(value) {
 field = value
 notifyPropertyChanged(BR.count)
 }
 }
 มߋ؂ࢹΛ͍ͨ͠৔߹͸ #BTF0CTFSWBCMFΛܧঝ ؂ࢹ͍ͨ͠ϓϩύςΟʔͷ HFUUFSʹ#JOEBCMF BOOPUBUJPOΛ͚ͭΔ ஋͕มߋ͞Εͨ͜ͱΛ௨஌

Slide 80

Slide 80 text

όΠϯσΟϯάΛఆٛ 
 
 
 
 
 
 
 
 
 


Slide 81

Slide 81 text

Change value class PropertyObserverActivity : AppCompatActivity() {
 val binding : ActivityPropertyObserverBinding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_property_observer ) } 
 override fun onCreate(savedInstanceState: Bundle?) {
 val counter = Counter()
 binding.counter = counter
 binding.buttonClickListener = View.OnClickListener {
 counter.count += 1 }
 }
 }

Slide 82

Slide 82 text

Change value class PropertyObserverActivity : AppCompatActivity() {
 val binding : ActivityPropertyObserverBinding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_property_observer ) } 
 override fun onCreate(savedInstanceState: Bundle?) {
 val counter = Counter()
 binding.counter = counter
 binding.buttonClickListener = View.OnClickListener {
 counter.count += 1 }
 }
 } ஋Ληοτ͢Δ͚ͩͰ มߋ͕൓ө͞ΕΔ

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

Binding adapter

Slide 86

Slide 86 text

Viewͷଐੑʹ஋Λ ηοτ͢ΔॲཧΛఆٛ ͢Δ࢓૊Έ

Slide 87

Slide 87 text

BindingAdapterΛఆٛ object Adapters {
 @JvmStatic
 @BindingAdapter("android:text")
 fun convertZonedDateTimeToString (textView: TextView, zonedDateTime : ZonedDateTime) {
 val timeString = zonedDateTime.format( DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm:SS"))
 textView.text = timeString
 }
 }

Slide 88

Slide 88 text

BindingAdapterΛఆٛ object Adapters {
 @JvmStatic
 @BindingAdapter("android:text")
 fun convertZonedDateTimeToString (textView: TextView, zonedDateTime : ZonedDateTime) {
 val timeString = zonedDateTime.format( DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm:SS"))
 textView.text = timeString
 }
 } +WN4UBUJDBOOPUBUJPOΛ ͚ͭΔ͜ͱͰTUBUJDϝιουͱͯ͠ఆٛ͞ΕΔ

Slide 89

Slide 89 text

BindingAdapterΛఆٛ object Adapters {
 @JvmStatic
 @BindingAdapter("android:text")
 fun convertZonedDateTimeToString (textView: TextView, zonedDateTime : ZonedDateTime) {
 val timeString = zonedDateTime.format( DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm:SS"))
 textView.text = timeString
 }
 } +WN4UBUJDBOOPUBUJPOΛ ͚ͭΔ͜ͱͰTUBUJDϝιουͱͯ͠ఆٛ͞ΕΔ BOESPJEUFYUଐੑ΁஋Λ ηοτͰ͖Δ

Slide 90

Slide 90 text

BindingAdapterΛఆٛ object Adapters {
 @JvmStatic
 @BindingAdapter("android:text")
 fun convertZonedDateTimeToString (textView: TextView, zonedDateTime : ZonedDateTime) {
 val timeString = zonedDateTime.format( DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm:SS"))
 textView.text = timeString
 }
 } +WN4UBUJDBOOPUBUJPOΛ ͚ͭΔ͜ͱͰTUBUJDϝιουͱͯ͠ఆٛ͞ΕΔ BOESPJEUFYUଐੑ΁஋Λ ηοτͰ͖Δ 5FYU7JFXͱ;POFE%BUF5JNF ͷ૊Έ߹Θͤͷ࣌ʹݺ͹ΕΔ

Slide 91

Slide 91 text

ϨΠΞ΢τΛॻ͘ 
 
 
 
 


Slide 92

Slide 92 text

ϨΠΞ΢τΛॻ͘ 
 
 
 
 
 ஋ΛόΠϯυ͢Δ͚ͩͰ0,

Slide 93

Slide 93 text

஋Ληοτ͢Δ class NowActivity : AppCompatActivity() {
 
 val binding : ActivityNowBinding by lazy {
 DataBindingUtil.setContentView(this, R.layout.activity_now)
 }
 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 binding.now = ZonedDateTime.now()
 }
 }

Slide 94

Slide 94 text

஋Ληοτ͢Δ class NowActivity : AppCompatActivity() {
 
 val binding : ActivityNowBinding by lazy {
 DataBindingUtil.setContentView(this, R.layout.activity_now)
 }
 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 binding.now = ZonedDateTime.now()
 }
 } ஋Ληοτ͢Δ

Slide 95

Slide 95 text

Value converter

Slide 96

Slide 96 text

ಛఆͷΫϥεͷ஋Λ ViewʹઃఆͰ͖Δ஋ ʹม׵͢Δ࢓૊Έ

Slide 97

Slide 97 text

KotlinͰఆٛͯ͠΋ Data Binding Libraryͷ ίϯύΠϥʔ͕ೝࣝ͠ͳ͍

Slide 98

Slide 98 text

JavaͰॻ͖·͠ΐ͏

Slide 99

Slide 99 text

BindingAdapterΛఆٛ public class Converters {
 @BindingConversion
 public static ColorDrawable convertColorStringToColor(String colorString) {
 return new ColorDrawable(Color.parseColor(colorString));
 }
 }


Slide 100

Slide 100 text

BindingAdapterΛఆٛ public class Converters {
 @BindingConversion
 public static ColorDrawable convertColorStringToColor(String colorString) {
 return new ColorDrawable(Color.parseColor(colorString));
 }
 }
 $PMPS%SBXBCMF͕ηοτͰ͖Δଐੑʹ 4USJOHΛόΠϯυ͠Α͏ͱͨ࣌͠ʹݺ͹ΕΔ

Slide 101

Slide 101 text

ϨΠΞ΢τΛॻ͘ 
 
 
 
 


Slide 102

Slide 102 text

ϨΠΞ΢τΛॻ͘ 
 
 
 
 
 ஋ΛόΠϯυ͢Δ͚ͩͰ0,

Slide 103

Slide 103 text

஋Ληοτ͢Δ class ColoredActivity : AppCompatActivity() {
 
 val binding : ActivityColoredBinding by lazy {
 DataBindingUtil.setContentView(this, R.layout.activity_colored)
 }
 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 binding.backgroundColor = "#00FF00"
 }
 }


Slide 104

Slide 104 text

஋Ληοτ͢Δ class ColoredActivity : AppCompatActivity() {
 
 val binding : ActivityColoredBinding by lazy {
 DataBindingUtil.setContentView(this, R.layout.activity_colored)
 }
 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 binding.backgroundColor = "#00FF00"
 }
 }
 ஋Ληοτ͢Δ

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

example : github.com/ takuji31/ KotlinDataBinding

Slide 107

Slide 107 text

Realm

Slide 108

Slide 108 text

Post SQLite ͳ ϞόΠϧ޲͚ DBϥΠϒϥϦʔ

Slide 109

Slide 109 text

࣌ؒͳ͍ͷͰলུ

Slide 110

Slide 110 text

github.com/realm/ realm-java/tree/ master/examples/ kotlinExample

Slide 111

Slide 111 text

Orma

Slide 112

Slide 112 text

AnnotationͰఆٛΛ ॻΔAndroid޲͚ͷ O/R mapper

Slide 113

Slide 113 text

※Dagger2ͱ࢖͏৔߹͸ɺ ModuleΛJavaͰॻ͔ͳ͍ͱ ੜ੒͞ΕͨϑΝΠϧΛೝࣝͰ ͖ͳ͍

Slide 114

Slide 114 text

࣌ؒͳ͍ͷͰলུ

Slide 115

Slide 115 text

github.com/gfx/ Orma-Kotlin-Example

Slide 116

Slide 116 text

Kotlinͷ ϥΠϒϥϦʔ Λ࢖͏

Slide 117

Slide 117 text

anko

Slide 118

Slide 118 text

JetBrains੡ͷ Android༻ ศརϥΠϒϥϦʔ

Slide 119

Slide 119 text

DSLʹΑΔViewఆٛ (Android Studioͷ ϨΠΞ΢τϓϨϏϡʔରԠ)

Slide 120

Slide 120 text

AndroidͷContext ΁ͷศར֦ு (GenericsͰIntentੜ੒ɺ Α͘࢖͏Intentੜ੒ؔ਺ͳͲ)

Slide 121

Slide 121 text

SQLiteͷϔϧύʔ

Slide 122

Slide 122 text

ͳͲͳͲ

Slide 123

Slide 123 text

DSL͸Data Binding Libraryͱ͸૊Έ߹Θ ͤΒΕͳ͍

Slide 124

Slide 124 text

github.com/Kotlin/ anko

Slide 125

Slide 125 text

Koreference

Slide 126

Slide 126 text

SharedPreferencesͷ஋ʹϓ ϩύςΟʔΞΫηε͢ΔͨΊ ͷϥΠϒϥϦʔ

Slide 127

Slide 127 text

ಛ௃ • ؆୯ʹ Preference model తͳ΋ͷΛॻ͚Δ • Null-SafeͳSharedPreference • جຊతͳSharedPreferencesͷܕΛαϙʔτ • ֦ுՄೳͳDelegated Property • Gson αϙʔτ͋Γ

Slide 128

Slide 128 text

Easy to make Preference model public class MyPreference( pref : SharedPreferences ) : SharedPreferences by pref {
 public var name : String by stringPreference("")
 }

Slide 129

Slide 129 text

Null-Safe SharedPreferences public class MyPreference( pref : SharedPreferences ) : SharedPreferences by pref {
 public var firstName : String by stringPreference("")
 public var lastName : String by stringPreference("")
 public var middleName : String? by nullableStringPreference(null)
 }

Slide 130

Slide 130 text

All Preference type support public class MyPreference(pref : SharedPreferences) : SharedPreferences by pref {
 public var stringValue : String by stringPreference("default value")
 public var intValue : Int by intPreference(256)
 public var longValue : Long by longPreference(256L)
 public var floatValue : Float by floatPreference(12.34f)
 public var boolValue : Boolean by booleanPreference(true)
 public var stringSetValue : Set by stringSetPreference(setOf()) public var stringValue : String? by nullableStringPreference()
 public var stringSetValue : Set? by nullableStringSetPreference() 
 }

Slide 131

Slide 131 text

Easy to create custom delegate property public class ByteArrayPreference( default : ByteArray = byteArrayOf(), name : String? = null ) : KoreferenceDelegate(default,name), StringPreference {
 override fun toModelValue(value: String): ByteArray {
 return Base64.decode(value, Base64.DEFAULT)
 }
 
 override fun toPreferenceValue(value: ByteArray): String {
 return Base64.encodeToString(value, Base64.DEFAULT)
 }
 }

Slide 132

Slide 132 text

Easy to create custom delegate property public class RealmPreference( pref : SharedPreferences ) : SharedPreferences by pref {
 var encryptionKey : ByteArray by ByteArrayPreference()
 } val pref = getSharedPreferences("realm",Context.MODE_PRIVATE)
 val model = RealmPreference(pref)
 model.encryptionKey

Slide 133

Slide 133 text

github.com/takuji31/ Koreference

Slide 134

Slide 134 text

·ͱΊ • AndroidͰKotlinΛ࢖͏ͷ͸ݱ࣮తʹͳΓͭͭ͋Δ • JavaͷϥΠϒϥϦʔ͸͍͍ͩͨͦͷ··࢖͑Δ • APTΛ࢖͏ϥΠϒϥϦʔ΋Ұ෦Λআ͍ͯී௨ʹ࢖͑Δ • Kotlinಛ༗ͷػೳΛར༻ͨ͠ϥΠϒϥϦʔͰ΋ͬͱॻ ͖΍͘͢Ͱ͖Δ

Slide 135

Slide 135 text

ؔ੢Kotlinษڧձ ఆظ։࠵͢Δͧʂ

Slide 136

Slide 136 text

ଓใΛ͓଴͍ͪͩ͘͞

Slide 137

Slide 137 text

Enjoy Kotlin Android life!