Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin in Google IO 2017 FINAL CA.kt

Kotlin in Google IO 2017 FINAL CA.kt

satorufujiwara

June 15, 2017
Tweet

More Decks by satorufujiwara

Other Decks in Programming

Transcript

  1. Kotlin & Me FRESH! ΛKotlinͰ։ൃ (2015೥3݄ʙ) Kotlin Advent Calendar (2015/2016)

    -ʮ2016೥ɺKotlinͰAndroid։ൃ͢Δํ΁ʯ @Qiita ( goo.gl/0medax ) Kotlinೖ໳·Ͱͷॿ૸ಡຊ ( goo.gl/5vUT7o )
  2. 4 big themes in Android • Kotlin • Android Studio

    & Libraries • App Quality & Success • Android Instant App
  3. Kotlin in Developer Keynote • First-class Language ʹ • γϯλοΫεϊΠζͷগͳ͍ඒ͍͠ݴޠ

    • Android Studio 3.0 ʹ૊Έࠐ·ΕΔ • Java8 ͷ͞ΒͳΔαϙʔτ(Android O)
  4. 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" }
  5. 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ϥΠηϯεͷΦʔϓϯιʔε͕ͩɺ͜Ε͔Β΋ΦʔϓϯιʔεϓϩδΣΫτ ͱͯ͠ඇӦརࡒஂ͕։ൃ͍ͯ͘͠
  6. Sessions about Kotlin • Introduction to Kotlin • Life is

    Great and Everything Will Be Ok, Kotlin is Here
  7. 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
  8. FRESH! ͰͷKotlin։ൃ • 2015೥3݄ Kotlinݕূ։࢝ɺ4݄͔Βຊ֨։ൃ (Kotlin M11) • 2016೥1݄21೔ αʔϏεϩϯν

    (Kotlin 1.0-beta3) • ʮ2016೥ɺKotlinͰAndroid։ൃ͢Δํ΁ʯ @Qiita ( goo.gl/0medax )
  9. FRESH! ͰKotlinΛ࠾༻ͨ͠ཧ༝ • ݴޠ࢓༷ (Null Safety, Lambda, Collections) • JetBrains

    • 100% interoperable with Java -> ʮԿ͔͋ͬͨΒJavaʹ໭ΔΜͰʯΛޱบʹͯ͠։ൃ
  10. fun findEmails(users: List<User>, predicate: (String) -> (Boolean)) : List<User> {

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

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

    //… } findEmails(users, { value -> value.endsWith(".com") }) findEmails(users, { it.endsWith(".com") }) findEmails(users) { it.endsWith(".com") }
  13. data class Money(val amount: Int, val currency: String) fun sumUp(money

    : Money?) { if (money != null) { sumOfAmount += money.amount } }
  14. 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 }
  15. 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 }
  16. 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 } }
  17. 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 } }
  18. 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 } }
  19. 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) } } }
  20. είʔϓؔ਺ • let / with / run /apply / also

    • Kotlin είʔϓؔ਺ ༻్·ͱΊ@Qiita • http://qiita.com/ngsw_taro/items/d29e3080d9fc8a38691e • inline prefix public inline fun <T, R> T.run(block: T.() -> R): R = block()
  21. ͓·͚ • 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
  22. //Kotlin class ViewModelFactory @Inject constructor(private val creators: Map<Class<out ViewModel>, Provider<ViewModel>>)

    //Java expected Map<Class<? extends ViewModel>, Provider<ViewModel>> //Java actual Map<Class<? extends ViewModel>,? extends Provider<ViewModel>> //Java in java.inject package public interface Provider<T>
  23. //Kotlin class ViewModelFactory @Inject constructor(private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards

    Provider<ViewModel>>) //Java expected Map<Class<? extends ViewModel>, Provider<ViewModel>> //Java actual Map<Class<? extends ViewModel>, Provider<ViewModel>>
  24. 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 )