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

How to start Kotlin APP DOJO

How to start Kotlin APP DOJO

今から始めるKotlinによるAndroidアプリ開発
2017/08/29 APP DOJO August 2017 @ Google Japan

satorufujiwara

August 29, 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 ) APP DOJO ʮKotlin in Google I/O 2017ʯ(2017/05/25) Kotlinೖ໳·Ͱͷॿ૸ಡຊ ( goo.gl/5vUT7o )
  2. Kotlin in Action • ຋༁ຊग़·͢ʂ • ༁ऀ • ௕ᖒଠ࿠ @ngsw_taro

    • ࢁຊ७ฏ @boohbah • yy_yank @yy_yank • ౻ݪ੟ @satorufujiwara
  3. Kotlin ͕໨ࢦ͍ͯ͠Δͱ͜Ζ • ݱࡏ Java ͕࢖Θ͍ͯΔ؀ڥʹɺΑΓ؆ܿ ͰɺΑΓੜ࢈ੑ͕ߴ͘ɺΑΓ ҆શͳ Java ͷ୅ସݴޠΛఏڙ͢Δ͜ͱ

    • ݱঢ়ͩͱAndroidͱαʔόʔαΠυ • কདྷతʹ͸JavaScript؀ڥ΍ɺଞͷ؀ڥ(iOSͳͲ)ʹ΋ରԠ
  4. Kotlin ͕໨ࢦ͍ͯ͠Δͱ͜Ζ • ݱࡏ Java ͕࢖Θ͍ͯΔ؀ڥʹɺΑΓ؆ܿ ͰɺΑΓੜ࢈ੑ͕ߴ͘ɺΑΓ ҆શͳ Java ͷ୅ସݴޠΛఏڙ͢Δ͜ͱ

    • ݱঢ়ͩͱAndroidͱαʔόʔαΠυ • কདྷతʹ͸JavaScript؀ڥ΍ɺଞͷ؀ڥ(iOSͳͲ)ʹ΋ରԠ
  5. data class Person(val name: String, val age: Int) fun findTheOldest(people:

    List<Person>): Person { var maxAge = 0 var theOldest: Person? = null for (person in people) { if (person.age > maxAge) { maxAge = person.age theOldest = person } } return theOldest }
  6. data class Person(val name: String, val age: Int) fun findTheOldest(people:

    List<Person>): Person { var maxAge = 0 var theOldest: Person? = null for (person in people) { if (person.age > maxAge) { maxAge = person.age theOldest = person } } return theOldest } fun findTheOldest(people: List<Person>) = persons.maxBy { it.age }
  7. data class Person(val name: String, val age: Int) fun findTheOldest(people:

    List<Person>) = persons.maxBy{ it.age } σʔλΫϥε ϥϜμࣜ ؔ਺ ϥϜμ ΛҾ਺ʹऔΔ֦ுؔ਺ ෆม
  8. //Java public final class Person { private final String name;

    private final int age; public Person(String name, int age) { this.name = name; this.age = age; } public final String getName() { return this.name; } public final int getAge() { return this.age; } public final Person copy(String name, int age) { return new Person(name, age); } public String toString() { return "Person(name=" + this.name + ", age=" + this.age + ")"; } public int hashCode() { return (this.name != null?this.name.hashCode():0) * 31 + this.age; } public boolean equals(Object var1) { //… } }
  9. Kotlin ͷߟ͑ํ • ࣮༻ओٛ - Javaͷߟ͑ํ͕ͦͷ··࢖͑Δɻ৽͍͠ػೳ͸طଘͷ΋ͷΛར ༻ɻֶश͕༰қɻ • ؆ܿ -

    ಡΈ΍͢͞ΛॏࢹɻϘΠϥʔϓϨʔτ͸ۃྗݮΒ͢ɻ • ҆શ - ੩తܕ෇͚ɻNull҆શɻεϚʔτΩϟετɻ • ૬ޓӡ༻ੑ - JavaͱKotlinͷؒ͸ࣗ༝ʹߦ͖དྷɻϥΠϒϥϦ΋طଘͷ΋ͷ Λͦͷ··ར༻Ͱ͖Δɻ
  10. Kotlin ͷݴޠػೳ • ม਺એݴ(val / var) & ܕਪ࿦ • Data

    classͱϓϩύςΟ • ੍ޚߏจ (if / when / for / when) • Nullڐ༰ܕ / Nullඇڐ༰ܕ • Lambda(ϥϜμ) • ඪ४ϥΠϒϥϦ & ֦ுؔ਺
  11. Kotlin ͷݴޠػೳ • ม਺એݴ(val / var) & ܕਪ࿦ • Data

    classͱϓϩύςΟ • ੍ޚߏจ (if / when / for / when) • Nullڐ༰ܕ / Nullඇڐ༰ܕ • Lambda(ϥϜμ) • ඪ४ϥΠϒϥϦ & ֦ுؔ਺
  12. ม਺એݴ(val / var) & ܕਪ࿦ ෆม(val) or Մม(var) Ͱ͋Δ͔Λࢦఆͯ͠ม਺Λએݴ ܕ͸ม਺ͷޙΖʹஔ͕͘ܕਪ࿦ग़དྷΔ৔߹͸লུՄೳ

    ෆม(val) ͸Javaͷfinalʹ૬౰͠ɺ࠶୅ೖͰ͖ͳ͍ Մม - mutable ,ෆม - immutable val answer = 42 var answer2 : Int = 42 answer = 4 //Error! answer2 = 4 //OK!
  13. ม਺એݴ(val / var) & ܕਪ࿦ จ຺తʹҰ౓͔͠ॳظԽ͞Εͳ͍৔߹΋val͕࢖༻Ͱ͖Δ val message: String if

    (canPerformOperation()) { message = "Success" // ... perform the operation } else { message = "Failed" }
  14. Kotlin ͷݴޠػೳ • ม਺એݴ(val / var) & ܕਪ࿦ • Data

    classͱϓϩύςΟ • ੍ޚߏจ (if / when / for / when) • Nullڐ༰ܕ / Nullඇڐ༰ܕ • Lambda(ϥϜμ) • ඪ४ϥΠϒϥϦ & ֦ுؔ਺
  15. Data class ίϯύΠϥͰͷϑΟʔϧυ΁ͷ୅ೖ getter / setter → ʮϓϩύςΟʯͱ͍͏ߟ͑ํ copy /

    toString / hasCode / equals ΛࣗಈͰੜ੒ data class Person(val name: String, var isMarried: Boolean)
  16. //Kotlin data class Person(val name: String, var isMarried: Boolean) //Kotlin

    >>> val person = Person("Bob", true) >>> println(person.name) Bob >>> println(person.isMarried) true
  17. //Kotlin data class Person(val name: String, var isMarried: Boolean) //Java

    >>> Person person = new Person("Bob", true); >>> System.out.println(person.getName()); Bob >>> System.out.println(person.isMarried()); true
  18. //Kotlin data class Person(val name: String, var isMarried: Boolean) //Java

    >>> Person person = new Person("Bob", true); >>> System.out.println(person.getName()); Bob >>> System.out.println(person.isMarried()); true person.setName("Alice") //Error! person.setMarried(false) //OK!
  19. //Kotlin data class Person(val name: String, var isMarried: Boolean) //Kotlin

    >>> val person = Person("Bob", true) >>> println(person.name) Bob >>> println(person.isMarried) true person.name = "Alice" //Error! person.isMarried = false //OK!
  20. Kotlin ͷݴޠػೳ • ม਺એݴ(val / var) & ܕਪ࿦ • Data

    classͱϓϩύςΟ • ੍ޚߏจ (if / when / for / while) • Nullڐ༰ܕ / Nullඇڐ༰ܕ • Lambda(ϥϜμ) • ඪ४ϥΠϒϥϦ & ֦ுؔ਺
  21. ੍ޚߏจ • if - Javaͱ΄΅ಉ͡ • when - JavaͷswitchΛڧྗʹͨ͠΋ͷ(Kotlinʹswitchจ͸ແ͍) •

    for - Javaͷfor-each(‘for (String item : list )’ͳͲ)͔͠ͳ͍ • while - Javaͱશ͘ಉ͡
  22. whenจ ڧྗͳswitchจ Javaͱҧ͍ɺϓϦϛςΟϒܕɺenumܕҎ֎ʹ΋࢖͑Δ fun getColorName(color: Color) = when (color) {

    Color.RED -> "Red" Color.ORANGE -> "Orange" Color.YELLOW -> "Yellow" else -> "Unknown" }
  23. whenจ ৚݅ʹෳ਺ͷ߲໨ΛೖΕΔ͜ͱ͕Մೳ fun getWarmth(color: Color) = when (color) { RED,

    ORANGE, YELLOW -> "warm" GREEN -> "neutral" BLUE, INDIGO, VIOLET -> "cold" }
  24. Kotlin ͷݴޠػೳ • ม਺એݴ(val / var) & ܕਪ࿦ • Data

    classͱϓϩύςΟ • ੍ޚߏจ (if / when / for / when) • Nullڐ༰ܕ / Nullඇڐ༰ܕ • Lambda(ϥϜμ) • ඪ४ϥΠϒϥϦ & ֦ுؔ਺
  25. Nullڐ༰ܕ / Nullඇڐ༰ܕ • Ͱ͖Δ͚ͩNullඇڐ༰ܕ(Non-Null)Ͱఆٛ͢Δ • ඞཁͳ৔໘͚ͩʹNullڐ༰ܕ(Nullable)Λ࢖͏ • Nullڐ༰ܕ͸ಛผʹѻ͏ඞཁ͕͋Δ •

    Kotlin಺Ͱ͸ඞͣͲͪΒ͔ͷܕʹͳΔ͕ɺϓϥοτϑΥʔϜܕ(ܕ໊ͷޙΖʹ!)ͱ͍ ͏Java͔Β΍ͬͯ͘Δಛผͳܕ͕͋Δ • ίϯύΠϧޙ͸Javaͷ௨ৗͷܕͱಉ༷ʹѻΘΕΔͷͰϥϯλΠϜ࣌ͷΦʔόʔϔο υ͸ͳ͍
  26. Nullڐ༰ܕ / Nullඇڐ༰ܕ • Ͱ͖Δ͚ͩNullඇڐ༰ܕ(Non-Null)Ͱఆٛ͢Δ • ඞཁͳ৔໘͚ͩʹNullڐ༰ܕ(Nullable)Λ࢖͏ • Nullڐ༰ܕ͸ಛผʹѻ͏ඞཁ͕͋Δ •

    Kotlin಺Ͱ͸ඞͣͲͪΒ͔ͷܕʹͳΔ͕ɺϓϥοτϑΥʔϜܕ(ܕ໊ͷޙΖʹ!)ͱ͍ ͏Java͔Β΍ͬͯ͘Δಛผͳܕ͕͋Δ • ίϯύΠϧޙ͸Javaͷ௨ৗͷܕͱಉ༷ʹѻΘΕΔͷͰϥϯλΠϜ࣌ͷΦʔόʔϔο υ͸ͳ͍
  27. ΤϧϏεԋࢉࢠ(?:) ԋࢉࢠ(?:)ͷલ͕nullͳΒޙΖͷ஋Λฦ͢ ΤϧϏεɾϓϨεϦʔ໊͕લͷ༝དྷ fun strLength(str : String?) : Int =

    str?.length ?: 0 >>> println(strLength("abc")) 3 >>> println(strLength(null)) 0 /VMMඇڐ༰ܕ
  28. είʔϓؔ਺ͱNullڐ༰ܕ είʔϓؔ਺ʹ͸ let / apply / with / run /

    also ͕͋Δ Nullඇڐ༰ܕΛҾ਺ʹͱΔؔ਺Λݺͼग़࣌͢ʹศར ϥϜμ(ޙड़)ͱ૊Έ߹Θͤͯ࢖͏ fun sendEmailTo(email: String) { /*...*/ } val email: String? = ... sendEmailTo(email) //Error! email?.let { sendEmailTo(it) } //OK! /VMMඇڐ༰ܕ
  29. Kotlin ͷݴޠػೳ • ม਺એݴ(val / var) & ܕਪ࿦ • Data

    classͱϓϩύςΟ • ੍ޚߏจ (if / when / for / when) • Nullڐ༰ܕ / Nullඇڐ༰ܕ • Lambda(ϥϜμ) • ඪ४ϥΠϒϥϦ & ֦ுؔ਺
  30. Lambda(ϥϜμ) ஋ͱͯ͠ѻ͑Δؔ਺ ߴ֊ؔ਺ͷҾ਺ʹͳΓಘΔ persons.maxBy { it.age } fun <T, R

    : Comparable<R>> Iterable<T>.maxBy(selector: (T) -> R): T? Ҿ਺͕ؔ਺ܕ
  31. Kotlin ͷݴޠػೳ • ม਺એݴ(val / var) & ܕਪ࿦ • Data

    classͱϓϩύςΟ • ੍ޚߏจ (if / when / for / when) • Nullڐ༰ܕ / Nullඇڐ༰ܕ • Lambda(ϥϜμ) • ඪ४ϥΠϒϥϦ & ֦ுؔ਺
  32. ঺հͨ͠Kotlin ͷݴޠػೳ • ม਺એݴ(val / var) & ܕਪ࿦ • Data

    classͱϓϩύςΟ • ੍ޚߏจ (if / when / for / when) • Nullڐ༰ܕ / Nullඇڐ༰ܕ • Lambda(ϥϜμ) • ඪ४ϥΠϒϥϦ & ֦ுؔ਺
  33. buid.gradle buildscript { ext.kotlin_version = '1.1.3-2' dependencies { classpath 'com.android.tools.build:gradle:3.0.0-beta3'

    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" }
  34. Kotlin with Android Studio • Ͳͷબ୒ࢶͰ΋Մೳ • ϓϩδΣΫτ։࢝࣌ʹKotlin supportΛબͿ •

    Tools>Kotlin>Configure Kotlin in ProjectΛ࢖ͬͯKotlinϓϩδΣΫτԽ • खॻ͖ͰKotlinͷઃఆΛgradleʹ௥Ճ
  35. Convert Java to Kotlin • JavaͷϑΝΠϧΛબ୒ͯ͠ʮCodeʯϝχϡʔ͔ ΒʮConvert Java File to

    Kotlin FileʯΛબͿ͜ͱ Ͱίʔυ͕Kotlinʹม׵͞ΕΔ • JavaͷίʔυΛKotlinͷϑΝΠϧ(.kt)΁ίϐϖ͢ Δ͜ͱͰ΋Kotlinʹม׵͞ΕΔ
  36. How to start Kotlin • Official Site : kotl.in/ ,

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